ssm之路(18)模糊综合查询+批量删除

记录一下我的模糊综合查询的实现过程:

controller层   ItemsController :类

@Controller
public class ItemsController {
    @Autowired
    private ItemsService itemsService;

    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request, ItemsQueryVo itemsQueryVo) throws Exception {
        List<ItemsCustom> itemsList = itemsService.finditemsList(itemsQueryVo);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("itemsList", itemsList);
        modelAndView.setViewName("/items/itemsList");
        return modelAndView;
    }
}

mapper层:ItemsMapperCustom :接口

public interface ItemsMapperCustom {
    public List<ItemsCustom>finditemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}

mapper层的映射文件:ItemsMapperCustom.xml

<?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="cn.itcast.ssm.mapper.ItemsMapperCustom">
    <sql id="query_items_where">
        <if test="itemsCustom != null and itemsCustom != ''">
            <if test="itemsCustom.name != null and itemsCustom.name != ''">
                items.name like '%${itemsCustom.name}%'
            </if>
        </if>
    </sql>
    <select id="finditemsList" parameterType="cn.itcast.ssm.pojo.ItemsQueryVo" resultType="cn.itcast.ssm.pojo.ItemsCustom">
    SELECT * FROM items
    <where>
        <include refid="query_items_where"></include>
    </where>
</select>
</mapper>

pojo层: 实体类item+实体类的拓展类itemCustom

public class Items {

    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;


//省略getter,setter
}

public class ItemsCustom extends Items {
}

service层  ItemsService接口和ItemsServiceImpl实现类:

public interface ItemsService {
    public List<ItemsCustom> finditemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
@Service
public class ItemsServiceImpl implements ItemsService {
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Override
    public List<ItemsCustom> finditemsList(ItemsQueryVo itemsQueryVo) throws Exception {
        List<ItemsCustom> list=itemsMapperCustom.finditemsList(itemsQueryVo);
        return list;
    }
}

视图层jsp页面:  itemsList.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>查询商品列表</title>
</head>
<body>
<%--<iframe name="targetIfr" style="display:none"></iframe>--%>
<form action="${pageContext.request.contextPath}/queryItems.action" method="post" id="itemsForm" target="targetIfr">
    查询条件:
    <table width="100%" border="1">
        <tr>
            <td>商品名称<input name="itemsCustom.name"></td>
            <td><input type="submit" value="查询" ><input type="button" value="批量删除"  onclick="deleteItems"/></td>
        </tr>
    </table>
    商品列表:
    <table width="100%" border="1">
        <tr>
            <td>选择</td>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${itemsList}" var="item">
            <tr>
                <td><input type="checkbox" name="items_id" value="${item.id}"></td>
            <td>${item.name}</td>
            <td>${item.price}</td>
            <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
            <td>${item.detail}</td>

            <td><a href="${pageContext.request.contextPath}/editItems.action">修改</a> </td>
            </tr>
        </c:forEach>
    </table>
</form>

<script type="text/javascript">
</script>
</body>
</html>

测试方法:  浏览器输入:http://localhost:8080/queryItems.action

写批量删除前先记录一个js动态修改表单的报错:

<form action="#" method="post" id="itemsForm">
<button onclick="queryItems()">查询</button>
</form>

js的代码:

<script type="text/javascript">
    function queryItems() {
        document.getElementById("itemsForm").action ="${pageContext.request.contextPath}/queryItems.action";
        document.getElementById("itemsForm").submit();
    }
</script>

开始时我写成

 document.itemsForm.action ="/xxxxx.action";

 document.itemsForm.submit ;

我在浏览器输入的地址的是:http://localhost:8080/queryItems.action

访问到页面后,我点击按钮,浏览器地址变为:http://localhost:8080/queryItems.action#(但仍能访问我的页面),后台报错:

cann't set property 'action' of null;

这样写当然是错的了,但有的真能正常无误的运行,不知为啥,

找到原因了,如果要 document.itemsForm.action ="/xxxxx.action";,则要给form增加name属性,及改成

<form action="#" method="post" name="itemsForm">,这样就能运行了

后面将其改为:

document.getElementById(itemsForm).action ="${pageContext.request.contextPath}/queryItems.action";
        document.getElementById(itemsForm).submit();

但我又手贱犯错了,getElementById(itemsForm)里面的itemsForm要加单引号啊。每次碰到自己手贱的问题,都会推理成我代码的语法不正确,有缓存,。。。其实很多时候就是“少加了个引号,导包导入的不正确,。。。。”路还是要一步一步走啊,不然你咋知道是哪错了呢,同样的错误,这次犯了,下次就不会犯,或犯了也能回忆起上一次是整么解决的,立马就能解决,,那就是在前进,同样的难题,这次不会,通过百度会了,通过请教他人会了,这是种收获,(不会的还不虚心向他人请教,那就是不会了,还谈什么收获呢,谈谈你的失败感言好了。),下一次再遇到,你会了,能解决它了,这是种收获,更是一种进步。若还不会,那要再重做,并做好记录,保存下来,经常浏览它,温习它,熟悉它,知道你下次碰到它的时候,你会了为止。遇到事情,不要慌,小问题,有办法。路虽远,你一步一步的走,行则将至。中间不免有诸多诱惑,学会说拒绝,不逞能,实事求是,不装大佬,不懂就坦诚说我不懂,别装作你懂,不懂就多问,别人爱将,你就多问点,不爱讲,你就少问点,虚心请教,知之就说知之,不知就说不知,这是种态度。说多了,开始撸码:

批量删除的主要代码如下:

首先是sql语句:    ItemsMapperCustom.xml:

   <delete id="deleteitemsList" parameterType="java.lang.Integer">
        delete FROM items
        <where>
            id in
            <foreach collection="array" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </delete>

mapper层的接口   ItemsMapperCustom:

public interface ItemsMapperCustom {
    public void deleteitemsList(Integer[] ids)throws Exception;
}

service层的实现类   ItemsServiceImpl

   @Override
    public void deleteitemsList(Integer[] ids) throws Exception {
         itemsMapperCustom.deleteitemsList(ids);
    }

servcie的接口   ItemsService:

public interface ItemsService {
    public void deleteitemsList(Integer[] ids)throws Exception;
}

controller层的   ItemsController:

@Controller
public class ItemsController {
    @Autowired
    private ItemsService itemsService; 

    @RequestMapping("/deleteItems")
    public void deleteItems(HttpServletResponse response,Integer[] ids) throws Exception {
        System.out.println(ids);
        itemsService.deleteitemsList(ids);
        response.getWriter().write("success");
    }

视图层的jsp页面   itemsList.jsp :

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>

<head>
  <%--  <base href="<%=basePath%>">--%>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>查询商品列表</title>
</head>
<body>
<form action="#" method="post" id="itemsForm">
    查询条件:
    <table width="100%" border="1">
        <tr>
            <td>商品名称&nbsp&nbsp&nbsp&nbsp<input name="itemsCustom.name"></td>
            <td><button onclick="queryItems()">查询</button> </td>
            <td><button onclick="deleteItems()">删除</button> </td>
        </tr>
    </table>
    商品列表:
    <table width="100%" border="1">
        <tr>
            <td>选择</td>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${itemsList}" var="item">
            <tr>
                <td><input type="checkbox" name="ids" value="${item.id}"></td>
            <td>${item.name}</td>
            <td>${item.price}</td>
            <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
            <td>${item.detail}</td>

            <td><a href="${pageContext.request.contextPath}/editItems.action">修改</a> </td>
            </tr>
        </c:forEach>
    </table>
</form>

<script type="text/javascript">
    function queryItems() {
        document.getElementById("itemsForm").action ="${pageContext.request.contextPath}/queryItems.action";
       document.getElementById("itemsForm").submit();
    }
    function deleteItems() {
        document.getElementById("itemsForm").action="${pageContext.request.contextPath}/deleteItems.action";
        document.getElementById("itemsForm").submit;
    }
</script>
</body>
</html>

输入地址测试即可。

中间有几点需要注意:

1.sql语句的映射文件里:

通过多个id查询信息:传入的参数的  Integer[] ids;      collection里不能写ids,如果传入是数组,要写array,是集合就写list;改写成如下: 

 <foreach collection="array" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>

否则会报Parameter 'ids' not found. Available parameters are [array]

因为是集合,所以也不能想当然的这样写:  delete FROM items where id in (#{ids})

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/83933479
今日推荐