// 根据用户名或者角色查询用户总数
public int getUserCount(Connection connection,String username,int userRole) throws SQLException;
UserImpl
// 根据用户名或者角色查询用户总数【最难理解的SQL】
public int getUserCount(Connection connection, String username, int userRole) throws SQLException {
PreparedStatement pstm = null;
ResultSet rs = null;
int count = 0;
if (connection!=null){
StringBuffer sql = new StringBuffer();
sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id ");
ArrayList
UserService
// 查询记录数
public int getUserCount(String username,int userRole);
UserServiceImpl
public class UserServiceImpl implements UserService{
// 业务层都会调用dao层,所有我们要引入Dao层
private UserDao userDao;
public UserServiceImpl(){
userDao = new UserDaoImpl();
}
public User login(String userCode, String password) {
Connection connection = null;
User user = null;
try {
connection = BaseDao.getConnection();
// 通过业务层调用对应的具体的数据库操作
user = userDao.getLoginUser(connection,userCode);
}catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;
}
public boolean updatePwd(int id, String pwd) {
Connection connection = null;
boolean flag = false;
// 修改密码
try {
connection = BaseDao.getConnection();
if (userDao.updatePwd(connection,id,pwd)>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return flag;
}
// 查询记录数
public int getUserCount(String username, int userRole) {
Connection connection = null;
int count = 0;
try {
connection = BaseDao.getConnection();
count = userDao.getUserCount(connection, username, userRole);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return count;
}
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
int userCount = userService.getUserCount(null,0);
System.out.println(userCount);
}
}
2.获取用户列表
userDao
// 通过条件查询-userList
public List getUserList(Connection connection,String userName,int userRole,int currentPageNo,int pageSize)throws Exception;
userDaoImpl
//通过用户输入的条件查询用户列表
public List getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception {
List userList = new ArrayList();
PreparedStatement pstm=null;
ResultSet rs=null;
if(connection!=null){
StringBuffer sql = new StringBuffer();
sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");
List list = new ArrayList();
if(!StringUtils.isNullOrEmpty(userName)){
sql.append(" and u.userName like ?");
list.add("%"+userName+"%");
}
if(userRole > 0){
sql.append(" and u.userRole = ?");
list.add(userRole);
}
sql.append(" order by creationDate DESC limit ?,?");
currentPageNo = (currentPageNo-1)*pageSize;
list.add(currentPageNo);
list.add(pageSize);
Object[] params = list.toArray();
System.out.println("sql ----> " + sql.toString());
rs = BaseDao.execute(connection,pstm,rs,sql.toString(),params);
while(rs.next()){
User _user = new User();
_user.setId(rs.getInt("id"));
_user.setUserCode(rs.getString("userCode"));
_user.setUserName(rs.getString("userName"));
_user.setGender(rs.getInt("gender"));
_user.setBirthday(rs.getDate("birthday"));
_user.setPhone(rs.getString("phone"));
_user.setUserRole(rs.getInt("userRole"));
_user.setUserRoleName(rs.getString("userRoleName"));
userList.add(_user);
}
BaseDao.closeResource(null, pstm, rs);
}
return userList;
}
userService
// 根据条件查询用户列表
public List getUserList(String queryUserName,int queryUserRole,int currentPageNo,int pageSize);
userServiceImpl
public List getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
// TODO Auto-generated method stub
Connection connection = null;
List userList = null;
System.out.println("queryUserName ---- > " + queryUserName);
System.out.println("queryUserRole ---- > " + queryUserRole);
System.out.println("currentPageNo ---- > " + currentPageNo);
System.out.println("pageSize ---- > " + pageSize);
try {
connection = BaseDao.getConnection();
userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
BaseDao.closeResource(connection, null, null);
}
return userList;
}
3.获取角色操作
为了我们职责统一,可以把角色的操作单独放在一个包中,和pojo类对应
RoleDao
// 获取角色列表
public List getRoleList(Connection connection)throws SQLException;
RoleDaoImpl
public class RoleDaoImpl implements RoleDao{
// 获取角色列表
public List getRoleList(Connection connection)throws SQLException {
PreparedStatement pstm = null;
ResultSet resultSet = null;
ArrayList roles = new ArrayList();
if (connection!=null) {
String sql = "select * from smbms_role";
Object[] params = {};
resultSet = BaseDao.execute(connection, pstm, resultSet, sql, params);
while(resultSet.next()){
Role role = new Role();
role.setId(resultSet.getInt("id"));
role.setRoleCode(resultSet.getString("roleCode"));
role.setRoleName(resultSet.getString("roleName"));
roles.add(role);
}
BaseDao.closeResource(null, pstm, resultSet);
}
return roles;
}
}
RoleService
// 获取角色列表
public List getRoleList();
RoleServiceImpl
public class RoleServiceImpl implements RoleService {
// 引入Dao
private RoleDao roleDao;
public RoleServiceImpl() {
roleDao = new RoleDaoImpl();
}
public List getRoleList() {
Connection connection = null;
List roleList = null;
try {
connection = BaseDao.getConnection();
roleList = roleDao.getRoleList(connection);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection, null, null);
}
return roleList;
}
}
4.用户显示的Servlet
获取用户前端的数据(查询)
判断请求是否需要执行,看参数的值判断
为了实现分页,需要计算出当前页面和总页面,页面大小…
用户列表展示
返回前端
//重点,难点
public void query(HttpServletRequest req, HttpServletResponse resp){
// 查询用户列表
// 从前端获取数据:
String queryUserName = req.getParameter("queryname");
String temp = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");
int queryUserRole = 0;
// 获得用户列表
UserServiceImpl userService = new UserServiceImpl();
List userList = null;
// 第一次走这个请求,一定是第一页,页面大小固定的:
int pageSize = 5;//可以把这些写到配置文件中,方便后期修改:
int currentPageNo = 1;
if (queryUserName==null){
queryUserName = "";
}
if (temp!=null&&!temp.equals("")){
queryUserRole = Integer.parseInt(temp);//给查询赋值!0,1,2,3
}
if (pageIndex!=null){
currentPageNo = Integer.parseInt(pageIndex);
}
// 获取用户的总数(分页:上一页,下一页的情况)
int totalCount = userService.getUserCount(queryUserName, queryUserRole);
// 总页数支持
PageSupport pageSupport = new PageSupport();
pageSupport.setCurrentPageNo(currentPageNo);
pageSupport.setPageSize(pageSize);
pageSupport.setTotalCount(totalCount);
int totalPageCount = pageSupport.getTotalPageCount();//总共有几页
System.out.println(currentPageNo);
System.out.println(totalPageCount);
// 控制首页和尾页
// 如果页面要小于1了,就显示第一页的数据
if (totalPageCount<1){
currentPageNo = 1;
}else if (currentPageNo>totalPageCount){//当前页面大于最后一页;
currentPageNo = totalPageCount;
}
// 获取用户列表展示
userList = userService.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
req.setAttribute("userList",userList);
// 获取角色列表展示
RoleServiceImpl roleService = new RoleServiceImpl();
List roleList = roleService.getRoleList();
req.setAttribute("roleList",roleList);
req.setAttribute("totalCount",totalCount);
req.setAttribute("currentPageNo",currentPageNo);
req.setAttribute("totalPageCount",totalPageCount);
req.setAttribute("queryUserName",queryUserName);
req.setAttribute("queryUserRole",queryUserRole);
// 返回前端
try {
req.getRequestDispatcher("userlist.jsp").forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}