📄 customerserviceimpl.java
字号:
package com.laoniu.service.impl;
import com.laoniu.bean.Customer;
import com.laoniu.common.BeanFactory;
import com.laoniu.common.exception.CustomerServiceException;
import com.laoniu.common.transaction.HibernateTransaction;
import com.laoniu.common.transaction.Transaction;
import com.laoniu.dao.ICustomerDao;
import com.laoniu.service.ICustomerService;
public class CustomerServiceImpl implements ICustomerService {
//登录
public Customer login(String name, String password)throws CustomerServiceException {
HibernateTransaction tran=new HibernateTransaction();
tran.beginTransaction();
ICustomerDao dao=(ICustomerDao)BeanFactory.getBean(BeanFactory.CUSTOMERDAO);
try {
Customer customer=dao.findCustomerByName(name);
if(null==customer){
throw new CustomerServiceException("用户还没有注册,请先注册");
}else{
if(password.equals(customer.getPassword())){
tran.commit();
return customer;
}
else{
throw new CustomerServiceException("密码不正确,请重新输入");
}
}
} catch (Exception e) {
if(e instanceof CustomerServiceException)
{
throw (CustomerServiceException)e;
}else
e.printStackTrace();
return null;
}
}
//注册
public void register(Customer customer) throws CustomerServiceException {
Transaction tran=new HibernateTransaction();
tran.beginTransaction();
Customer customer1=null;
ICustomerDao dao=(ICustomerDao)BeanFactory.getBean(BeanFactory.CUSTOMERDAO);
try {
customer1=dao.findCustomerByName(customer.getName());
if(customer1!=null){
throw new CustomerServiceException("用户已经存在!");
}
else{
dao.saveOrupdateCustomer(customer);
tran.commit();
}
} catch (Exception e) {
tran.rollback();
if(e instanceof CustomerServiceException)
{
throw (CustomerServiceException)e;
}
else
e.printStackTrace();
}
}
//保存或更新
public void update(Customer customer) throws CustomerServiceException {
//Transaction tran=new HibernateTransaction();
//tran.beginTransaction();
ICustomerDao dao=(ICustomerDao)BeanFactory.getBean(BeanFactory.CUSTOMERDAO);
try {
dao.saveOrupdateCustomer(customer);
//tran.commit();
} catch (Exception e) {
//tran.rollback();
e.printStackTrace();
}
}
public Customer findByid(Long id) throws CustomerServiceException
{
HibernateTransaction tran=new HibernateTransaction();
ICustomerDao dao=(ICustomerDao)BeanFactory.getBean(BeanFactory.CUSTOMERDAO);
Customer customer=null;
try {
tran.beginTransaction();
customer=dao.findCustomerById(id);
if(customer==null)
throw new CustomerServiceException("用户不存在");
tran.commit();
return customer;
} catch (Exception e) {
e.printStackTrace();
throw new CustomerServiceException(e.getMessage());
}
}
public Customer findByname(String name) throws CustomerServiceException
{
HibernateTransaction tran=new HibernateTransaction();
ICustomerDao dao=(ICustomerDao)BeanFactory.getBean(BeanFactory.CUSTOMERDAO);
Customer customer=null;
try {
tran.beginTransaction();
customer=dao.findCustomerByName(name);
if(customer==null)
throw new CustomerServiceException("用户不存在");
tran.commit();
return customer;
} catch (Exception e) {
e.printStackTrace();
tran.rollback();
throw new CustomerServiceException(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -