搜索与分页的后台代码
- (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¤tPage=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开始计数