dao
package com.bw.dao;
import com.bw.pojo.THouse;
import com.bw.pojo.TPerson;
import com.bw.pojo.TUser;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author zhj
* @create 2017/10/26 8:24
*/
public interface IUserDao {
List<TUser> UserLogin(TUser User);
List<THouse> findAllHouseAndByhouseName(THouse house);
List<TPerson> findperson();
void addHouse(THouse house);
THouse findHouseById(THouse house);
void updateHouseById(THouse house);
void delHouse(@Param("hid") int hid);
serviceimpl
import com.alibaba.fastjson.JSON;
import com.bw.mapper.UserMapper;
import com.bw.pojo.THouse;
import com.bw.pojo.TPerson;
import com.bw.pojo.TUser;
import com.bw.util.jedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<TUser> UserLogin(TUser user) {
return userMapper.UserLogin(user);
}
@Override
public PageInfo<THouse> findAllHouseAndByhouseName(THouse house,String pageNo, String pageSize) {
int num = 1;
int size = 3;
if(pageNo != null && !"".equals(pageNo)) {
num = Integer.parseInt(pageNo);
}
if (pageSize != null && !"".equals(pageSize)) {
size = Integer.parseInt(pageSize);
}
/**
* 开始分页
*
* @param pageNum 页码
* @param pageSize 每页显示数量
* @param count 是否进行count查询
* @param reasonable 分页合理化,null时用默认配置
* @param pageSizeZero true且pageSize=0时返回全部结果,false时分页,null时用默认配置
*/
//开始分页
PageHelper.startPage(num,size);
//查询数据库信息
List<THouse> houseAll = userMapper.findAllHouseAndByhouseName(house);
System.out.println(houseAll);
//将信息放入PageInfo进行分页
PageInfo<THouse> pageInfo =new PageInfo<THouse>(houseAll);
System.out.println(pageInfo);
System.out.println(pageInfo.getPageNum());
System.out.println(pageInfo.getPageSize());
System.out.println(pageInfo.getTotal());
System.out.println(pageInfo.getFirstPage());
System.out.println(pageInfo.getLastPage());
System.out.println(pageInfo.getPrePage());
System.out.println(pageInfo.getNextPage());
return pageInfo;
}
@Override
public List<TPerson> findPerson() {
List<TPerson> list = null;
String str = jedisUtil.get("shouList");
if(str==null){
list = userMapper.findPerson();
String json = (String) JSON.toJSONString(list);
jedisUtil.set("shouList", json, 10000);
}else{
list = (List<TPerson>) JSON.parse(str);
}
return list;
}
@Override
public void addHouse(THouse house) {
userMapper.addHouse(house
);
}
@Override
public THouse findHouseById(THouse house) {
return userMapper.findHouseById(house);
}
@Override
public void updateHouseById(THouse house) {
userMapper.updateHouseById(house);
}
@Override
public void delHouse(int hid) {
userMapper.delHouse(hid);
}
}
controller
import com.bw.pojo.THouse;
import com.bw.pojo.TPerson;
import com.bw.pojo.TUser;
import com.bw.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Controller
@RequestMapping("HouseController")
public class HouseController {
@Autowired
private UserService userService;
/**
查看所有
*/
@RequestMapping("findAllHouseAndByhouseName")
public String findAllHouseAndByhouseName(THouse house, String pageNo, String pageSize, Model model){
PageInfo<THouse> allHouseAndByhouseName = userService.findAllHouseAndByhouseName(house, pageNo, pageSize);
List<TPerson> person = userService.findPerson();
model.addAttribute("person",person);
model.addAttribute("all",allHouseAndByhouseName);
return "all";
}
/**
添加方法
*/
@RequestMapping(value = "addHouse" ,method = RequestMethod.POST)
public String addHouse(THouse house, MultipartFile multipartFile, HttpServletRequest request) throws IOException {
System.out.println(multipartFile + "-------------------------------");
//获取文件名字
String originalFilename = multipartFile.getOriginalFilename();
System.out.println(originalFilename + "++++++++++++++++++++");
//获取不会重复的毫秒数
long l = System.currentTimeMillis();
//新名字
String newName = l + originalFilename;
System.out.println(newName + "----------++++++");
//图片的输入流名字
InputStream inputStream = multipartFile.getInputStream();
//存在于项目中的路径
String wlPath = "E:\\小实训2\\lianxi5\\web\\image/" + newName;
//逻辑路径,存到数据库中
String ljPath = "/image/" + newName;
//临时路径
String realPath = request.getSession().getServletContext().getRealPath("/");
String lsPath = realPath + "//image//" + newName;
//如果没有那个文件夹就创建
File wlFile = new File("E:\\小实训2\\lianxi5\\web\\image");
if (!wlFile.exists()) {
wlFile.mkdir();
}
File isFile = new File(realPath + "\\image\\");
if (!isFile.exists()) {
isFile.mkdir();
}
//不为空时
if (!multipartFile.isEmpty()) {
//输出流 写物理路径
FileOutputStream wlStream = new FileOutputStream(wlPath);
//输出流 写临时路径
FileOutputStream lsStream = new FileOutputStream(lsPath);
int len = 0;
while ((len = inputStream.read()) != -1) {
wlStream.write(len);//写入
lsStream.write(len);
}
wlStream.flush();
lsStream.flush();
wlStream.close();
lsStream.close();
inputStream.close();
}
System.out.println(wlPath);
System.out.println(ljPath);
System.out.println(lsPath);
house.setHouseImg(ljPath);
userService.addHouse(house);
return "redirect:/HouseController/findAllHouseAndByhouseName.action";
}
//去添加页面
@RequestMapping("toadd")
public String toadd(Model model){
List<TPerson> person = userService.findPerson();
model.addAttribute("person",person);
return "add";
}
/**
去修改页面
*/
@RequestMapping("toupdate")
public String toupdate(THouse house,Model model){
THouse houseById = userService.findHouseById(house);
List<TPerson> person = userService.findPerson();
model.addAttribute("houseById",houseById);
model.addAttribute("person",person);
return "update";
} @RequestMapping(value = "updateHouseById" ,method = RequestMethod.POST)
public String updateHouseById(THouse house, MultipartFile multipartFile, HttpServletRequest request) throws IOException {
System.out.println(multipartFile + "-------------------------------");
//获取文件名字
String originalFilename = multipartFile.getOriginalFilename();
System.out.println(originalFilename + "++++++++++++++++++++");
//获取不会重复的毫秒数
long l = System.currentTimeMillis();
//新名字
String newName = l + originalFilename;
System.out.println(newName + "----------++++++");
//图片的输入流名字
InputStream inputStream = multipartFile.getInputStream();
//存在于项目中的路径
String wlPath = "E:\\小实训2\\lianxi5\\web\\image/" + newName;
//逻辑路径,存到数据库中
String ljPath = "/image/" + newName;
//临时路径
String realPath = request.getSession().getServletContext().getRealPath("/");
String lsPath = realPath + "//image//" + newName;
//如果没有那个文件夹就创建
File wlFile = new File("E:\\小实训2\\lianxi5\\web\\image");
if (!wlFile.exists()) {
wlFile.mkdir();
}
File isFile = new File(realPath + "\\image\\");
if (!isFile.exists()) {
isFile.mkdir();
}
//不为空时
if (!multipartFile.isEmpty()) {
//输出流 写物理路径
FileOutputStream wlStream = new FileOutputStream(wlPath);
//输出流 写临时路径
FileOutputStream lsStream = new FileOutputStream(lsPath);
int len = 0;
while ((len = inputStream.read()) != -1) {
wlStream.write(len);//写入
lsStream.write(len);
}
wlStream.flush();
lsStream.flush();
wlStream.close();
lsStream.close();
inputStream.close();
}
System.out.println(wlPath);
System.out.println(ljPath);
System.out.println(lsPath);
house.setHouseImg(ljPath);
userService.updateHouseById(house);
return "redirect:/HouseController/findAllHouseAndByhouseName.action";
}
/**
删除方法
*/
@RequestMapping("delHouse")
@ResponseBody
public String delHouse(String ids){
System.out.println("进来了");
String[] split = ids.split(",");//因为传过来的ids以(1,2,3...)这种形式 所以要分割
for (String s:split
) {
int i =Integer.parseInt(s);
userService.delHouse(i);
}
System.out.println("走完了");
return "ok";
}
}
usercontroller
import com.bw.pojo.TUser;
import com.bw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("UserController")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("UserLogin")
@ResponseBody
public String UserLogin(TUser user){
List<TUser> tUsers = userService.UserLogin(user);
if(tUsers.size()>0){
return "ok";
}
else{
return "no";
}
}
}
sql
<?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.bw.mapper.UserMapper">
<resultMap type="THouse" id="houseMap">
<id column="hid" property="hid"/>
<result column="house_name" property="houseName"/>
<result column="house_img" property="houseImg"/>
<result column="house_area" property="houseArea"/>
<result column="house_price" property="housePrice"/>
<result column="total_count" property="totalCount"/>
<result column="buy_date" property="buyDate"/>
<association property="person" javaType="TPerson">
<id column="id" property="id"/>
<result column="person_name" property="personName"/>
</association>
</resultMap >
<!-- 查询sql-->
<select id="findAllHouseAndByhouseName" resultMap="houseMap" parameterType="thouse">
select *from t_person p,t_house h where p.pid=h.person_id
<if test="houseName!=null">
and house_name LIKE '%${houseName}%'
</if>
<if test="pid!=null and pid!=''">
AND pid=#{pid}
</if>
</select>
<!-- 登录sql-->
<select id="UserLogin" parameterType="tuser" resultType="tuser">
select *from t_user WHERE user_name=#{userName} and user_pwd=#{userPwd}
</select>
<!-- 查询房主sql-->
<select id="findPerson" resultType="tperson">
SELECT * FROM `t_person`;
</select>
<!-- 添加sql-->
<insert id="addHouse" parameterType="thouse">
INSERT INTO t_house(house_name,house_img,house_area,house_price,total_count,buy_date,person_id)
VALUES (
#{houseName},#{houseImg},#{houseArea},#{housePrice},#{houseArea}*#{housePrice},NOW(),#{pid}
)
</insert>
<!-- 修改之前根据id查询房子信息-->
<select id="findHouseById" resultType="thouse" parameterType="thouse">
select *from t_house WHERE hid=#{hid}
</select>
<!-- 根据id修改-->
<update id="updateHouseById" parameterType="thouse">
UPDATE t_house SET house_name=#{houseName},house_img=#{houseImg},house_area=#{houseArea},house_price=#{housePrice},total_count=#{houseArea}*#{housePrice},person_id=#{pid} WHERE hid=#{hid}
</update>
<!-- 删除sql-->
<delete id="delHouse" parameterType="int">
DELETE from t_house WHERE hid=#{hid}
</delete>
<!-- <select id="findStockAll" resultType="stock" parameterType="String">
select *from t_stock
<if test="stockName!=null">
WHERE stock_name LIKE '%${stockName}%'
</if>
</select>
<update id="outBound" parameterType="int">
UPDATE t_stock SET stock_update_date=NOW() WHERE id =#{ids}
</update>
<insert id="addStock" parameterType="stock">
INSERT INTO t_stock(stock_name,stock_num,stock_from,stock_photo,stock_create_date,stock_update_date)
VALUES (#{stockName},
#{stockNum},
#{stockFrom},
#{stockPhoto},
NOW(),
#{stockUpdateDate} )
</insert>
-->
</mapper>
jsp add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page contentType="text/html; utf-8" %> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'fs.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <!-- 新 Bootstrap 核心 CSS 文件 --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- 可选的Bootstrap主题文件(一般不使用) --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"></script> <!-- jQuery文件。务必在bootstrap.min.js 之前引入 --> <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <form action="/HouseController/addHouse.action" method="post" enctype="multipart/form-data"> <input type="file" name="multipartFile"/><br/> <input class="form-control input-lg" type="text" placeholder="房屋名称" name="housename"> <input class="form-control input-lg" type="text" placeholder="房屋面积" name="housearea"> <input class="form-control input-lg" type="text" placeholder="房屋单价" name="houseprice"> <select name="pid"> <c:forEach items="${person}" var="person"> <option value="${person.pid}"> ${person.personname} </option> </c:forEach> </select> <input type="submit" class="btn btn-primary btn-lg"> </form> </body> </html>
jsp all.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page contentType="text/html; utf-8" %> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'fs.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <!-- 新 Bootstrap 核心 CSS 文件 --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- 可选的Bootstrap主题文件(一般不使用) --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"></script> <!-- jQuery文件。务必在bootstrap.min.js 之前引入 --> <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script type="text/javascript"> $(function () { $("#todel").click(function () { var con=confirm("确认删除?"); if(con==true){ var arr=[]; var arr2=$(":checked"); $.each(arr2,function () { arr.push($(this).val()); }) alert(arr) $.ajax({ url : "HouseController/delHouse.action", type : "GET", data : {"ids" : arr.join(",") }, dataType : "text", success : function(data){ if(data=="ok"){ alert("删除成功") location.href="HouseController/findAllHouseAndByhouseName.action"; } else{ alert("删除失败"); } }, error : function(){ alert("至少选择一个"); } }); } else{ return false; } }) }) function mm(o) { var a=document.getElementsByName("id"); for (var i=0;i<a.length;i++){ a[i].checked = o.checked; } } </script> </head> <body > <div > <div align="center"> <h1>库存信息展示</h1> <form action="/HouseController/findAllHouseAndByhouseName.action"> <input type="text" style="width: 200px; height: 20px;" name="housename" > <input type="submit" value="搜索"> </form> </div><div class="col-md-offset-5"> <h3>全选</h3><input type="checkbox" id="out" style="width:30px;height:30px;"value="0" οnclick="mm(this)"> <div class="dropup"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Dropup <span class="caret"></span> </button> <ul class="dropdown-menu" aria-labelledby="dropdownMenu2"> <li><a href="HouseController/findAllHouseAndByhouseName.action">查看所有</a></li> <c:forEach items="${person}" var="person"> <li><a href="HouseController/findAllHouseAndByhouseName.action?pid=${person.pid}">${person.personame}</a></li> </c:forEach> </ul> </div> </div> <br> <br><br> <c:forEach items="${all.list}" var="a"> <div class="col-md-3 " style="margin-right: 0px"> <input type="hidden" class="id"> <img src="${a.houseimg}" style="width: 300px;height: 200px;"><br> 购买日期: ${a.buydate} <br> <h4>房屋主人:${a.person.personname};</h4> <br><div class="col-md-offset-4"> <input type="checkbox" name="id" style="width:30px;height:30px;"value="${a.hid}" > </div> </div> <div class="col-md-1" style="margin: 0px"> 房屋名称:${a.housename}<br> 房屋面积:${a.housearea}<br> 房屋单价:${a.houseprice}元/每平米<br> 房屋总价:${a.totalcount}元<br> <a class="btn btn-success" href="/HouseController/toupdate.action?hid=${a.hid}" role="button">修改房屋信息</a> </div> </c:forEach> </div> <br><br> <div class="col-md-8 col-md-offset-4 " > <br><br><br> <a class="btn btn-primary" href="/HouseController/toadd.action" role="button">添加房屋信息</a> <button class="btn btn-danger" id="todel" >删除选中房屋</button> <br><br><br> 共<span>${all.total}条记录当前显示</span><span>现在显示第${all.pageNum}页</span> <a href="/HouseController/findAllHouseAndByhouseName.action?pageNo=${all.firstPage}&pageSize=${all.pageSize}" >首页</a> <c:choose> <c:when test="${all.isFirstPage==true}"> <a href="/HouseController/findAllHouseAndByhouseName.action?pageNo=${all.firstPage}&pageSize=${all.pageSize}" >上一页</a> </c:when> <c:otherwise> <a href="/HouseController/findAllHouseAndByhouseName.action?pageNo=${all.prePage}&pageSize=${all.pageSize}" >上一页</a> </c:otherwise> </c:choose> <c:choose> <c:when test="${all.isLastPage==true}"> <a href="/HouseController/findAllHouseAndByhouseName.action?pageNo=${all.lastPage}&pageSize=${all.pageSize}" >下一页</a> </c:when> <c:otherwise> <a href="/HouseController/findAllHouseAndByhouseName.action?pageNo=${all.nextPage}&pageSize=${all.pageSize}" >下一页</a> </c:otherwise> </c:choose> <a href="/HouseController/findAllHouseAndByhouseName.action?pageNo=${all.lastPage}&pageSize=${all.pageSize}">尾页</a> </div> </body> </html>
jsp update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page contentType="text/html; utf-8" %> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'fs.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <!-- 新 Bootstrap 核心 CSS 文件 --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- 可选的Bootstrap主题文件(一般不使用) --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"></script> <!-- jQuery文件。务必在bootstrap.min.js 之前引入 --> <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <form action="/HouseController/updateHouseById.action" method="post" enctype="multipart/form-data"> <input type="file" name="multipartFile"/><br/> <input type="hidden" name="hid"value="${houseById.hid}"> <input class="form-control input-lg" type="text" placeholder="房屋名称" name="housename" value="${houseById.housename}"> <input class="form-control input-lg" type="text" placeholder="房屋面积" name="housearea" value="${houseById.housearea}"> <input class="form-control input-lg" type="text" placeholder="房屋单价" name="houseprice" value="${houseById.houseprice}"> <select name="pid"> <c:forEach items="${person}" var="person"> <option value="${person.pid}"> ${person.personname} </option> </c:forEach> </select> <input type="submit" class="btn btn-primary btn-lg"> </form> </body> </html>