1 List<Goods> listGoods(Condition con);
2
3 List<Gtype> gtypes();
4
5 int add(Goods g);
6
7 Goods toUpdate(Integer gid);
8
9 int update(Goods g);
10
11 boolean delete(String gids);
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <!-- 映射文件中标签的id值要和dao层接口中的方法名相同 -->
6 <mapper namespace="com.xwx.dao.GoodsDao">
7 <!-- 列表展示 -->
8 <select id="listGoods" resultMap="goodsMap">
9 select * from goods join gtype on goods.tid = gtype.tid
10 <where>
11 <if test="gname!=null and gname!=''"> and gname like concat('%',#{gname},'%') </if>
12 <if test="price1!=null and price1!=0"> and price >=#{price1} </if>
13 <if test="price2!=null and price2!=0"> and price <=#{price2} </if>
14 </where>
15 </select>
16 <!-- 配置映射关系 -->
17 <resultMap type="Goods" id="goodsMap">
18 <id property="gid" column="gid"/>
19 <result property="gname" column="gname"/>
20 <result property="price" column="price"/>
21 <result property="base_num" column="base_num"/>
22 <result property="address" column="address"/>
23 <association property="gtype" javaType="Gtype">
24 <id property="tid" column="tid"/>
25 <result property="tname" column="tname"/>
26 </association>
27 </resultMap>
28
29 <!-- 查询所有商品类型 -->
30 <select id="gtypes" resultType="Gtype">
31 select * from gtype
32 </select>
33 <!-- 添加 -->
34 <insert id="add">
35 insert into goods values(null,#{gname},#{price},#{base_num},#{address},#{gtype.tid})
36 </insert>
37 <!-- 回显 -->
38 <select id="toUpdate" resultMap="goodsMap">
39 select * from goods join gtype on goods.tid = gtype.tid where gid=#{gid}
40 </select>
41
42 <!-- 修改 -->
43 <update id="update">
44 update goods set gname=#{gname},price=#{price},base_num=#{base_num},address=#{address},tid=#{gtype.tid} where gid=#{gid}
45 </update>
46 <!-- 删除 -->
47 <delete id="delete">
48 delete from goods where gid in(${value})
1 package com.xwx.controller;
2
3 import java.util.List;
4
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.ui.Model;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.ResponseBody;
10
11 import com.github.pagehelper.PageHelper;
12 import com.github.pagehelper.PageInfo;
13 import com.xwx.entity.Condition;
14 import com.xwx.entity.Goods;
15 import com.xwx.entity.Gtype;
16 import com.xwx.service.GoodsService;
17
18 @Controller
19 public class GoodsController {
20
21
22 @Autowired
23 private GoodsService service;
24
25
26 @RequestMapping("listGoods.do")
27 public String listGoods(Model m,Condition con) {
28 //当前页处理
29 if(con.getPageNum()==null) {
30 con.setPageNum(1);
31 }
32 //使用分页工具
33 PageHelper.startPage(con.getPageNum(),4);
34 //列表查询
35 List<Goods> list = service.listGoods(con);
36 //将集合使用pageInfo封装,得到分页要用的数据
37 PageInfo<Goods> page = new PageInfo<Goods>(list);
38
39 //将数据存储到model中
40 m.addAttribute("page", page);
41 m.addAttribute("list", list);
42 m.addAttribute("con", con);
43
44 return "list";
45 }
46 @ResponseBody
47 @RequestMapping("gtypes.do")
48 public Object gtypes() {
49 //获取所有商品类型
50 List<Gtype> list = service.gtypes();
51
52 return list;
53 }
54 @ResponseBody
55 @RequestMapping("add.do")
56 public Object add(Goods g) {
57 //执行添加,获取添加结果
58 boolean flag = service.add(g);
59
60 return flag;
61 }
62 @ResponseBody
63 @RequestMapping("toUpdate.do")
64 public Object toUpdate(Integer gid) {
65 //查询商品
66 Goods g = service.toUpdate(gid);
67
68 return g;
69 }
70 @ResponseBody
71 @RequestMapping("update.do")
72 public Object update(Goods g) {
73 //执行添加,获取添加结果
74 boolean flag = service.update(g);
75
76 return flag;
77 }
78 @ResponseBody
79 @RequestMapping("delete.do")
80 public Object delete(String gids) {
81 //执行添加,获取添加结果
82 boolean flag = service.delete(gids);
83
84 return flag;
85 }
86 }
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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; 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 //将当前页赋给form保单中隐藏标签
14 $("[name=pageNum]").val(pageNum);
15 //提交form表单
16 $("form").submit();
17
18 }
19 //全选,全不选
20 function choose(own){
21 $("[name=check]").attr("checked",own.checked);
22 }
23 //添加
24 function add(){
25 location="add.jsp";
26 }
27 //修改
28 function upd(gid){
29 location="update.jsp?gid="+gid;
30 }
31 //批量删除
32 function deleteBatch(){
33
34 /* var gids ="";
35 $("[name=check]:checked").each(function(){
36 gids+=","+this.value;
37 })
38
39 alert(gids.substring(1)); */
40
41 //map:将jquery集合转换成其他数组
42 //get():将其他数组转换成js数组
43 //join():将数组中的元素拼接成字符串,如果不传值默认用逗号拼接
44
45 if(confirm("确定删除?")){
46 //拼接字符串
47 var gids = $("[name=check]:checked").map(function(){
48 return this.value;
49 }).get().join();
50 //将拼接好的gid字符串传到后台
51 $.post("delete.do",{gids:gids},function(flag){
52
53 if(flag){
54 alert("删除成功!");
55 location="listGoods.do";
56 }else{
57 alert("删失败!");
58 }
59 },"json")
60 }
61
62 }
63 //单删
64 function del(gids){
65 $.post("delete.do",{gids:gids},function(flag){
66 if(flag){
67 alert("删除成功!");
68 location="listGoods.do";
69 }else{
70 alert("删失败!");
71 }
72 },"json")
73 }
74 </script>
75 </head>
76 <body>
77 <form action="listGoods.do" method="post">
78 <input type="hidden" name="pageNum"/>
79 商品:<input type="text" name="gname" value="${con.gname}"/>
80 价格:<input type="text" name="price1" value="${con.price1}"/>--
81 <input type="text" name="price2" value="${con.price2}"/>
82 <button>查看</button>
83 </form>
84 <table>
85 <tr>
86 <td>
87 <input type="checkbox" onclick="choose(this)"/>
88 </td>
89 <td>编号</td>
90 <td>商品</td>
91 <td>价格</td>
92 <td>库存</td>
93 <td>地址</td>
94 <td>类型</td>
95 <td>操作
96 <button onclick="add()">添加</button>
97 </td>
98 </tr>
99 <c:forEach items="${list}" var="g" varStatus="count">
100 <tr>
101 <td>
102 <input type="checkbox"name="check" value="${g.gid}"/>
103 </td>
104 <td>${count.count+page.startRow-1}</td>
105 <td>${g.gname}</td>
106 <td>${g.price}</td>
107 <td>${g.base_num}</td>
108 <td>${g.address}</td>
109 <td>${g.gtype.tname}</td>
110 <td>
111 <button>查看</button>
112 <button onclick="upd('${g.gid}')">编辑</button>
113 <button onclick="del('${g.gid}')">删除</button>
114 </td>
115 </tr>
116 </c:forEach>
117 <tr>
118 <td colspan="10">
119 <button onclick="fenye(1)">首页</button>
120 <button onclick="fenye(${page.prePage==0?'1':page.prePage})">上一页</button>
121 <button onclick="fenye(${page.nextPage==0?page.pages:page.nextPage})">下一页</button>
122 <button onclick="fenye(${page.pages})">尾页</button>
123 当前${page.pageNum}/${page.pages}页,共${page.total}条
124
125
126 <button onclick="deleteBatch()">批量删除</button>
127 </td>
128 <tr>
129 </table>
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; 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 //文档就绪函数
12 $(function(){
13 //使用ajax动态查询商品类型,然后追加到页面的下拉框中
14 $.post("gtypes.do",function(arr){
15 for ( var i in arr) {
16 $("select").append("<option value='"+arr[i].tid+"'>"+arr[i].tname+"</option>");
17 }
18 },"json")
19 })
20
21 //添加
22 function tj(){
23 //使用表单序列化方法获取表单中数据
24 var param = $("form").serialize();
25
26 $.post("add.do",param,function(flag){
27
28 if(flag){
29 alert("添加成功!");
30 location="listGoods.do";
31 }else{
32 alert("添加失败!");
33 }
34 },"json")
35 }
36 </script>
37 </head>
38 <body>
39 <form>
40 <Table>
41 <tr>
42 <Td>商品:</Td>
43 <Td>
44 <input type="text" name="gname"/>
45 </Td>
46 </tr>
47 <tr>
48 <Td>价格:</Td>
49 <Td>
50 <input type="text" name="price"/>
51 </Td>
52 </tr>
53 <tr>
54 <Td>库存:</Td>
55 <Td>
56 <input type="text" name="base_num"/>
57 </Td>
58 </tr>
59 <tr>
60 <Td>地址:</Td>
61 <Td>
62 <input type="text" name="address"/>
63 </Td>
64 </tr>
65 <tr>
66 <Td>商品类型:</Td>
67 <Td>
68 <select name="gtype.tid">
69 <option value="-1">请选择</option>
70 </select>
71 </Td>
72 </tr>
73 <tr>
74 <Td></Td>
75 <Td>
76 <input type="button" value="提交" onclick="tj()"/>
77 </Td>
78 </tr>
79 </Table>
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; 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 <%-- <%=request.getParameter("gid")%> == ${param.gid} --%>
11 <script type="text/javascript">
12 //文档就绪函数
13 $(function(){
14
15 //接收要回显数据的gid
16 var gid = "${param.gid}";
17 //使用ajax传gid,查询这条记录
18 $.post("toUpdate.do",{gid:gid},function(obj){
19 //查询到的数据给标签赋值
20 $("[name=gname]").val(obj.gname);
21 $("[name=price]").val(obj.price);
22 $("[name=base_num]").val(obj.base_num);
23 $("[name=address]").val(obj.address);
24
25 //使用ajax动态查询商品类型,然后追加到页面的下拉框中
26 $.post("gtypes.do",function(arr){
27 for ( var i in arr) {
28 if(arr[i].tid==obj.gtype.tid){
29
30 $("select").append("<option selected value='"+arr[i].tid+"'>"+arr[i].tname+"</option>");
31 }else{
32 $("select").append("<option value='"+arr[i].tid+"'>"+arr[i].tname+"</option>");
33 }
34 }
35 },"json")
36
37 },"json")
38
39
40 })
41
42 //修改
43 function tj(){
44 //使用表单序列化方法获取表单中数据
45 var param = $("form").serialize();
46
47 $.post("update.do",param,function(flag){
48
49 if(flag){
50 alert("修改成功!");
51 location="listGoods.do";
52 }else{
53 alert("修改失败!");
54 }
55 },"json")
56 }
57 </script>
58 </head>
59 <body>
60 <form>
61 <Table>
62 <tr>
63 <Td>商品:</Td>
64 <Td>
65 <input type="hidden" name="gid" value="${param.gid}"/>
66 <input type="text" name="gname"/>
67 </Td>
68 </tr>
69 <tr>
70 <Td>价格:</Td>
71 <Td>
72 <input type="text" name="price"/>
73 </Td>
74 </tr>
75 <tr>
76 <Td>库存:</Td>
77 <Td>
78 <input type="text" name="base_num"/>
79 </Td>
80 </tr>
81 <tr>
82 <Td>地址:</Td>
83 <Td>
84 <input type="text" name="address"/>
85 </Td>
86 </tr>
87 <tr>
88 <Td>商品类型:</Td>
89 <Td>
90 <select name="gtype.tid">
91 <option value="-1">请选择</option>
92 </select>
93 </Td>
94 </tr>
95 <tr>
96 <Td></Td>
97 <Td>
98 <input type="button" value="提交" onclick="tj()"/>
99 </Td>
100 </tr>
101 </Table>
102
103 </form>
104 </body>
105 </html>
80
81 </form>
82 </body>
83 </html>
130 </body>
131 </html>
49 </delete>
50 </mapper>