Mybatis实现批量删除和多条件查询方式

 更新时间:2026年05月14日 09:12:43   作者:Rk..  
本文介绍了批量删除功能的实现步骤,包括后台代码的修改,具体涉及到mapper层、Service层、Controller接口等层面的修改,同时也介绍了多条件查询功能的实现,具体包括创建封装查询条件的类,mapper层、Service层、Controller接口的修改等

一、批量删除后台代码

1、mapper层

Mapper接口
     //批量删除商品的功能
    int deleteBatch(String[] ids);
Mapper.xml
  <!--  批量删除-->
  <delete id="deleteBatch">
    delete from product_info where p_id in
    <foreach collection="array" item="pid" separator="," open="(" close=")">
      #{pid}
    </foreach>
  </delete>

注意:接口传过来的是String数组,foreach循环中取的是array

2、Service层

Service接口
    int deleteBatch(String[] ids);



Service实现类
    @Override
    public int deleteBatch(String[] ids) {
        return productInfoMapper.deleteBatch(ids);
    }

3、Controller接口

   //批量删除 pids是要删除的商品的id字符串,例如:1,2,3,4,5,
    @RequestMapping("/deletebatch")
    public String deleteBatch(String pids,HttpServletRequest request) {
        String[] ps = pids.split(",");//转为数组[1,2,3,4,5]
        int num= 0;
        try {
            num = productInfoService.deleteBatch(ps);
            if(num>0)
            {
                request.setAttribute("msg","批量删除成功!");
            }else {
                request.setAttribute("msg","批量删除失败");
            }
        } catch (Exception e) {
          request.setAttribute("msg","商品不可删除!");
        }
        //批量删除后 重新分页查询
        return "forward:/prod/deleteAjaxSplit.action";
    }

4、前台视图

<input type="button" class="btn btn-warning" id="btn1"
value="批量删除" onclick="deleteBatch()">
</div>


js部分:

   //批量删除
    function deleteBatch() {
        
        //取得所有被选中删除商品的pid
        var zhi=$("input[name=ck]:checked");
        var str="";
        var id="";
        if(zhi.length==0){
            alert("请选择将要删除的商品!");
        }else{
            // 有选中的商品,则取出每个选 中商品的ID,拼提交的ID的数据
            if(confirm("您确定删除"+zhi.length+"条商品吗?")){
                //拼接ID
                $.each(zhi,function (index,item) {
                    id=$(item).val(); //22 33
                    if(id!=null)
                        str += id+",";  //22,33,44
                });
                //发送请求到服务器端
                // window.location="${pageContext.request.contextPath}/prod/deletebatch.action?str="+str;
                $.ajax({
                    url: "${pageContext.request.contextPath}/prod/deletebatch.action",
                    data: {"pids":str},
                    type: "post",
                    dataType:"text",
                    success:function (msg)
                    {
                        alert(msg);
                        $("#table").load("http://localhost:8099/admin/product.jsp #table");
                    }
                });
            }
        }
    }

二、多条件查询后台代码

1、创建一个封装查询条件的类

//封装查询条件
public class ProductInfoVo {
    //商品名称
    private String pname;
    //商品类型
    private Integer typeid;
    //最低价格
    private Double lprice;
    //最高价格
    private Double hprice;

    //补全get set  有/无参构造方法 toString()
}

2、mapper层

Mapper接口:

    //多条件查询商品
    List<ProductInfo> selectCondition(ProductInfoVo vo);

Mapper.xml:

<!--多条件查询-->
  <select id="selectCondition" parameterType="com.rk.pojo.vo.ProductInfoVo" resultMap="BaseResultMap">
    select *
    from product_info
    <!--拼条件-->
    <where>
      <if test="pname!=null and pname!=''">
        and p_name like '%${pname}%'
      </if>
      <if test="typeid!=null and typeid!=-1">
        and type_id =#{typeid}
      </if>
      <if test="(lprice!=null and lprice!='') and  (hprice==null or hprice=='')">
        and p_price &gt;= #{lprice}
      </if>
      <if test="(lprice==null or lprice=='') and  (hprice!=null and hprice!='')">
        and p_price &lt;= #{hprice}
      </if>
      <if test="(lprice!=null and lprice!='') and  (hprice!=null and hprice!='')">
        and p_price between #{lprice} and #{hprice}
      </if>
    </where>
    order by p_id desc
  </select>

3、Service层

service接口:
     //多条件商品查询
    List<ProductInfo> selectCondition(ProductInfoVo vo);

service实现类:

    @Override
    public List<ProductInfo> selectCondition(ProductInfoVo vo) {
        return productInfoMapper.selectCondition(vo);
    }

4、Controller接口

    //多条件查询
    @ResponseBody
    @RequestMapping("/condition")
    public void condition(ProductInfoVo vo, HttpSession session)
    {
        List<ProductInfo> list = productInfoService.selectCondition(vo);
        session.setAttribute("list",list);
    }

查询到的结果存入session,前端页面从新加载table表格

5、前台视图

  <div id="condition" style="text-align: center">
        <form id="myform">
            商品名称:<input name="pname" id="pname">&nbsp;&nbsp;&nbsp;
            商品类型:<select name="typeid" id="typeid">
            <option value="-1">请选择</option>
            <c:forEach items="${ptlist}" var="pt">
                <option value="${pt.typeId}">${pt.typeName}</option>
            </c:forEach>
        </select>&nbsp;&nbsp;&nbsp;
            价格:<input name="lprice" id="lprice">-<input name="hprice" id="hprice">
            <input type="button" value="查询" onclick="condition()">
        </form>
    </div>


js部分:
     //多条件查询
    function condition(){
        //取出查询条件
        var pname=$("#pname").val();
        var typeid=$("#typeid").val();
        var lprice=$("#lprice").val();
        var hprice=$("#hprice").val();
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/prod/condition.action",
            data:{"pname":pname,"typeid":typeid,
                "lprice":lprice,"hprice":hprice},
            success:function () {
                //刷新表格  后台将查询到的结果存储到seession
                $("#table").load("http://localhost:8099/admin/product.jsp #table");
            }
            }
        )
    }

效果展示:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java Web之限制用户多处登录实例代码

    Java Web之限制用户多处登录实例代码

    本篇文章主要介绍了Java Web之限制用户多处登录实例代码,可以限制单个用户在多个终端登录。非常具有实用价值,需要的朋友可以参考下。
    2017-03-03
  • springboot中spring.profiles.include的妙用分享

    springboot中spring.profiles.include的妙用分享

    这篇文章主要介绍了springboot中spring.profiles.include的妙用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • SpringCloud的JPA连接PostgreSql的教程

    SpringCloud的JPA连接PostgreSql的教程

    这篇文章主要介绍了SpringCloud的JPA接入PostgreSql 教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-06-06
  • Java详解ScriptEngine接口动态执行JS脚本

    Java详解ScriptEngine接口动态执行JS脚本

    ScriptEngine是基本接口,其方法必须在本规范的每个实现中完全起作用。这些方法提供基本脚本功能。 写入这个简单接口的应用程序可以在每个实现中进行最少的修改。 它包括执行脚本的方法,以及设置和获取值的方法
    2022-08-08
  • java转发和重定向的区别

    java转发和重定向的区别

    这篇文章主要介绍了java转发和重定向的区别,需要的朋友可以参考下
    2014-10-10
  • 在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

    在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

    在MyBatis的XML映射文件中,<trim>元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示例展示了如何在UPDATE、SELECT、INSERT和SQL片段中使用<trim>元素,以实现动态的SQL构建,感兴趣的朋友一起看看吧
    2025-01-01
  • SpringMVC中的SimpleUrlHandlerMapping用法详解

    SpringMVC中的SimpleUrlHandlerMapping用法详解

    这篇文章主要介绍了SpringMVC中的SimpleUrlHandlerMapping用法详解,SimpleUrlHandlerMapping是Spring MVC中适用性最强的Handler Mapping类,允许明确指定URL模式和Handler的映射关系,有两种方式声明SimpleUrlHandlerMapping,需要的朋友可以参考下
    2023-10-10
  • Maven多模块项目调试与问题排查的完整指南

    Maven多模块项目调试与问题排查的完整指南

    在现代企业级Java开发中,Maven多模块项目因其清晰的代码组织,依赖管理和高效的构建流程已成为主流架构模式,本文深入剖析多模块项目的四大核心痛点解决方案,感兴趣的小伙伴可以跟随小编一起了解下
    2025-06-06
  • java实现Yaml转Json示例详解

    java实现Yaml转Json示例详解

    这篇文章主要为大家介绍了java实现Yaml转Json示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 详解如何继承Mybatis中Mapper.xml文件

    详解如何继承Mybatis中Mapper.xml文件

    这篇文章主要为大家介绍了详解如何继承Mybatis中Mapper.xml文件,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09

最新评论