狂神说 javaweb 35集:smbms用户管理底层实现_SMBMS 项目搭建(六)
用户管理实现
-
导入分页的工具类
-PageSupport
-
用户列表页面导入
-userlist.jsp
-rollpage.jsp
1、获取用户数量
-
UserDao
//根据用户名或者角色查询用户总数
public int getUserCount (Connection connection, String username, int userRole) throws SQLException; -
UserDaoImpl
//根据用户名或者角色查询用户总数【最难理解的SQL】
public int getUserCount(Connection connection, String username, int userRole) throws SQLException {
ResultSet rs = null;
PreparedStatement pstm = null;
int count = 0;
if (null != connection) {
StringBuffer sql = new StringBuffer();
sql.append( "select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id" );
ArrayList<Object> list = new ArrayList<Object>();//存放我们的参数
if (!StringUtils.isNullOrEmpty( username )) {
sql.append( " and u.username like ?" );
list.add( "%"+ username +"%" );//index:0
}
if (userRole > 0) {
sql.append( " and u.userRole = ?" );
list.add( userRole );//index:1
}
//怎么把list转换为数组
Object[] params = list.toArray();
System.out.println("UserDaoImpl--> getUserCount:" + sql.toString());//输出最后完整的SQL语句
rs = BaseDao.execute( connection, pstm, rs, sql.toString(), params );
if (rs.next()){
count = rs.getInt( "count" );//从结果集中获取最终的数量
}
BaseDao.closeResource( null,pstm,rs );
}
return count;
} -
UserService
//查询记录数
public int getUserCount(String username, int userRole); -
UserServiceImpl
//查询记录数
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 throwables) {
throwables.printStackTrace();
}finally {
BaseDao.closeResource( connection,null,null );
}
return count;
}
-
UserDao
//通过条件查询-userList
public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception; -
UserDaoImpl
//通过条件查询-userList
public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize)
throws Exception {
PreparedStatement pstm = null;
ResultSet rs = null;
List<User> userList = new ArrayList<User>();
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<Object> list = new ArrayList<Object>();
if (!StringUtils.isNullOrEmpty(userName)) {
sql.append(" and u.userName like ?");
list.add("%" + userName + "%");
}
if (userRole > 0) {
sql.append(" and u.userRole = ?");
list.add(userRole);
}
//在mysql数据库中,分页使用 limit startIndex,pageSize ; 总数
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<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize); -
UserServiceImpl
//根据条件查询用户列表
public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
Connection connection = null;
List<User> userList = null;
try {
connection = BaseDao.getConnection();
userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
} catch (Exception e) {
e.printStackTrace();
} finally {
BaseDao.closeResource(connection, null, null);
}
return userList;
} -
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人