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

一、批量删除后台代码

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

效果展示: 

         

猜你喜欢

转载自blog.csdn.net/m0_46979453/article/details/121469222