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

发布时间 - 2026-01-11 01:59:19    点击率:

模糊查询:

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


# mybatis  # 条件查询  # mybatis增删改查  # 详解MyBatis直接执行SQL查询及数据批量插入  # Mybatis如何根据List批量查询List结果  # mybatis接收以逗号分隔的字符串批量查询方式  # mybatis批量新增、删除、查询和修改方式  # MyBatis的五种批量查询实例总结  # 小编  # 类目  # 还可以  # 在此  # 给大家  # 所述  # 给我留言  # 感谢大家  # 判断是否  # 疑问请  # 有任何  # insertLabelForCompany  # Long  # Delete  # List 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel Admin后台管理框架推荐_Laravel快速开发后台工具  Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道  Laravel Pest测试框架怎么用_从PHPUnit转向Pest的Laravel测试教程  微信小程序 配置文件详细介绍  香港服务器租用每月最低只需15元?  Laravel如何实现API版本控制_Laravel API版本化路由设计策略  Linux网络带宽限制_tc配置实践解析【教程】  Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】  PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)  Angular 表单中正确绑定输入值以确保提交与验证正常工作  香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  Python并发异常传播_错误处理解析【教程】  Laravel Seeder填充数据教程_Laravel模型工厂Factory使用  高性能网站服务器配置指南:安全稳定与高效建站核心方案  Python文件异常处理策略_健壮性说明【指导】  佛山企业网站制作公司有哪些,沟通100网上服务官网?  Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件  作用域操作符会触发自动加载吗_php类自动加载机制与::调用【教程】  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧  iOS UIView常见属性方法小结  如何用西部建站助手快速创建专业网站?  JS弹性运动实现方法分析  手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?  浅析上传头像示例及其注意事项  如何制作一个表白网站视频,关于勇敢表白的小标题?  使用spring连接及操作mongodb3.0实例  如何构建满足综合性能需求的优质建站方案?  微信小程序 scroll-view组件实现列表页实例代码  如何快速搭建支持数据库操作的智能建站平台?  如何在阿里云完成域名注册与建站?  为什么php本地部署后css不生效_静态资源加载失败修复技巧【技巧】  Laravel事件监听器怎么写_Laravel Event和Listener使用教程  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?  在线制作视频的网站有哪些,电脑如何制作视频短片?  为什么要用作用域操作符_php中访问类常量与静态属性的优势【解答】  如何快速查询网址的建站时间与历史轨迹?  EditPlus中的正则表达式 实战(1)  制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?  焦点电影公司作品,电影焦点结局是什么?  高端网站建设与定制开发一站式解决方案 中企动力  新三国志曹操传主线渭水交兵攻略  nginx修改上传文件大小限制的方法  Laravel如何使用缓存系统提升性能_Laravel缓存驱动和应用优化方案  WordPress 子目录安装中正确处理脚本路径的完整指南  阿里云网站搭建费用解析:服务器价格与建站成本优化指南  哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?  Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】