
1、RoleDao接口
package com.thhh.dao.role;
import com.thhh.pojo.Role;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public interface RoleDao {
/**
* 1、獲取角色列表
* @param conn:數據庫連接對象
* @return:返回的結構集
* @throws SQLException
*/
public List<Role> getRoleList(Connection conn) throws SQLException;
}
2、RoleDaoImpl接口實現
package com.thhh.dao.role;
import com.thhh.dao.BaseDao;
import com.thhh.pojo.Role;
import org.junit.Test;
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 RoleDaoImpl implements RoleDao{
//1、獲取角色列表
@Override
public List<Role> getRoleList(Connection conn) throws SQLException {
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Role> list = null;//存儲角色對象的集合
if (conn!=null){
list = new ArrayList<Role>();
String sql = "SELECT * FROM smbms_role";//直接寫死,不用參數
Object[] params = {};
rs = BaseDao.executeQuery(sql,params,conn,pstmt,rs);
while (rs.next()){
Role role = new Role();
role.setId(rs.getInt("id"));
role.setRoleCode(rs.getString("roleCode"));
role.setRoleName(rs.getString("roleName"));
list.add(role);
}
BaseDao.close(null,pstmt,rs);
}
return list;
}
}
3、RoleService接口
package com.thhh.service.role;
import com.thhh.pojo.Role;
import java.util.List;
public interface RoleService {
//1、獲取角色列表
public List<Role> getRoleList();
}
4、RoleService接口實現
package com.thhh.service.role;
import com.thhh.dao.BaseDao;
import com.thhh.dao.role.RoleDao;
import com.thhh.dao.role.RoleDaoImpl;
import com.thhh.pojo.Role;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class RoleServiceImpl implements RoleService{
private RoleDao roleDao = null;
public RoleServiceImpl() {
this.roleDao = new RoleDaoImpl(); //servlet中一旦調用這個service,就會實例化該Dao對象
}
@Override
public List<Role> getRoleList() {
Connection conn = null;//獲取連接
List<Role> roleList = null;
try {
conn = BaseDao.getConnection();
roleList = roleDao.getRoleList(conn);//服務層調用Dao層方法
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
BaseDao.close(conn,null,null);//關閉連接
}
return roleList;
}
}
5、測試
@Test
public void test(){
RoleService roleService = new RoleServiceImpl();
List<Role> list = roleService.getRoleList();
for (Role role:list) {
System.out.println(role.getId()+"\t"+role.getRoleName()+"\t"+role.getRoleCode());
}
}

6、編寫servlet

