/**
* @Date 2019/3/11 - 16:36
* 对应数据库的字段
*/
public class User {
private int id;
private String username;
private String password;
private String email;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public User() {
}
public User(int id, String username, String password, String email, Date birthday) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", birthday=" + birthday +
'}';
}
}
Dao层
//接口
public interface IUserDao {
//插入数据
public void insert(User user);
//通过用户名和密码查找数据
public User findUser(String username,String password);
//通过用户名查找数据
public User findUser(String username);
}
//实现类
public class UserDaoImpl implements IUserDao {
@Override
public void insert(User user) {
Connection con = null;
PreparedStatement ps = null;
//1.注册驱动
try {
//2.获取Connection对象
con = DBUtils.getConnection();
//3.获取Statement对象
String sql = "INSERT INTO t_user (username,password,email,birthday)" +
"VALUES (?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getEmail());
ps.setDate(4, new Date(user.getBirthday().getTime()));
//4.执行sql语句
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5.关闭资源
DBUtils.close(con, ps, null);
}
}
@Override
public User findUser(String username, String password) {
User user = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
//1.注册驱动
try {
//2.获取Connection对象
con = DBUtils.getConnection();
//3.获取Statement对象
String sql = "SELECT * FROM t_user WHERE username = ? AND password = ?";
ps = con.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
while (rs.next()) {
user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setId(rs.getInt("id"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5.关闭资源
DBUtils.close(con, ps, rs);
}
return user;
}
@Override
public User findUser(String username) {
User user = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
//1.注册驱动
try {
//2.获取Connection对象
con = DBUtils.getConnection();
//3.获取Statement对象
String sql = "SELECT * FROM t_user WHERE username = ?";
ps = con.prepareStatement(sql);
ps.setString(1, username);
rs = ps.executeQuery();
while (rs.next()) {
user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setId(rs.getInt("id"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5.关闭资源
DBUtils.close(con, ps, rs);
}
return user;
}
}
Service层
//接口
public interface IUserService {
//注册业务
public void register(User user);
//登陆业务
public User login(String username, String password);
public User login(User user) throws UserLoginException;
//判断用户是否已经存在
public boolean userExist(String username);
}
//实现类
public class UserServiceImpl implements IUserService {
private IUserDao userDao = new UserDaoImpl();
@Override
public void register(User user) {
userDao.insert(user);
}
@Override
public User login(String username, String password) {
return userDao.findUser(username, password);
}
@Override
public User login(User user) throws UserLoginException {
User u = userDao.findUser(user.getUsername(), user.getPassword());
if (u != null) {
return u;
} else {
throw new UserLoginException("用户名或密码错误");
}
}
@Override
public boolean userExist(String username) {
User user = userDao.findUser(username);
return user != null;
}
}
校验表单数据
public class UserForm {
private String username;
private String password;
private String repassword;
private String email;
private String birthday;
private Map<String, String> err = new HashMap<>();
public boolean validate() {
//用户名不能以空格开头或结尾,中间不能有空格,只能是字母数字下划线
/**
* 1.用户名不能为空,用户名长度5~10
* 2.密码不能为空,密码长度大于6小于9
* 3.确认密码要与之前的密码一致
* 4.邮箱格式要正确
* 5.日期的格式也要正确
*/
if (username.startsWith(" ") && username.endsWith(" ")) {
err.put("username", "*用户名不能以空格开头或结尾");
} else if (username.trim().split(" ").length > 1) {
err.put("username", "*用户名中不能有空格");
} else if (!username.matches("\\w{5,15}")) {
err.put("username", "*用户名长度应为5~15位,只能包含数字与英文大小写");
} else if (username == null) {
err.put("username", "*用户名不能为空");
}
if (password.startsWith(" ") && password.endsWith(" ")) {
err.put("password", "*密码不能以空格开头或结尾");
} else if (password.trim().split(" ").length > 1) {
err.put("password", "*密码中不能有空格");
} else if (!password.matches("\\w{6,9}")) {
err.put("password", "*密码长度应为6~9位,只能包含数字与英文大小写");
} else if (password == null) {
err.put("password", "*密码不能为空");
}
if (!password.equals(repassword)) {
err.put("repassword", "*两次密码输入不一致");
}
if (!email.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")) {
err.put("email", "*邮箱格式输入有误");
} else if (email == null) {
err.put("email", "*邮箱不能为空");
}
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
try {
sdf.parse(birthday);
} catch (ParseException e) {
err.put("birthday", "*日期格式有误,日期格式应为YYYY-MM-DD");
}
if (birthday == null) {
err.put("birthday", "*生日不能为空");
}
return err.size() == 0;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Map<String, String> getErr() {
return err;
}
public void setErr(Map<String, String> err) {
this.err = err;
}
}