SSM整合多对多表关系
sql语句
1 <mapper namespace="com.bw.dao.HouDao"> 2 <select id="list" resultMap="houseMap"> 3 select * from house 4 5 <trim prefix="where" prefixOverrides="and|or"> 6 <if test="h_address!=null and h_address!=''"> and h_address like concat('%',#{h_address},'%')</if> 7 </trim> 8 </select> 9 10 <resultMap type="House" id="houseMap"> 11 <id property="h_id" column="h_id"/> 12 <result property="h_address" column="h_address"/> 13 <result property="h_use" column="h_use"/> 14 <result property="h_area" column="h_area"/> 15 <result property="h_pic" column="h_pic"/> 16 <collection property="owner" 17 18 select="com.bw.dao.HouDao.findHid" 19 column="h_id" 20 > 21 </collection> 22 </resultMap> 23 24 <select id="findHid" resultType="Owner"> 25 select * from owner join h_o on owner.o_id=h_o.oid 26 27 where h_o.hid=#{hid} 28 </select> 29 30 <select id="ownerAll" resultType="Owner"> 31 select * from owner 32 </select> 33 34 <insert id="add" useGeneratedKeys="true" keyProperty="h_id"> 35 insert into house values(null,#{h_address},#{h_use},#{h_area},#{h_pic}) 36 </insert> 37 38 <insert id="addHO"> 39 insert into h_o values 40 <foreach collection="oids" item="oids" separator=","> 41 (#{h_id},#{oids}) 42 </foreach> 43 </insert> 44 45 <select id="selectOne" resultMap="houseMap"> 46 select * from house where h_id=#{h_id} 47 </select> 48 49 <update id="update"> 50 update house set h_address=#{h_address},h_use=#{h_use},h_area=#{h_area},h_pic=#{h_pic} where h_id=#{h_id} 51 </update> 52 53 <delete id="delHO"> 54 delete from h_o where hid=#{h_id} 55 </delete> 56 57 <select id="login" resultType="java.lang.Integer"> 58 select count(*) from sysuser where user_name=#{username} and user_password=#{password} 59 </select> 60 </mapper>
dao层
1 public interface HouDao { 2 3 List<House> list(Condition con); 4 5 List<Owner> findHid(Integer hid); 6 7 List<Owner> ownerAll(); 8 9 int add(House hou); 10 11 int addHO(@t("h_id")Integer h_id, @Param("oids")int[] oids); 12 13 House selectOne(Integer h_id); 14 15 int update(House hou); 16 17 int delHO(@Param("h_id")Integer h_id, @Param("oids")int[] oids); 18 19 Integer login(@Param("username")String username, @Param("password")String password); 20 21 }
server层
1 @Service("service") 2 public class HouServiceImpl implements HouService{ 3 @Autowired 4 private HouDao dao; 5 6 @Override 7 public List<House> list(Condition con) { 8 // TODO Auto-generated method stub 9 return dao.list(con); 10 } 11 12 @Override 13 public List<Owner> ownerAll() { 14 // TODO Auto-generated method stub 15 return dao.ownerAll(); 16 } 17 18 @Override 19 public void add(House hou) { 20 int i=dao.add(hou); 21 if (i>0) { 22 i=dao.addHO(hou.getH_id(),hou.getOids()); 23 } 24 } 25 26 @Override 27 public House selectOne(Integer h_id) { 28 // TODO Auto-generated method stub 29 return dao.selectOne(h_id); 30 } 31 32 @Override 33 public void update(House hou) { 34 int i=dao.update(hou); 35 if (i>0) { 36 i=dao.delHO(hou.getH_id(),hou.getOids()); 37 if (i>0) { 38 i=dao.addHO(hou.getH_id(), hou.getOids()); 39 } 40 } 41 } 42 43 @Override 44 public boolean login(String username, String password) { 45 // TODO Auto-generated method stub 46 return dao.login(username,password)>0; 47 } 48 }
controller层
1 @Controller 2 public class HouController { 3 @Autowired 4 private HouService service; 5 6 @RequestMapping("list.do") 7 public String list(Model m,Condition con) { 8 if (con.getPageNum()==null) { 9 con.setPageNum(1); 10 } 11 PageHelper.startPage(con.getPageNum(), 3); 12 List<House> list=service.list(con); 13 PageInfo<House> page=new PageInfo<House>(list); 14 15 m.addAttribute("page", page); 16 m.addAttribute("list", list); 17 m.addAttribute("con", con); 18 return "list"; 19 20 } 21 22 @ResponseBody 23 @RequestMapping("ownerAll.do") 24 public Object ownerAll() { 25 return service.ownerAll(); 26 27 } 28 29 @RequestMapping("add.do") 30 public String add(House hou,HttpServletRequest request,MultipartFile myFile) throws IllegalStateException, IOException { 31 String realName = myFile.getOriginalFilename(); 32 33 hou.setH_pic(realName); 34 35 String endName = realName.substring(realName.lastIndexOf(".")); 36 37 String startName = UUID.randomUUID().toString(); 38 39 String realPath = request.getServletContext().getRealPath("/load/"); 40 41 File file=new File(realPath+startName+endName); 42 43 myFile.transferTo(file); 44 45 hou.setH_pic(startName+endName); 46 47 service.add(hou); 48 49 return "redirect:list.do"; 50 51 } 52 53 @ResponseBody 54 @RequestMapping("selectOne.do") 55 public Object selectOne(Integer h_id) { 56 return service.selectOne(h_id); 57 58 } 59 60 @RequestMapping("update.do") 61 public String update(House hou,HttpServletRequest request,MultipartFile myFile) throws IllegalStateException, IOException { 62 String realName = myFile.getOriginalFilename(); 63 64 hou.setH_pic(realName); 65 66 String endName = realName.substring(realName.lastIndexOf(".")); 67 68 String startName = UUID.randomUUID().toString(); 69 70 String realPath = request.getServletContext().getRealPath("/load/"); 71 72 File file=new File(realPath+startName+endName); 73 74 myFile.transferTo(file); 75 76 hou.setH_pic(startName+endName); 77 78 service.update(hou); 79 80 return "redirect:list.do"; 81 82 } 83 84 @RequestMapping("login.do") 85 public String login(String username,String password,Model m,HttpSession session) { 86 boolean flag=service.login(username,password); 87 88 String path=""; 89 if (flag) { 90 session.setAttribute("username", username); 91 return "redirect:list.do"; 92 }else { 93 m.addAttribute("msg", "密码或账号错误请重新登录"); 94 path="login"; 95 } 96 97 98 return path; 99 100 } 101 }
页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 <link rel="stylesheet" href="css/index3.css"> 10 <script type="text/javascript" src="js/jquery-1.8.3.js"></script> 11 <script type="text/javascript"> 12 function fenye(pageNum) { 13 $("[name=pageNum]").val(pageNum); 14 $("form").submit(); 15 } 16 function add() { 17 location="add.jsp"; 18 } 19 function upd(h_id) { 20 location="update.jsp?h_id="+h_id; 21 } 22 </script> 23 </head> 24 <body> 25 <form action="list.do" method="post"> 26 <input type="hidden" name="pageNum"> 27 房产坐落地址:<input type="text" name="h_address" value="${con.h_address}"> 28 房主姓名:<input type="text" name="o_name" value="${con.o_name}"> 29 <button>查询</button> 30 </form> 31 <table> 32 <tr> 33 <th>房产编号</th> 34 <th>房主姓名</th> 35 <th>坐落地址</th> 36 <th>用途</th> 37 <th>面积</th> 38 <th>不动资产证书</th> 39 <th>操作 40 <input type="button" value="添加" onclick="add()"> 41 </th> 42 </tr> 43 <c:forEach items="${list}" var="h" varStatus="count"> 44 <tr> 45 <th>${count.count+page.startRow-1}</th> 46 <th> 47 <c:forEach items="${h.owner}" var="o"> 48 ${o.o_name} 49 </c:forEach> 50 </th> 51 <th>${h.h_address}</th> 52 <th>${h.h_use}</th> 53 <th>${h.h_area}</th> 54 <th> 55 <img alt="" src="load/${h.h_pic}" style="width: 100px; height: 100px;"> 56 </th> 57 <th> 58 <input type="button" value="修改" onclick="upd(${h.h_id})"> 59 </th> 60 </tr> 61 </c:forEach> 62 <tr> 63 <th colspan="10"> 64 <button onclick="fenye(1)">首页</button> 65 <button onclick="fenye(${page.prePage==0?'1':page.prePage})">上一页</button> 66 <button onclick="fenye(${page.nextPage==0?page.pages:page.nextPage})">下一页</button> 67 <button onclick="fenye(${page.pages})">尾页</button> 68 当前是${page.pageNum}/${page.pages}页,一共${page.total}页 69 </th> 70 </tr> 71 </table> 72 </body> 73 </html>
添加
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <link rel="stylesheet" href="css/index3.css"> 9 <script type="text/javascript" src="js/jquery-1.8.3.js"></script> 10 <script type="text/javascript"> 11 $(function() { 12 $.post("ownerAll.do",function(arr){ 13 for ( var i in arr) { 14 var s=arr[i]; 15 var op="<input type='checkbox' name='oids' value='"+s.o_id+"'>"+s.o_name+""; 16 $("#hz").append(op); 17 } 18 },"json") 19 }) 20 </script> 21 </head> 22 <body> 23 <form action="add.do" method="post" enctype="multipart/form-data"> 24 <table> 25 <tr> 26 <th>房产坐落地址</th> 27 <th> 28 <input type="text" name="h_address"> 29 </th> 30 </tr> 31 32 <tr> 33 <th>房产用途</th> 34 <th> 35 <input type="text" name="h_use"> 36 </th> 37 </tr> 38 39 <tr> 40 <th>房产面积</th> 41 <th> 42 <input type="text" name="h_area"> 43 </th> 44 </tr> 45 46 <tr> 47 <th>房产户主</th> 48 <th id="hz"> 49 50 </th> 51 </tr> 52 53 <tr> 54 <th>不动资产证书</th> 55 <th> 56 <input type="file" name="myFile"> 57 </th> 58 </tr> 59 60 <tr> 61 <th colspan="10"> 62 <button>点击添加</button> 63 </th> 64 </tr> 65 </table> 66 </form> 67 </body> 68 </html>
修改
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <link rel="stylesheet" href="css/index3.css"> 9 <script type="text/javascript" src="js/jquery-1.8.3.js"></script> 10 <script type="text/javascript"> 11 $(function() { 12 $.post("ownerAll.do",function(arr){ 13 for ( var i in arr) { 14 var s=arr[i]; 15 var op="<input type='checkbox' name='oids' value='"+s.o_id+"'>"+s.o_name+""; 16 $("#hz").append(op); 17 } 18 19 var h_id=${param.h_id}; 20 $.post("selectOne.do",{h_id:h_id},function(obj){ 21 22 var owner=obj.owner; 23 for ( var i in owner) { 24 $("[name=oids]").each(function() { 25 if (this.value==owner[i].o_id) { 26 $(this).prop("checked",true); 27 } 28 }) 29 } 30 31 $("[name=h_address]").val(obj.h_address); 32 $("[name=h_use]").val(obj.h_use); 33 $("[name=h_area]").val(obj.h_area); 34 },"json") 35 },"json") 36 }) 37 </script> 38 </head> 39 <body> 40 <form action="update.do" method="post" enctype="multipart/form-data"> 41 <table> 42 <tr> 43 <th>房产坐落地址</th> 44 <th> 45 <input type="hidden" name="h_id" value="${param.h_id}"> 46 <input type="text" name="h_address"> 47 </th> 48 </tr> 49 50 <tr> 51 <th>房产用途</th> 52 <th> 53 <input type="text" name="h_use"> 54 </th> 55 </tr> 56 57 <tr> 58 <th>房产面积</th> 59 <th> 60 <input type="text" name="h_area"> 61 </th> 62 </tr> 63 64 <tr> 65 <th>房产户主</th> 66 <th id="hz"> 67 68 </th> 69 </tr> 70 71 <tr> 72 <th>不动资产证书</th> 73 <th> 74 <input type="file" name="myFile"> 75 </th> 76 </tr> 77 78 <tr> 79 <th colspan="10"> 80 <button>点击修改</button> 81 </th> 82 </tr> 83 </table> 84 </form> 85 </body> 86 </html>
登录
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <link rel="stylesheet" href="css/index3.css"> <script type="text/javascript" src="js/jquery-1.8.3.js"></script> </head> <body> ${msg} <form action="login.do" method="post"> <table> <tr> <th>用户名</th> <th> <input type="text" name="username"> </th> </tr> <tr> <th>密码</th> <th> <input type="password" name="password"> </th> </tr> <tr> <th colspan="10"> <button>登录</button> </th> </tr> </table> </form> </body> </html>