欢迎来到刘认真的博客

It's not me that's wrong. It's the whole world

SSM整合 mybatis多条件查询与分页

 

多条件查询与分页:

通过页面的houseName、floorage获取值传到前端视图(HouseSearchVO)实体类中的houseName,floorage建立houseSearchVO对象。

通过controller控制层getHouseSearch方法得到houseName,floorage的值 存在哪个就将此值传到后端(HouseSearch)实体类中建立houseSearch对象。

 

    list.jsp:                                               HouseSearchVO:

     

      HouseController:

  

 

此时返回一个houseSearch对象。通过HouseService中的search方法调用HouseDAO中的查询,此时传到HouseDAO.xml调用sql语句

    HouseService:                               HouseDAO:

                                     

 

  HouseDAO.xml:

 分页插件:

 页面显示:

 

 

 

升级版:

将页面查询时添加的条件 用HouseSearchVO对象保存,再将此对象保存到@ModelAttribute(“vo”)vo对象中返回给页面。

此时,页面就能拿到vo对象,然后通过vo对象的属性来将条件重新显示

最后将下一页修改成点击事件,将页码和表单一起提交。

HouseController

 

 jsp:

 

 

源码:

 mybatis-config.xml

1 <!--插件 分页-->
2     <plugins>
3         <plugin interceptor="com.github.pagehelper.PageHelper">
4             <property name="dialect" value="mysql"></property>
5         </plugin>
6     </plugins>

 

 

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.etc.dao.HouseDAO">
 4     <insert id="add">
 5  INSERT INTO `t_house` (
 6     `h_name`,
 7     `h_rect`,
 8     `h_date`,
 9     `h_loc1`,
10     `h_loc2`,
11     `h_img`,
12     `h_userId`
13 )
14 VALUES
15     (
16         #{hName},
17         #{hRect},
18         #{hDate},
19         #{hLoc1},
20         #{hLoc2},
21         #{hImg},
22         #{userId}
23     );
24 </insert>
25 
26     <select id="serach" resultType="house">
27         SELECT * from `t_house`
28         <where>
29             <if test="houseName!=null">
30                 h_name like '%${houseName}%'
31             </if>
32             <if test="minRect!=null and maxRect!=null">
33                 and h_rect between #{minRect} and #{maxRect}
34             </if>
35         </where>
36 
37     </select>
38 
39 </mapper>
HouseDAO.xml

 

 1 package com.etc.controller;
 2 
 3 import com.etc.Vo.HouseSearchVO;
 4 import com.etc.Vo.HouseVO;
 5 import com.etc.common.Constant;
 6 import com.etc.converter.HouseConverter;
 7 import com.etc.entity.House;
 8 import com.etc.entity.HouseSearch;
 9 import com.etc.entity.User;