href="${pageContext.request.contextPath }/jsp/user.do?method=query"
/jsp/user.do
//這個URL映射的servlet我們在前面已經注冊過了,所以區別就是它后面跟的參數
method=query
這個參數就是在傳遞指定servlet應該調用的方法
這一次編寫的servlet比較的復雜,我們把它單獨拎出來看
//3、按照用戶名/職位名稱查詢用戶列表或整表查詢
//【重點&難點】
public void quary(HttpServletRequest req, HttpServletResponse resp){
//1、從前端獲取數據
String queryname = req.getParameter("queryname");
String queryUserRole = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");//通過隱藏域進行的提交,默認 = 1
int UserRole = 0;//我們先讓UserRole = 0,因為從前端接收到的queryUserRole可能就是一個NULL,此時我們就需要將其指定為0
int pageSize = 5;//這個數字最好是寫在配置文件中,這樣以后想要修改一頁上面顯示的行數,我們就不用再從新編譯代碼和測試了
int currentPageNo = 1;//先給當前頁設置一個默認的值
//2、通過判斷參數決定哪些請求需要處理
if (queryname == null){
queryname = "";//如果前端沒有按照用戶名查詢,我們就將用戶名設置""
}
if (queryUserRole!=null && queryUserRole!=""){
UserRole = Integer.parseInt(queryUserRole);//當前端傳過來的queryUserRole有數據的時候我們才對其進行解析
}
if (pageIndex!=null){
currentPageNo = Integer.parseInt(pageIndex);
}
//3、為了實現分頁,需要使用工具類PageSupport並傳入總用戶數,計算出totalPageCount
//參數totalCount由getUserCount得出;pageSize是固定死了的;currentPageNo默認設為1
UserService userService = new UserServiceImpl();
int totalCount = userService.getUserCount(queryname,UserRole);
//使用最開始導入的工具類
PageSupport pageSupport = new PageSupport();
pageSupport.setPageSize(pageSize);//設置一頁多少行數據
pageSupport.setTotalCount(totalCount);//設置總頁數
pageSupport.setCurrentPageNo(currentPageNo);//設置當前頁數
int totalPageCount = 0;
totalPageCount = pageSupport.getTotalPageCount();
//4、控制翻頁
if (currentPageNo<1){//在第一頁的時候還想點擊上一頁
currentPageNo = 1;
}else if(currentPageNo>pageSupport.getTotalPageCount()) {//在第最后一頁的時候還想點擊下一頁
currentPageNo = totalPageCount;
}
//5、用戶列表展示
List<User> userList = userService.getUserList(queryname,UserRole,currentPageNo,pageSize);
//將集合返回給前端進行解析顯示
req.setAttribute("userList",userList);
//6、角色列表展示
List<Role> roleList = new RoleServiceImpl().getRoleList();
req.setAttribute("roleList",roleList);
//7、將參數返回前端
req.setAttribute("queryUserName",queryname);
req.setAttribute("queryUserRole",queryUserRole);
req.setAttribute("totalPageCount",totalPageCount);
req.setAttribute("totalCount",totalCount);
req.setAttribute("currentPageNo",currentPageNo);
//8、重定向刷新頁面
try {
System.out.println("=======================進入到servlet,且i調用method = query");
req.getRequestDispatcher("userlist.jsp").forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
首先,這個servlet直接和前端的用戶管理頁面進行交互,且用戶管理頁面所有展示的數據都是由這個servlet的query()返回的,所以我們應該按照前端素材來編寫servlet的query()



通過上面的分析我們可以發現:我們需要從前端頁面獲取3個參數,並給前端返回7個參數
- 獲取前端參數
- queryname:按照姓名查詢的姓名
- queryUserRole:按照職位查詢的職位名稱
- pageIndex:當前的頁面index
- 傳遞給前端的參數
- userList:存儲前端展示的用戶(user)對象集合
- roleList:存儲前端展示的角色(role)對象集合
- queryname:再將這個參數傳遞回去是為了前端搜索之后搜索框中還保留着用戶搜索的值
- queryUserRole:作用同queryname
- totalCount:總共多少條用戶記錄
- currentPageNo:當前所在頁數
- totalPageCount:總頁數參數,注意:這個參數來自於工具類pageSupport

//1、從前端獲取數據
String queryname = req.getParameter("queryname");
String queryUserRole = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");//通過隱藏域進行的提交,默認 = 1
int UserRole = 0;//我們先讓UserRole = 0,因為從前端接收到的queryUserRole可能就是一個NULL,此時我們就需要將其指定為0
int pageSize = 5;//這個數字最好是寫在配置文件中,這樣以后想要修改一頁上面顯示的行數,我們就不用再從新編譯代碼和測試了
int currentPageNo = 1;//先給當前頁設置一個默認的值
//2、通過判斷參數決定哪些請求需要處理
if (queryname == null){
queryname = "";//如果前端沒有按照用戶名查詢,我們就將用戶名設置""
}
if (queryUserRole!=null && queryUserRole!=""){
UserRole = Integer.parseInt(queryUserRole);//當前端傳過來的queryUserRole有數據的時候我們才對其進行解析
}
if (pageIndex!=null){
currentPageNo = Integer.parseInt(pageIndex);
}
//3、為了實現分頁,需要使用工具類PageSupport並傳入總用戶數,計算出totalPageCount
//參數totalCount由getUserCount得出;pageSize是固定死了的;currentPageNo默認設為1
UserService userService = new UserServiceImpl();
int totalCount = userService.getUserCount(queryname,UserRole);
//使用最開始導入的工具類
PageSupport pageSupport = new PageSupport();
pageSupport.setPageSize(pageSize);//設置一頁多少行數據
pageSupport.setTotalCount(totalCount);//設置總頁數
pageSupport.setCurrentPageNo(currentPageNo);//設置當前頁數
int totalPageCount = 0;
totalPageCount = pageSupport.getTotalPageCount();
//4、控制翻頁
if (currentPageNo<1){//在第一頁的時候還想點擊上一頁
currentPageNo = 1;
}else if(currentPageNo>pageSupport.getTotalPageCount()) {//在第最后一頁的時候還想點擊下一頁
currentPageNo = totalPageCount;
}
//5、用戶列表展示
List<User> userList = userService.getUserList(queryname,UserRole,currentPageNo,pageSize);
//將集合返回給前端進行解析顯示
req.setAttribute("userList",userList)
//6、角色列表展示
List<Role> roleList = new RoleServiceImpl().getRoleList();
req.setAttribute("roleList",roleList);
//7、將參數返回前端
req.setAttribute("queryUserName",queryname);
req.setAttribute("queryUserRole",queryUserRole);
req.setAttribute("totalPageCount",totalPageCount);
req.setAttribute("totalCount",totalCount);
req.setAttribute("currentPageNo",currentPageNo);
//8、重定向刷新頁面
try {
System.out.println("=======================進入到servlet,且i調用method = query");
req.getRequestDispatcher("userlist.jsp").forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
7、測試



測試完成!
