接上一篇用户注册之用户名验证例题
html页面都是同一个,这里点击注册按钮实现注册功能
一、entity实体层
1.1 User类
不加构造方法原因是要使用BeanUtils,不用添加构造方法
重点是Serializable接口Serializable接口-所有实体类都继承
二、dao层代码
dao层没什么特别的
就是提供数据库访问
2.1UserDao接口代码
package com.itqf.dao;
import com.itqf.entity.User;
/*
* 用户模块数据库访问的接口*/
public interface UserDao {
public User selectUserByUname(String username);
public int insert(User user);
}
2.2UserDaoImpl类代码
package com.itqf.dao.impl;
import com.itqf.dao.UserDao;
import com.itqf.entity.User;
import com.itqf.utils.C3p0Utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.Connection;
import java.sql.SQLException;
/*
* 用户模块数据库访问的实现类*/
public class UserDaoImpl implements UserDao {
private QueryRunner queryRunner = new QueryRunner(C3p0Utils.getDataSource());
@Override
public User selectUserByUname(String username) {
User user = null;
String sql = "select u_id as uid,u_name as uname,u_password as upassword,u_email as uemail,u_sex as usex,u_status as status,u_code as ucode,u_role as urole from user where u_name = ?;";
try {
user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
@Override
public int insert(User user) {
int result = 0;
String sql = "insert into user(u_name,u_password,u_email,u_sex,u_status,u_code,u_role) values(?,?,?,?,?,?,?);";
try {
result = queryRunner.update(sql, user.getUname(), user.getUpassword(), user.getUemail(), user.getUsex(), user.getStatus(), user.getUcode(), user.getUrole());
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
三 、service层
3.1 UserService接口代码
package com.itqf.service;
import com.itqf.entity.User;
public interface UserService {
//检测用户名是否存在
public boolean checkUser(String name);
//注册
public int register(User user);
}
3.2 UserServiceImpl类代码
package com.itqf.service.impl;
import com.itqf.dao.UserDao;
import com.itqf.dao.impl.UserDaoImpl;
import com.itqf.entity.User;
import com.itqf.service.UserService;
public class UserServiceImpl implements UserService {
UserDao userDao = new UserDaoImpl();
@Override
public