1.最后一个模块Role接口以及Role接口实现类
package com.kuang.dao.role;
import com.kuang.pojo.Role;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public interface RoleDao {
//查询Role表的列表
public List<Role> getRoleList(Connection connection) throws SQLException;
}
package com.kuang.dao.role;
import com.kuang.dao.BaseDao;
import com.kuang.pojo.Role;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class RoleDaoImp implements RoleDao{
@Override
public List<Role> getRoleList(Connection connection) throws SQLException {
PreparedStatement preparedStatement = null;
ResultSet rs = null;
List<Role> roles = new ArrayList<Role>();;
if (connection != null) {
String sql = "select * from smbms_role";
Object[] param = {};
rs = BaseDao.execute(connection, preparedStatement, rs, sql, param);
while (rs.next()) {
Role _role = new Role();
_role.setId(rs.getInt("id"));
_role.setRoleCode(rs.getString("roleCode"));
_role.setRoleName(rs.getString("roleName"));
roles.add(_role);
}
BaseDao.closeResource(null, preparedStatement, rs);
}
return roles;
}
}
2.Role业务层接口 以及业务层接口实现类
package com.kuang.service.role;
import com.kuang.pojo.Role;
import java.sql.Connection;
import java.util.List;
public interface RoleService {
//查询Role表
public List<Role> getRoleList();
}
package com.kuang.service.role;
import com.kuang.dao.BaseDao;
import com.kuang.dao.role.RoleDao;
import com.kuang.dao.role.RoleDaoImp;
import com.kuang.pojo.Role;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class RoleServiceIml implements RoleService{
private RoleDao roleDao;
public RoleServiceIml(){
roleDao=new RoleDaoImp();
}
@Override
public List<Role> getRoleList() {
Connection connection=null;
List<Role> roles=null;
try {
connection= BaseDao.getConnection();
roles = roleDao.getRoleList(connection);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return roles;
}
}
3.实现Servlet整合在一起
//查询用户表单
public void query(HttpServletRequest req, HttpServletResponse resp){
//查询用户的名字
String queryUserName = req.getParameter("queryname");
//查询用户角色ID
String queryUserRole = req.getParameter("queryUserRole");
//获取到当前页
String pageIndex = req.getParameter("pageIndex");
int UserRole=0;
int currentPageNo=1;
int pageSize= Constants.PAGE_SIZE;
if (queryUserName==null){
queryUserName="";
}
if (!StringUtils.isNullOrEmpty(queryUserRole)){
UserRole=Integer.parseInt(queryUserRole);
}
if (!StringUtils.isNullOrEmpty(pageIndex)){
currentPageNo=Integer.parseInt(pageIndex);
}
UserServiceIml userServiceIml = new UserServiceIml();
int userCount = userServiceIml.getUserCount(queryUserName, UserRole);
//分页工具类的构造算出页的总数
PageSupport pageSupport = new PageSupport();
pageSupport.setCurrentPageNo(currentPageNo);
pageSupport.setPageSize(pageSize);
pageSupport.setTotalCount(userCount);
pageSupport.setTotalPageCountByRs();
//获取页的总数
int totalPageCount = pageSupport.getTotalPageCount();
if (currentPageNo<1){
currentPageNo=1;
}else if (currentPageNo>totalPageCount){
currentPageNo=totalPageCount;
}
RoleServiceIml roleServiceIml = new RoleServiceIml();
//查询出用户角色的列表
List<Role> roleList = roleServiceIml.getRoleList();
//查询用户列表
List<User> userList = userServiceIml.getUserList(queryUserName, UserRole, currentPageNo, pageSize);
req.setAttribute("totalCount",userCount);
req.setAttribute("currentPageNo",currentPageNo);
req.setAttribute("totalPageCount",totalPageCount);
req.setAttribute("roleList",roleList);
req.setAttribute("userList",userList);
try {
req.getRequestDispatcher("userlist.jsp").forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}