10 import com.etc.servise.HouseService;
11 import com.github.pagehelper.PageHelper;
12 import com.github.pagehelper.PageInfo;
13 import org.apache.commons.io.IOUtils;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Controller;
16 import org.springframework.ui.Model;
17 import org.springframework.util.StringUtils;
18 import org.springframework.web.bind.annotation.ModelAttribute;
19 import org.springframework.web.bind.annotation.RequestMapping;
20 
21 import javax.servlet.http.HttpServletResponse;
22 import javax.servlet.http.HttpSession;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.util.List;
27 
28 @Controller
29 @RequestMapping("/house")
30 public class HouseController {
31 
32     public static final String UPLOAD_DIR = "D:\\images\\";
33 
34     @RequestMapping("/images")
35     public String images(String imgName, HttpServletResponse response) throws IOException {
36         //将图片的输入流,复制到response的输出流中,
37         IOUtils.copy(new FileInputStream(UPLOAD_DIR+imgName),response.getOutputStream());
38         return null;
39     }
40 
41 
42     @Autowired
43     private HouseService houseService;
44 
45     @RequestMapping("/add")
46     public String add(HouseVO houseVO, HttpSession session) throws IOException {
47         //获取session中的user对象
48         User user = (User) session.getAttribute("user");
49         //拿到user对象的id
50         int userId = user.getUserId();
51         //设置文件上传的路径
52         houseVO.getHImg().transferTo(new File(UPLOAD_DIR + houseVO.getHImg().getOriginalFilename()));
53         //建立house对象
54         House house = HouseConverter.convert(houseVO, houseVO.getHImg().getOriginalFilename(), userId);
55         //调用添加方法
56         houseService.add(house);
57         return "list";
58     }
59 
60     @RequestMapping("/serach")
61     public String serach(@ModelAttribute("vo") HouseSearchVO houseSearchVO, Model model, Integer pageNum) {
62 
63         HouseSearch houseSearch = getHouseSearch(houseSearchVO);
64 
65         if (pageNum == null) {
66             pageNum = 1;
67         }
68 
69         PageHelper.startPage(pageNum, Constant.PAGE_SIZE);
70         List<House> serach = houseService.serach(houseSearch);
71         PageInfo<House> pageInfo = new PageInfo<>(serach);
72 
73         model.addAttribute("pageInfo", pageInfo);
74         return "list";
75     }
76 
77     private HouseSearch getHouseSearch(HouseSearchVO houseSearchVO) {
78         HouseSearch houseSearch = new HouseSearch();
79         if (!StringUtils.isEmpty(houseSearchVO.getHouseName())) {
80             houseSearch.setHouseName(houseSearchVO.getHouseName());
81         }
82         if (!StringUtils.isEmpty(houseSearchVO.getFloorage())) {
83             String[] split = houseSearchVO.getFloorage().split("-");
84             houseSearch.setMinRect(Double.valueOf(split[0]));
85             houseSearch.setMaxRect(Double.valueOf(split[1]));
86         }
87         return houseSearch;
88     }
89 
90 }
HouseController.java

 

 1 package com.etc.servise;
 2 
 3 import com.etc.entity.HouseSearch;
 4 import com.etc.dao.HouseDAO;
 5 import com.etc.entity.House;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 import java.util.List;
10 
11 @Service
12 public class HouseService {
13 
14     @Autowired
15     private HouseDAO houseDAO;
16 
17     public void add(House house){
18         houseDAO.add(house);
19     }
20 
21     public List<House> serach(HouseSearch houseSearch){
22         List<House> serach = houseDAO.serach(houseSearch);
23         return serach;
24     }
25 
26 }
HouseService.java

 

 1 package com.etc.dao;
 2 
 3 import com.etc.entity.HouseSearch;
 4 import com.etc.entity.House;
 5 
 6 import java.util.List;
 7 
 8 public interface HouseDAO {
 9 
10     void add(House house);
11 
12     List<House> serach(HouseSearch houseSearch);
13 }
HouseDAO.java

 

 1 package com.etc.Vo;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 @Data
 8 @AllArgsConstructor
 9 @NoArgsConstructor
10 public class HouseSearchVO {
11     private String houseName;
12     private String floorage;
13 }
HouseSearchVO

 

 1 package com.etc.entity;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 @Data
 8 @AllArgsConstructor
 9 @NoArgsConstructor
10 public class HouseSearch {
11     private String houseName;
12     private Double minRect;
13     private Double maxRect;
14 
15 }
HouseSearch

 

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
 3 <!-- saved from url=(0030)http://localhost:8080/House-2/ -->
 4 <HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>布谷租房 - 首页</TITLE>
 5 <META content="text/html; charset=utf-8" http-equiv=Content-Type>
 6 <LINK rel=stylesheet type=text/css href="../css/style.css">
 7 <META name=GENERATOR content="MSHTML 8.00.7601.17514"></HEAD>
 8 <BODY>
 9 <DIV id=header class=wrap>
