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

📄 customerdaoimpl.java

📁 由于跟踪客户的购物意向
💻 JAVA
字号:
/*
 */

package com.relationinfo.customertrace.daoimpl;

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

import org.hibernate.Session;

import com.relationinfo.customertrace.dao.CustomerDao;
import com.relationinfo.customertrace.dto.Customer;
import com.relationinfo.customertrace.dto.CustomerPk;
import com.relationinfo.customertrace.exceptions.CustomerDaoException;

public class CustomerDaoImpl implements CustomerDao {
	/**
	 * 增加新记录到 customer table.
	 */
	public CustomerPk insert(Customer dto) throws CustomerDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 创建和复制Hibernate对象
			com.relationinfo.customertrace.hibernate.Customer hibernate = new com.relationinfo.customertrace.hibernate.Customer();
			hibernate.setCustomertypecode(dto.getCustomertypecode());
			hibernate.setCustomername(dto.getCustomername());
			hibernate.setAddress(dto.getAddress());
			hibernate.setPhone(dto.getPhone());
			hibernate.setMobile(dto.getMobile());
			hibernate.setEmail(dto.getEmail());
			hibernate.setBirthday(dto.getBirthday());
			hibernate.setHobby(dto.getHobby());
			hibernate.setNote(dto.getNote());

			session.save(hibernate);

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

	}

	/**
	 * 更新单笔记录 customer table.
	 */
	public void update(CustomerPk pk, Customer dto) throws CustomerDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 使用DTO主键值返回Hibernate类
			List list = HibernateUtil.executeSQLQuery("customer",
					Customer.class, "customercode = ?", new Object[] { pk
							.getCustomercode() });
			if (list.size() == 0) {
				throw new CustomerDaoException("row not found");
			}

			com.relationinfo.customertrace.hibernate.Customer hibernate = (com.relationinfo.customertrace.hibernate.Customer) list
					.get(0);
			hibernate.setCustomercode(dto.getCustomercode());
			hibernate.setCustomertypecode(dto.getCustomertypecode());
			hibernate.setCustomername(dto.getCustomername());
			hibernate.setAddress(dto.getAddress());
			hibernate.setPhone(dto.getPhone());
			hibernate.setMobile(dto.getMobile());
			hibernate.setEmail(dto.getEmail());
			hibernate.setBirthday(dto.getBirthday());
			hibernate.setHobby(dto.getHobby());
			hibernate.setNote(dto.getNote());

			session.save(hibernate);

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

	}

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

			// 使用DTO主键值返回Hibernate类
			List list = HibernateUtil.executeSQLQuery("customer",
					Customer.class, "customercode = ?", new Object[] { pk
							.getCustomercode() });
			if (list.size() == 0) {
				throw new CustomerDaoException("row not found");
			}

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

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

	}

	/**
	 * Returns the rows from the customer table that matches the specified
	 * primary-key value.
	 */
	public Customer findByPrimaryKey(CustomerPk pk) throws CustomerDaoException {
		return findByPrimaryKey(pk.getCustomercode());
	}

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

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

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

	/**
	 * Returns all rows from the customer table that match the criteria
	 * 'customercode = :customercode'.
	 */
	public Customer[] findWhereCustomercodeEquals(String customercode)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "customercode = ?",
				new Object[] { customercode }));
	}

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

	/**
	 * Returns all rows from the customer table that match the criteria
	 * 'customername = :customername'.
	 */
	public Customer[] findWhereCustomernameEquals(String customername)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "customername = ?",
				new Object[] { customername }));
	}

	/**
	 * Returns all rows from the customer table that match the criteria 'address =
	 * :address'.
	 */
	public Customer[] findWhereAddressEquals(String address)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "address = ?", new Object[] { address }));
	}

	/**
	 * Returns all rows from the customer table that match the criteria 'phone =
	 * :phone'.
	 */
	public Customer[] findWherePhoneEquals(String phone)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "phone = ?", new Object[] { phone }));
	}

	/**
	 * Returns all rows from the customer table that match the criteria 'mobile =
	 * :mobile'.
	 */
	public Customer[] findWhereMobileEquals(String mobile)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "mobile = ?", new Object[] { mobile }));
	}

	/**
	 * Returns all rows from the customer table that match the criteria 'email =
	 * :email'.
	 */
	public Customer[] findWhereEmailEquals(String email)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "email = ?", new Object[] { email }));
	}

	/**
	 * Returns all rows from the customer table that match the criteria
	 * 'birthday = :birthday'.
	 */
	public Customer[] findWhereBirthdayEquals(Date birthday)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "birthday = ?",
				new Object[] { birthday == null ? null : new java.sql.Date(
						birthday.getTime()) }));
	}

	/**
	 * Returns all rows from the customer table that match the criteria 'hobby =
	 * :hobby'.
	 */
	public Customer[] findWhereHobbyEquals(String hobby)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "hobby = ?", new Object[] { hobby }));
	}

	/**
	 * Returns all rows from the customer table that match the criteria 'note =
	 * :note'.
	 */
	public Customer[] findWhereNoteEquals(String note)
			throws CustomerDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("customer",
				Customer.class, "note = ?", new Object[] { note }));
	}

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

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

			list.add(dto);
		}

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

}

⌨️ 快捷键说明

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