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");
            }
            }
        )
    }

效果展示:

总结

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

相关文章

  • mybatis类型处理器JSR310标准详解

    mybatis类型处理器JSR310标准详解

    这篇文章主要介绍了mybatis类型处理器JSR310标准详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java 中使用数组存储和操作数据

    Java 中使用数组存储和操作数据

    本文将介绍Java中常用的数组操作方法,通过详细的示例和解释,帮助读者全面理解和掌握这些方法,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • Java文件上传下载、邮件收发实例代码

    Java文件上传下载、邮件收发实例代码

    这篇文章主要介绍了Java文件上传下载、邮件收发实例代码的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • 如何实现Java中一个简单的LinkedList

    如何实现Java中一个简单的LinkedList

    LinkedList与ArrayList都是List接口的具体实现类。下面将介绍如何实现一个简单的LinkedList,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • Java string不可变原理实例解析

    Java string不可变原理实例解析

    这篇文章主要介绍了Java string不可变原理实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Spring过滤器中OncePerRequestFilter应用实现

    Spring过滤器中OncePerRequestFilter应用实现

    OncePerRequestFilter是Spring框架提供的一个过滤器基类,本文就来介绍一下Spring过滤器中OncePerRequestFilter应用实现,感兴趣的可以了解一下
    2024-12-12
  • SpringBoot通过URL地址获取文件的多种方式

    SpringBoot通过URL地址获取文件的多种方式

    本文介绍了多种在SpringBoot中通过URL地址获取文件的方法,包括Java原生、RestTemplate、WebClient等,并提供了详细的步骤和示例代码,同时,还讨论了异常处理、资源清理、并发控制等优化建议,需要的朋友可以参考下
    2026-01-01
  • Maven 集成 groovy 脚本插件gmavenplus-plugin详解

    Maven 集成 groovy 脚本插件gmavenplus-plugin详解

    文章介绍了在Maven构建过程中使用Groovy脚本进行动态配置的示例,重点解析了如何在`<build>`配置片段中绑定脚本到`initialize`生命周期阶段,并详细说明了脚本执行流程、常见使用场景以及潜在问题和优化建议,感兴趣的朋友跟随小编一起看看吧
    2025-12-12
  • 解决nacos项目启动报错:Connection refused: no further informa问题

    解决nacos项目启动报错:Connection refused: no further&

    这篇文章主要介绍了解决nacos项目启动报错:Connection refused: no further informa问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Spring Boot、Redis、RabbitMQ 在项目中的核心作用详解(代码示例)

    Spring Boot、Redis、RabbitMQ 在项目中的核心作用详解(代码示例)

    在现代企业级应用开发中,Spring Boot、Redis 和 RabbitMQ 已经成为不可或缺的技术组件,本文将深入剖析这三者在实际项目中的作用,并通过代码示例和流程图展示它们的实际应用,感兴趣的朋友一起看看吧
    2025-11-11

最新评论