10 <DIV id=logo><IMG src="../images/logo.gif"></DIV></DIV>
11 <DIV id=navbar class=wrap>
12 <DL class="search clearfix">
13   <FORM id=sform method=post action=/house/serach>
14       <input type="hidden" id="pageNum" name="pageNum">
15   <DT>
16   <UL>
17     <LI class=bold>房屋信息</LI>
18     <LI>标题:<INPUT class=text type=text name=houseName value="${vo.houseName}"> <LABEL class=ui-blue>
19               <INPUT value=搜索房屋 type=submit name=search></LABEL>
20     </LI></UL></DT>
21     <DD>
22       <UL>
23         <LI class=first>面积 </LI>
24         <LI><SELECT name=floorage> <OPTION ${vo.floorage==""?"selected":""} selected value="">不限</OPTION> <OPTION
25                 value=0-40 ${vo.floorage=="0-40"?"selected":""}>40以下</OPTION> <OPTION value=40-500 ${vo.floorage=="40-500"?"selected":""}>40-500</OPTION> <OPTION
26                 value=500-1000000 ${vo.floorage=="500-1000000"?"selected":""}>500以上</OPTION></SELECT> </LI></UL></DD>
27   <DD>
28   <UL>
29     <LI class=first>价格 </LI>
30     <LI><SELECT name=price> <OPTION  selected value="">不限</OPTION> <OPTION
31       value=0-100 >100元以下</OPTION> <OPTION value=100-200 >100元—200元</OPTION>
32       <OPTION value=200-1000000 >200元以上</OPTION></SELECT> </LI></UL></DD>
33   <DD>
34   <UL>
35     <LI class=first>房屋位置</LI>
36     <LI><SELECT id=street name=street_id> <OPTION selected 
37       value="">不限</OPTION> <OPTION value=1000>知春路</OPTION> <OPTION 
38       value=1001>中关村大街</OPTION> <OPTION value=1002>学院路</OPTION> <OPTION 
39       value=1003>朝阳路</OPTION></SELECT> </LI></UL></DD>
40   <DD>
41   <UL>
42     <LI class=first>房型</LI>
43     <LI><SELECT name=type_id> <OPTION selected value="">不限</OPTION> <OPTION 
44       value=1000>一室一厅</OPTION> <OPTION value=1001>一室两厅</OPTION> <OPTION 
45       value=1002>两室一厅</OPTION> <OPTION value=1003>两室两厅</OPTION></SELECT> 
46   </LI></UL></DD>
47 </FORM></DL></DIV>
48 <DIV class="main wrap">
49   <TABLE class=house-list>
50     <TBODY>
51     <c:forEach items="${pageInfo.list}" var="house">
52       <TR>
53         <TD class=house-thumb><span><A href="../../details.jsp" target="_blank"><img src="/house/images?imgName=${house.HImg}" width="100" height="75" alt=""></a></span></TD>
54         <TD>
55           <DL>
56             <DT><A href="../../details.jsp" target="_blank">${house.HName}</A></DT>
57             <DD>${house.HLoc1}${house.HLoc2},${house.HRect}平米<BR>联系方式:3456 </DD></DL></TD>
58         <TD class=house-type>一室一厅</TD>
59         <TD class=house-price><SPAN>346.0</SPAN>元/月</TD></TR>
60     </c:forEach>
61     </TBODY></TABLE>
62   <DIV class=pager>
63     <UL>
64       <LI class=current><A href="javascript:void(0)" onclick="form(${pageInfo.firstPage})">首页</A></LI>
65       <LI><A href="javascript:void(0)" onclick="form(${pageInfo.hasPreviousPage?pageInfo.prePage:pageInfo.pageNum})">上一页</A></LI>
66       <LI><A href="javascript:void(0)" onclick="form(${pageInfo.hasNextPage?pageInfo.nextPage:pageInfo.pageNum})">下一页</A></LI>
67       <LI><A href="javascript:void(0)" onclick="form(${pageInfo.lastPage})">末页</A></LI></UL><SPAN
68           class=total>${pageInfo.pageNum}/${pageInfo.pages}页</SPAN> </DIV></DIV>
69 <DIV id=footer class=wrap>
70 <DL>
71   <DT>布谷租房 © 2010 布谷租房 京ICP证1000001号</DT>
72   <DD>关于我们 · 联系方式 · 意见反馈 · 帮助中心</DD></DL></DIV></BODY><script>
73      function form(pageNum){
74          var page=document.getElementById("pageNum");
75          page.value=pageNum;
76          var form=document.getElementById("sform");
77          form.submit();
78     }
79 
80 </script></HTML>
list.jsp

 

posted @ 2019-07-27 14:42  刘认真  阅读(3642)  评论(0编辑  收藏  举报