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 条件查询 批量增删改查功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 阿里四面之Spring Exception的原理解析

    阿里四面之Spring Exception的原理解析

    本文给大家介绍阿里四面之Spring Exception的原理解析,本文通过错误场景分析给大家详细介绍Spring异常处理流程,感兴趣的朋友一起看看吧
    2021-10-10
  • SpringBoot实现PDF转图片的代码示例

    SpringBoot实现PDF转图片的代码示例

    在本文中,我们使用SpringBoot演示了如何将PDF文件转换为一张或多张图片,这些示例演示了如何使用Java编程语言与其他开源技术集成,以实现各种文件格式之间的转换,感兴趣的小伙伴跟着小编一起来看看吧
    2024-08-08
  • SpringBoot中@Conditional注解的使用

    SpringBoot中@Conditional注解的使用

    这篇文章主要介绍了SpringBoot中@Conditional注解的使用,@Conditional注解是一个条件装配注解,主要用于限制@Bean注解在什么时候才生效,以指定的条件形式控制bean的创建,需要的朋友可以参考下
    2024-01-01
  • 枚举java语言中的修饰符组合的实例代码

    枚举java语言中的修饰符组合的实例代码

    这篇文章主要介绍了枚举java语言中的修饰符组合,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07
  • HttpClient HttpRoutePlanner接口确定请求目标路由

    HttpClient HttpRoutePlanner接口确定请求目标路由

    这篇文章主要为大家介绍了使用HttpClient HttpRoutePlanner接口确定请求目标路由,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • 浅谈Java锁机制

    浅谈Java锁机制

    在多线程环境下,程序往往会出现一些线程安全问题,为此,Java提供了一些线程的同步机制来解决安全问题,比如:synchronized锁和Lock锁都能解决线程安全问题。下面小编就来详细介绍该知识点,需要的朋友可以参考一下
    2021-09-09
  • Hibernate实现批量添加数据的方法

    Hibernate实现批量添加数据的方法

    这篇文章主要介绍了Hibernate实现批量添加数据的方法,详细分析了基于Hibernate执行批量添加操作的具体步骤与相关实现代码,需要的朋友可以参考下
    2016-03-03
  • 关于MyBatis结果映射的实例总结

    关于MyBatis结果映射的实例总结

    结果集映射主要是为了解决属性名和类型名不一致的问题,下面这篇文章主要给大家介绍了关于MyBatis结果映射的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • Spring Boot使用Redisson实现滑动窗口限流的项目实践

    Spring Boot使用Redisson实现滑动窗口限流的项目实践

    滑动窗口限流是一种流量控制策略,用于控制在一定时间内的请求频率,本文主要介绍了Spring Boot使用Redisson实现滑动窗口限流的项目实践,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • Java及nginx实现文件权限控制代码实例

    Java及nginx实现文件权限控制代码实例

    这篇文章主要介绍了Java及nginx实现文件权限控制代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论