📄 userservicehibernateimpl.java
字号:
package com.jsfabc.jsh.model.service.impl;
import com.jsfabc.jsh.model.service.UserService;
import com.jsfabc.jsh.model.exception.UserException;
import com.jsfabc.jsh.model.exception.UserNotExistException;
import com.jsfabc.jsh.model.exception.DataNotFoundException;
import com.jsfabc.jsh.model.exception.DbException;
import com.jsfabc.jsh.model.dao.hibernateImpl.UserDaoHibernateImpl;
import com.jsfabc.jsh.model.exception.DuplicateUserException;
import com.jsfabc.jsh.model.exception.DuplicateKeyException;
import org.springframework.dao.DataAccessException;
import com.jsfabc.jsh.model.bo.User;
import com.jsfabc.jsh.model.dao.UserDao;
import java.util.Date;
public class UserServiceHibernateImpl implements UserService{
//数据访问对象
private UserDao userDao;
//依赖注入
public void setUserDao(UserDao newValue){
this.userDao=newValue;
}
//构造函数
public UserServiceHibernateImpl() {
}
public User login(String userName, String password) throws UserException {
//构建一个新用户
User user=new User();
try {
//UserDao dao=new UserDaoHibernateImpl();
user=userDao.find(userName);
if(user!=null){
//判断密码
if(password.equals(user.getPassword())){
//加入登录日期
Date loginDate=new Date();
user.setLoginDate(loginDate);
}
else{
//表示用户名和密码合乎要求的用户不存在
user=null;
}
}
else{
throw new DataNotFoundException(userName);
}
return user;
}
catch(DataNotFoundException ue){
ue.printStackTrace();
throw new UserNotExistException(userName);
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public User register(String userName, String password, String pwd)
throws UserException {
//构建一个新用户
User user=new User();
//创建一个新的数据访问对象
//UserDao dao=new UserDaoHibernateImpl();
try {
//判断密码
if(password.equals(pwd)){
//设置用户属性
user.setUserName(userName);
user.setPassword(password);
//用dao在数据库表中创建一个用户记录
String userID=userDao.create(user);
}
else{
//表示欲注册的用户名不合乎要求,不能返回用户对象
user=null;
}
//返回用户对象
return user;
}
//处理数据库访问异常
catch(DataAccessException daoe){
//判断是否是因为该用户名已经注册过
try{
userDao.find(userName);
}
catch(DataAccessException daoe1){
throw new DbException(daoe.getMessage());
}
daoe.printStackTrace();
//如果该用户名已经注册,则抛出重复的用户异常
throw new DuplicateUserException(userName);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -