欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Mybatis 条件查询 批量增删改查功能

时间:2020-10-12 09:31:43|栏目:JAVA代码|点击:

模糊查询:

@Select({
    "SELECT * FROM account where account like CONCAT('%',#{query},'%') or email like CONCAT('%',#{query},'%')"
})
Account findAccountByAccountOrMail(@Param("query") String query);

批量添加:

@Insert({
    "<script>" +
        "INSERT INTO company_label(company_id,label_id) values " +
        " <foreach collection=\"item\" item=\"item\" index=\"index\" separator=\",\" > " +
        "    (#{companyId},#{item}) " +
        "  </foreach>" +
        "</script>"
})
void insertLabelForCompany(@Param("companyId") Long companyId,@Param("item") List<Long> item);

批量删除:

@Delete({
    "<script>delete from company_label where company_id = #{companyId} and label_id in " +
        "<foreach collection = \"item\" item = \"item\" open=\"(\" separator=\",\" close=\")\">" +
        "#{item}" +
        "</foreach>" +
        "</script>"
})
void removeLabelForCompany(@Param("companyId") Long companyId,@Param("item") List<Long> item);

批量修改:

@Update(value = "<script>" + "update banner b set b.display = #{status} where b.id in "+
    "<foreach item = 'item' index = 'index' collection = 'ids' open = '(' separator = ',' close = ')'>#{item}</foreach>" +
    "" +
    "</script>")
int updateStatus(@Param("status") Long status, @Param("ids") Long[] ids);

批量查询:

@Select({
    "<script>" +
        "select * from product where id in" +
        "<foreach item = 'item' index = 'index' collection = 'idList' open = '(' separator = ',' close = ')'>#{item}</foreach>" +
        "</script>"
})
List<Product> findByIdList(@Param("idList")List<Long> idList);

条件查询,if里面不仅可以判空,还可以判断是否满足某个条件

@Select({
      "<script>SELECT * FROM company where 1=1 and parent_id = #{companyId} " +
          //平级
          "<if test = \"isScanSameLevelValue == 1\">and type = #{type}</if>" +
           "<if test = \"isScanSameLevelValue == 0\">and type != #{type}</if>" +

          "</script> "
  })
  List<Company> findCompanyConditional(@Param("isScanSameLevelValue") String isScanSameLevelValue, @Param("isScanParentLevelValue") String isScanParentLevelValue, @Param("companyId") Long companyId, @Param("type") Integer type);

条件查询:

 */
@Lang(XMLLanguageDriver.class)
@Select({"<script>select DISTINCT p.* FROM `us_product`.`hot_category_surgery` hcs "+
    "LEFT JOIN `us_product`.`product` p ON hcs.`product_id` =p.`id`"+
    "LEFT JOIN `us_product`.`category_surgery` cs on cs.`product_id` =p.`id`"+
    "LEFT JOIN `us_product`.`merchant_product` mp on mp.`product_id` = p.`id`"+
    "LEFT JOIN `us_product`.`org_product` op on op.`product_id` =p.`id`"+
    "where p.`type` =1 and p.`is_for_sale` =1 "+
    "        <if test=\"hId != null\"> and hcs.hot_category_id = #{hId} and p.id = hcs.product_id</if>" + //热门类目id
    "        <if test=\"categoryId != null\"> and cs.category_id = #{categoryId} and p.id = cs.product_id</if>" + //类目id
    "        <if test=\"input != null\">    and (p.name like CONCAT('%',#{input},'%') or p.company like CONCAT('%',#{input},'%')) </if> "+  //用户输入,包括商品名和店铺名,模糊
    "        <if test = \" location != null\"> and p.location like CONCAT('%',#{location},'%') </if> "+    //位置..
    "        <if test=\"method != null\">   and mp.filter_id = #{method} and p.id = mp.product_id</if> "+  //筛选条件  手术方式
    "        <if test=\"org != null\">     and op.filter_id = #{org} and p.id = op.product_id</if> "+   //筛选条件  所属机构
    "         ORDER BY sale_volume DESC"+
    "        </script>"
})
List<Product> findProductFromLocal(@Param("hId")Long hId,@Param("categoryId")Long categoryId,@Param("input")String input,@Param("method")Long method,@Param("org")Long org,@Param("location")String location);

上一篇:SpringBoot启动报错Failed to determine a suitable driver class

栏    目:JAVA代码

下一篇:Java实现excel大数据量导入

本文标题:Mybatis 条件查询 批量增删改查功能

本文地址:http://www.codeinn.net/misctech/10077.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有