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

📄 customerserviceimpl.java

📁 网上书城源代码~~在学习JAVA的时候做的
💻 JAVA
字号:
package com.briup.service.impl;

import com.briup.bean.Customer;
import com.briup.common.BeanFactory;
import com.briup.dao.ICustomerDao;
import com.briup.service.ICustomerService;
import com.briup.common.exception.CustomerServiceException;
import com.briup.common.transaction.HibernateTransaction;

public class CustomerServiceImpl implements ICustomerService {
	private ICustomerDao customerDao;
	public CustomerServiceImpl(){
		customerDao=(ICustomerDao) BeanFactory.getBean(BeanFactory.CUSTOMERDAO);
	}
	public Customer login(String name, String password)
			throws CustomerServiceException {
		HibernateTransaction tran=new HibernateTransaction();
		tran.beginTransaction();
		Customer c=null;
		try {
			c=customerDao.findCustomerByName(name);
			if(c!=null){
				if(c.getPassword().equals(password)){
					tran.commit();
				}else{
					throw new CustomerServiceException("密码错误");
				}
			}else{
				throw new CustomerServiceException("用户名不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
			if(e instanceof CustomerServiceException){
				throw (CustomerServiceException)e;
			}
		}
		return c;
	}

	public void register(Customer customer) throws CustomerServiceException {
		//创建事务对象
		HibernateTransaction tran=new HibernateTransaction();
		//启动事务
		tran.beginTransaction();
		try {
			String name=customer.getName();
			Customer c=customerDao.findCustomerByName(name);
			if(c==null){
				customerDao.saveOrupdateCustomer(customer);
				//提交事务
				tran.commit();
			}else{
				throw new CustomerServiceException("用户名已经存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			//回滚,在catch块中进行回滚
			tran.rollback();
			if(e instanceof CustomerServiceException){
				throw (CustomerServiceException)e;
			}
		}
		
	}

	public void update(Customer customer) throws CustomerServiceException {
		HibernateTransaction tran=new HibernateTransaction();
		tran.beginTransaction();
		try {
			customerDao.saveOrupdateCustomer(customer);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
			if(e instanceof CustomerServiceException){
				throw (CustomerServiceException)e;
			}
		}
	}

}

⌨️ 快捷键说明

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