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

 更新时间:2017年06月21日 11:13:50   作者:番号  
这篇文章主要介绍了mybatis 脚本处理语句之条件查询 批量增删改查功能,需要的的朋友参考下吧

模糊查询:

@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);

以上所述是小编给大家介绍的Mybatis 条件查询 批量增删改查功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Java class文件格式之属性详解_动力节点java学院整理

    Java class文件格式之属性详解_动力节点java学院整理

    这篇文章主要介绍了Java class文件格式之属性详解,需要的朋友可以参考下
    2017-06-06
  • java Semaphore共享锁实现原理解析

    java Semaphore共享锁实现原理解析

    这篇文章主要为大家介绍了Semaphore共享锁实现原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • 深度解析Java中ArrayList的使用

    深度解析Java中ArrayList的使用

    ArrayList 类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素。本文将通过示例带你深度解析Java中ArrayList的使用,需要的可以参考一下
    2022-09-09
  • Java的ArrayList扩容源码解析

    Java的ArrayList扩容源码解析

    这篇文章主要介绍了Java的ArrayList扩容源码解析,通过动态扩容,ArrayList能够在添加元素时保持高效的性能,扩容操作是有一定开销的,但由于扩容的时间复杂度为O(n),其中n是当前元素个数,所以平均情况下,每次添加元素的时间复杂度仍然是O(1),需要的朋友可以参考下
    2024-01-01
  • Java调用Shell命令和脚本的实现

    Java调用Shell命令和脚本的实现

    这篇文章主要介绍了Java调用Shell命令和脚本的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • java类的组成结构详解

    java类的组成结构详解

    大家好,本篇文章主要讲的是java类的组成结构详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • java的SimpleDateFormat线程不安全的几种解决方案

    java的SimpleDateFormat线程不安全的几种解决方案

    但我们知道SimpleDateFormat是线程不安全的,处理时要特别小心,要加锁或者不能定义为static,要在方法内new出对象,再进行格式化,本文就介绍了几种方法,感兴趣的可以了解一下
    2021-08-08
  • MybatisPlus:使用SQL保留字(关键字)的操作

    MybatisPlus:使用SQL保留字(关键字)的操作

    这篇文章主要介绍了MybatisPlus:使用SQL保留字(关键字)的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • java接入创蓝253短信验证码的实例讲解

    java接入创蓝253短信验证码的实例讲解

    下面小编就为大家分享一篇java接入创蓝253短信验证码的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • Java实现石头剪刀布游戏

    Java实现石头剪刀布游戏

    这篇文章主要为大家详细介绍了Java实现石头剪刀布游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10

最新评论