多条件查询与分页:
通过页面的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
<!--插件 分页-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"></property>
</plugin>
</plugins>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.etc.dao.HouseDAO">
<insert id="add">
INSERT INTO `t_house` (
`h_name`,
`h_rect`,
`h_date`,
`h_loc1`,
`h_loc2`,
`h_img`,
`h_userId`
)
VALUES
(
#{hName},
#{hRect},
#{hDate},
#{hLoc1},
#{hLoc2},
#{hImg},
#{userId}
);
</insert> <select id="serach" resultType="house">
SELECT * from `t_house`
<where>
<if test="houseName!=null">
h_name like '%${houseName}%'
</if>
<if test="minRect!=null and maxRect!=null">
and h_rect between #{minRect} and #{maxRect}
</if>
</where> </select> </mapper>
HouseDAO.xml
package com.etc.controller; import com.etc.Vo.HouseSearchVO;
import com.etc.Vo.HouseVO;
import com.etc.common.Constant;
import com.etc.converter.HouseConverter;
import com.etc.entity.House;
import com.etc.entity.HouseSearch;
import com.etc.entity.User;
import com.etc.servise.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List; @Controller
@RequestMapping("/house")
public class HouseController { public static final String UPLOAD_DIR = "D:\\images\\"; @RequestMapping("/images")
public String images(String imgName, HttpServletResponse response) throws IOException {
//将图片的输入流,复制到response的输出流中,
IOUtils.copy(new FileInputStream(UPLOAD_DIR+imgName),response.getOutputStream());
return null;
} @Autowired
private HouseService houseService; @RequestMapping("/add")
public String add(HouseVO houseVO, HttpSession session) throws IOException {
//获取session中的user对象
User user = (User) session.getAttribute("user");
//拿到user对象的id
int userId = user.getUserId();
//设置文件上传的路径
houseVO.getHImg().transferTo(new File(UPLOAD_DIR + houseVO.getHImg().getOriginalFilename()));
//建立house对象
House house = HouseConverter.convert(houseVO, houseVO.getHImg().getOriginalFilename(), userId);
//调用添加方法
houseService.add(house);
return "list";
} @RequestMapping("/serach")
public String serach(@ModelAttribute("vo") HouseSearchVO houseSearchVO, Model model, Integer pageNum) { HouseSearch houseSearch = getHouseSearch(houseSearchVO); if (pageNum == null) {
pageNum = 1;
} PageHelper.startPage(pageNum, Constant.PAGE_SIZE);
List<House> serach = houseService.serach(houseSearch);
PageInfo<House> pageInfo = new PageInfo<>(serach); model.addAttribute("pageInfo", pageInfo);
return "list";
} private HouseSearch getHouseSearch(HouseSearchVO houseSearchVO) {
HouseSearch houseSearch = new HouseSearch();
if (!StringUtils.isEmpty(houseSearchVO.getHouseName())) {
houseSearch.setHouseName(houseSearchVO.getHouseName());
}
if (!StringUtils.isEmpty(houseSearchVO.getFloorage())) {
String[] split = houseSearchVO.getFloorage().split("-");
houseSearch.setMinRect(Double.valueOf(split[0]));
houseSearch.setMaxRect(Double.valueOf(split[1]));
}
return houseSearch;
} }
HouseController.java
package com.etc.servise; import com.etc.entity.HouseSearch;
import com.etc.dao.HouseDAO;
import com.etc.entity.House;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class HouseService { @Autowired
private HouseDAO houseDAO; public void add(House house){
houseDAO.add(house);
} public List<House> serach(HouseSearch houseSearch){
List<House> serach = houseDAO.serach(houseSearch);
return serach;
} }
HouseService.java
package com.etc.dao; import com.etc.entity.HouseSearch;
import com.etc.entity.House; import java.util.List; public interface HouseDAO { void add(House house); List<House> serach(HouseSearch houseSearch);
}
HouseDAO.java
package com.etc.Vo; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@AllArgsConstructor
@NoArgsConstructor
public class HouseSearchVO {
private String houseName;
private String floorage;
}
HouseSearchVO
package com.etc.entity; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@AllArgsConstructor
@NoArgsConstructor
public class HouseSearch {
private String houseName;
private Double minRect;
private Double maxRect; }
HouseSearch
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0030)http://localhost:8080/House-2/ -->
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>布谷租房 - 首页</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<LINK rel=stylesheet type=text/css href="../css/style.css">
<META name=GENERATOR content="MSHTML 8.00.7601.17514"></HEAD>
<BODY>
<DIV id=header class=wrap>
<DIV id=logo><IMG src="../images/logo.gif"></DIV></DIV>
<DIV id=navbar class=wrap>
<DL class="search clearfix">
<FORM id=sform method=post action=/house/serach>
<input type="hidden" id="pageNum" name="pageNum">
<DT>
<UL>
<LI class=bold>房屋信息</LI>
<LI>标题:<INPUT class=text type=text name=houseName value="${vo.houseName}"> <LABEL class=ui-blue>
<INPUT value=搜索房屋 type=submit name=search></LABEL>
</LI></UL></DT>
<DD>
<UL>
<LI class=first>面积 </LI>
<LI><SELECT name=floorage> <OPTION ${vo.floorage==""?"selected":""} selected value="">不限</OPTION> <OPTION
value=0-40 ${vo.floorage=="0-40"?"selected":""}>40以下</OPTION> <OPTION value=40-500 ${vo.floorage=="40-500"?"selected":""}>40-500</OPTION> <OPTION
value=500-1000000 ${vo.floorage=="500-1000000"?"selected":""}>500以上</OPTION></SELECT> </LI></UL></DD>
<DD>
<UL>
<LI class=first>价格 </LI>
<LI><SELECT name=price> <OPTION selected value="">不限</OPTION> <OPTION
value=0-100 >100元以下</OPTION> <OPTION value=100-200 >100元—200元</OPTION>
<OPTION value=200-1000000 >200元以上</OPTION></SELECT> </LI></UL></DD>
<DD>
<UL>
<LI class=first>房屋位置</LI>
<LI><SELECT id=street name=street_id> <OPTION selected
value="">不限</OPTION> <OPTION value=1000>知春路</OPTION> <OPTION
value=1001>中关村大街</OPTION> <OPTION value=1002>学院路</OPTION> <OPTION
value=1003>朝阳路</OPTION></SELECT> </LI></UL></DD>
<DD>
<UL>
<LI class=first>房型</LI>
<LI><SELECT name=type_id> <OPTION selected value="">不限</OPTION> <OPTION
value=1000>一室一厅</OPTION> <OPTION value=1001>一室两厅</OPTION> <OPTION
value=1002>两室一厅</OPTION> <OPTION value=1003>两室两厅</OPTION></SELECT>
</LI></UL></DD>
</FORM></DL></DIV>
<DIV class="main wrap">
<TABLE class=house-list>
<TBODY>
<c:forEach items="${pageInfo.list}" var="house">
<TR>
<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>
<TD>
<DL>
<DT><A href="../../details.jsp" target="_blank">${house.HName}</A></DT>
<DD>${house.HLoc1}${house.HLoc2},${house.HRect}平米<BR>联系方式:3456 </DD></DL></TD>
<TD class=house-type>一室一厅</TD>
<TD class=house-price><SPAN>346.0</SPAN>元/月</TD></TR>
</c:forEach>
</TBODY></TABLE>
<DIV class=pager>
<UL>
<LI class=current><A href="javascript:void(0)" onclick="form(${pageInfo.firstPage})">首页</A></LI>
<LI><A href="javascript:void(0)" onclick="form(${pageInfo.hasPreviousPage?pageInfo.prePage:pageInfo.pageNum})">上一页</A></LI>
<LI><A href="javascript:void(0)" onclick="form(${pageInfo.hasNextPage?pageInfo.nextPage:pageInfo.pageNum})">下一页</A></LI>
<LI><A href="javascript:void(0)" onclick="form(${pageInfo.lastPage})">末页</A></LI></UL><SPAN
class=total>${pageInfo.pageNum}/${pageInfo.pages}页</SPAN> </DIV></DIV>
<DIV id=footer class=wrap>
<DL>
<DT>布谷租房 © 2010 布谷租房 京ICP证1000001号</DT>
<DD>关于我们 · 联系方式 · 意见反馈 · 帮助中心</DD></DL></DIV></BODY><script>
function form(pageNum){
var page=document.getElementById("pageNum");
page.value=pageNum;
var form=document.getElementById("sform");
form.submit();
} </script></HTML>
list.jsp