⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customertypedaoimpl.java

📁 由于跟踪客户的购物意向
💻 JAVA
字号:
package com.relationinfo.customertrace.daoimpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import com.relationinfo.customertrace.dao.CustomerTypeDao;
import com.relationinfo.customertrace.dto.CustomerType;
import com.relationinfo.customertrace.dto.CustomerTypePk;
import com.relationinfo.customertrace.exceptions.CustomerTypeDaoException;

public class CustomerTypeDaoImpl implements CustomerTypeDao {
	/**
	 * 增加新记录到 customerType table.
	 */
	public CustomerTypePk insert(CustomerType dto)
			throws CustomerTypeDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 创建和复制Hibernate对象
			com.relationinfo.customertrace.hibernate.CustomerType hibernate = new com.relationinfo.customertrace.hibernate.CustomerType();
			hibernate.setCustomertypename(dto.getCustomertypename());

			session.save(hibernate);

			// 返回DTO主键对象
			return dto.createPk();
		} catch (Exception _e) {
			System.out.println(_e);
			throw new CustomerTypeDaoException(_e.getMessage());
		}

	}

	/**
	 * 更新单笔记录 customerType table.
	 */
	public void update(CustomerTypePk pk, CustomerType dto)
			throws CustomerTypeDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 使用DTO主键值返回Hibernate类
			List list = HibernateUtil.executeSQLQuery("customerType",
					CustomerType.class, "customertypecode = ?",
					new Object[] { pk.getCustomertypecode() });
			if (list.size() == 0) {
				throw new CustomerTypeDaoException("row not found");
			}

			com.relationinfo.customertrace.hibernate.CustomerType hibernate = (com.relationinfo.customertrace.hibernate.CustomerType) list
					.get(0);
			hibernate.setCustomertypecode(dto.getCustomertypecode());
			hibernate.setCustomertypename(dto.getCustomertypename());

			session.save(hibernate);

		} catch (Exception _e) {
			System.out.println(_e);
			throw new CustomerTypeDaoException(_e.getMessage());
		}

	}

	/**
	 * Deletes a single row in the customerType table.
	 */
	public void delete(CustomerTypePk pk) throws CustomerTypeDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 使用DTO主键值返回Hibernate类
			List list = HibernateUtil.executeSQLQuery("customerType",
					CustomerType.class, "customertypecode = ?",
					new Object[] { pk.getCustomertypecode() });
			if (list.size() == 0) {
				throw new CustomerTypeDaoException("row not found");
			}

			com.relationinfo.customertrace.hibernate.CustomerType hibernate = (com.relationinfo.customertrace.hibernate.CustomerType) list
					.get(0);
			session.delete(hibernate);

		} catch (Exception _e) {
			System.out.println(_e);
			throw new CustomerTypeDaoException(_e.getMessage());
		}

	}

	/**
	 * Returns the rows from the customerType table that matches the specified
	 * primary-key value.
	 */
	public CustomerType findByPrimaryKey(CustomerTypePk pk)
			throws CustomerTypeDaoException {
		return findByPrimaryKey(pk.getCustomertypecode());
	}

	/**
	 * Returns all rows from the customerType table that match the criteria
	 * 'customertypecode = :customertypecode'.
	 */
	public CustomerType findByPrimaryKey(String customertypecode)
			throws CustomerTypeDaoException {
		CustomerType ret[] = convertToDTO(HibernateUtil.executeSQLQuery(
				"customerType", CustomerType.class, "customertypecode = ?",
				new Object[] { customertypecode }));
		return ret.length == 0 ? null : ret[0];
	}

	/**
	 * Returns all rows from the customerType table that match the criteria ''.
	 */
	public CustomerType[] findAll() throws CustomerTypeDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customerType",
				CustomerType.class, "", null));
	}

	/**
	 * Returns all rows from the customerType table that match the criteria
	 * 'customertypecode = :customertypecode'.
	 */
	public CustomerType[] findWhereCustomertypecodeEquals(
			String customertypecode) throws CustomerTypeDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customerType",
				CustomerType.class, "customertypecode = ?",
				new Object[] { customertypecode }));
	}

	/**
	 * Returns all rows from the customerType table that match the criteria
	 * 'customertypename = :customertypename'.
	 */
	public CustomerType[] findWhereCustomertypenameEquals(
			String customertypename) throws CustomerTypeDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customerType",
				CustomerType.class, "customertypename = ?",
				new Object[] { customertypename }));
	}

	/**
	 * Method 'CustomerTypeDaoImpl'
	 * 
	 */
	public CustomerTypeDaoImpl() {
	}

	/**
	 * Method 'convertToDTO'
	 * 
	 * @param hibernateList
	 * @throws CustomerTypeDaoException
	 * @return CustomerType[]
	 */
	public CustomerType[] convertToDTO(List hibernateList)
			throws CustomerTypeDaoException {
		Iterator iter = hibernateList.iterator();
		List list = new ArrayList();
		while (iter.hasNext()) {
			CustomerType dto = (CustomerType) iter.next();
			list.add(dto);
		}

		CustomerType ret[] = new CustomerType[list.size()];
		list.toArray(ret);
		return ret;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -