Day18JavaWeb【旅遊專案】 搜尋與分頁的後臺程式碼***

2020-10-06 11:00:17

搜尋與分頁的後臺程式碼

  • (1)使用者輸入路線名稱關鍵字,後臺將資料返回給前臺
  • (2)前臺使用分頁顯示
  • (3)分頁工具條
  • (4)分頁列表
    在這裡插入圖片描述
    在這裡插入圖片描述

RouteServiceTest

public class TestRouteService {
    @Test
    public void test01(){
        //業務物件
        RouteService  routeService = new RouteService();
        //業務方法 search
        //為什麼返回結果不能是一個List<Route>就可以搞定?
        //搜尋結果可能是幾十萬條資料,不顯示全部而是按分頁來顯示
        //分頁中需要的資料有 總記錄數100 每頁記錄數20 總頁數5 當前1 當前頁的集合
        PageBean pageBean =  routeService.search("寧夏",1,20);
        //顯示
        System.out.println(pageBean);
    }
}

PageBean

public class PageBean {
    //總記錄數 100
    private int totalCount;
    //每頁記錄數 20
    private  int pageSize;
    //總頁數 5
    private int totalPage;
    //當前頁號
    private int currentPage;
    //當前頁的資料
    private List<Route> list;

Route

從資料裡面複製

//旅遊路線
public class Route {
    private int rid;//線路id,必輸
    private String rname;//線路名稱,必輸
    private double price;//價格,必輸
    private String routeIntroduce;//線路介紹
    private String rflag;   //是否上架,必輸,0代表沒有上架,1代表是上架
    private String rdate;   //上架時間
    private String isThemeTour;//是否主題旅遊,必輸,0代表不是,1代表是
    private int count;//收藏數量
    private int cid;//所屬分類,必輸
    private String rimage;//縮圖
    private int sid;//所屬商家
    private String sourceId;//抓取資料的來源id

RouteService

public class RouteService {
    //搜尋功能
    public PageBean search(String keyword, int currentPage, int pageSize) {
        //建立分頁資料物件  包含4個整數與一個集合
        PageBean pb = new PageBean();
        //設定當前頁號
        pb.setCurrentPage(currentPage);//1*
        //設定每頁記錄數
        pb.setPageSize(pageSize);//2
        //設定總記錄數
        RouteDao routeDao = MySessionUtils2.getMapper(RouteDao.class);
        //'%長沙%'
        int totalCount = routeDao.findCountByName("%" + keyword + "%");
        pb.setTotalCount(totalCount);//3*

        //設定總頁數  每頁最多隻能放20條   40  2  與 41  3
        //在java中,整數相除只保留整數部,丟失小數部  41/20  就是2
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        pb.setTotalPage(totalPage);//4*

        //設定PageBean中的list集合
        int start = (currentPage - 1) * pageSize;
        List<Route> list = routeDao.findPageByName("%" + keyword + "%", start, pageSize);
        pb.setList(list);
        return pb;
    }
}

RouteDao

public interface RouteDao {
    //select count(*) from tab_route where rname like '%長沙%';
    int findCountByName(String keyword);

    //select * from tab_route where rname like '%長沙%' limit 0,20;
    List<Route> findPageByName(String keyword, int start, int pageSize);
}

com\wzx\dao\RouteDao.xml

    <!--  List<Route> findPageByName(String keyword, int start, int pageSize); -->
    <select id="findPageByName" resultType="route">
        select * from tab_route where rname like #{arg0} limit #{arg1},#{arg2};
    </select>
    <!-- <select id="findPageByName" resultType="route" >
         select * from tab_route where rname like #{param1} limit #{param2},#{param3};
     </select>
     -->

核心組態檔

    <mappers>
        <mapper resource="com/wzx/dao/UserDao.xml"/>
        <mapper resource="com/wzx/dao/CategoryDao.xml"/>
        <mapper resource="com/wzx/dao/RouteDao.xml"/>
    </mappers>

RouteServlet


@WebServlet("/routeServlet")
public class RouteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接收請求,獲取引數
        String keyword = request.getParameter("keyword");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        int currentPage = 1;
        if (currentPageStr != null && currentPageStr.length() > 0) {
            //"11"  "你好"
            try {
                currentPage = Integer.parseInt(currentPageStr);//11
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        int pageSize = 20;
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            //"30"  "你好"
            try {
                pageSize = Integer.parseInt(pageSizeStr);//11
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //處理引數
        RouteService routeService = new RouteService();
        //PageBean pb =  routeService.search("長沙",1,20);
        PageBean pb =  routeService.search(keyword,currentPage,pageSize);

        //響應瀏覽器
        ResponseInfo info = new ResponseInfo();
        info.setCode(200);
        info.setData(pb);
        String json = new ObjectMapper().writeValueAsString(info);
        response.getWriter().println(json);
    }
}

  • 測試//http://localhost:8080/lvyou_war_exploded/routeServlet?keyword=%E5%8C%97%E4%BA%AC&currentPage=1&pageSize=20

MyBatis的多引數傳遞

  • (1)Mybatis 支援 多引數傳遞
  • (2)Dao方法
    List findPage(String cid, int start, int pageSize);
  • (3)對映檔案設定
    可使用引數出現的順序號碼參照引數
  • (4)範例
    第一個引數用arg0或param1表示
    第二個引數用arg1或param2表示
    arg從0開始計數,param從1開始計數