mybatis批量查询

注意这里的 in 和   <trim prefix="(" suffix=")"> 以及 in ( )的三种方式的(例1(推荐),例2,例3(推荐))等价使用

例1:

List<UBaseMenu> findMenuName(List<String> valueList);
<select id="findMenuName" resultType="java.lang.String" parameterType="java.util.List">
select menu_name
from menu
where menu_id in
<foreach collection="list" item="valueList" open="(" close=")" separator=",">
#{valueList}
</foreach>
</select>

例2:

List<ReturnCodeEntity> selectByIds(@Param("ids") List<Long> ids);
<select id="selectByIds" parameterType="java.util.List"
resultType="com.paic.ocss.gateway.model.entity.ReturnCodeEntity">
SELECT
id,
code,
message,
recommendation,
status
FROM openapi_return_code
WHERE id in
<trim prefix="(" suffix=")">
<foreach collection="ids" index="index" item="id" separator=",">
#{id}
</foreach>
</trim>
</select>

 例3:

mapper接口代码:

public List<User> findUserListByIdList(List<Long> idList);    

xml代码:

<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach collection="list" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>

批量插入:

mapper.java

int addResource(List<Resource> ResourceList);

mapper.xml

<insert id="addResource" parameterType="java.util.List">
insert into resource (object_id, res_id, res_detail_value,
res_detail_name)
values
<foreach collection="list" item=" ResourceList " index="index" separator=",">
(#{ResourceList.objectId,jdbcType=VARCHAR},
#{ResourceList.resId,jdbcType=VARCHAR},
#{ResourceList.resDetailValue,jdbcType=VARCHAR},
#{ResourceList.resDetailName,jdbcType=VARCHAR}
)
</foreach>
</insert>

批量更新:

mapper.java

int updateRoles(List<String> roleList);

mapper.xml

<update id="updateRoles" parameterType="java.util.List">
update role
set enabled = '0'
where role_id in <foreach collection="list" item="roleIds" index="index" open="(" separator="," close=")">
#{roleIds}
</foreach>
</update>

Mybatis批处理(批量查询,更新,插入)的更多相关文章

  1. mybatis之批量查询

    关于MyBatis批量更新和添加,参考我的如下文章即可:MyBatis的批量更新实例 MyBatis的批量添加实例 另外不管是批量的新增.删除.修改.查询也好,还是单个新增.删除.修改查询也罢.都会用 ...

  2. 记录MyBatis text类型 查询 更新 数据是null

    数据库表里面存在text或者blob字段.逆向工程自动生成的MyBatis的xml中会多出几个以withBlobs结尾的方法和resultMap 此时查询数据或者更新数据的使用仍然使用selectBy ...

  3. myBatista批量查询和插入

    <select id="queryCompanyByDistrict" resultType="WyCompany"> SELECT * FROM ...

  4. mybatis + mysql 批量插入、删除、更新

    mybatis + mysql 批量插入.删除.更新 Student 表结构 批量插入 public int insertBatchStudent(List<Student> studen ...

  5. mybatis+mysql批量插入和批量更新、存在及更新

    mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ...

  6. MySQL进行 批量插入,批量删除,批量更新,批量查询

    1.批量插入 ServiceImpl层 List<Person> addPeople = new ArrayList<>(); //addPeople存放多个Person对象 ...

  7. mybatis中批量插入的两种方式(高效插入)

    MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用 ...

  8. Mybatis 实现批量插入和批量删除源码实例

    Mybatis 实现批量插入数据和批量删除数据 学习内容: 准备工作 1.数据库新建表 2.新建 Maven 项目和设置编译版本及添加依赖 3.新建 db.properties 4.新建 mybati ...

  9. mybatis执行批量更新update

    Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...

随机推荐

  1. 马蜂窝 IM 系统架构的演化和升级

    今天,越来越多的用户被马蜂窝持续积累的笔记.攻略.嗡嗡等优质的分享内容所吸引,在这里激发了去旅行的热情,同时也拉动了马蜂窝交易的增长.在帮助用户做出旅行决策.完成交易的过程中,IM 系统起到了重要的作 ...

  2. 个人永久性免费-Excel催化剂功能第82波-复制粘贴按源区域大小自动扩展收缩目标区域

    日常工作中,复制粘贴的操作,永远是最高频的操作,没有之一,在最高频的操作上,进行优化,让过程更智能,比一天到晚鼓吹人工智能替换人的骇人听闻的新闻来得更实际.此篇带来一点点的小小的改进,让日后无数的复制 ...

  3. Error:Failed to resolve: com.android.support:support-annotations:26.0.2

    异常信息记录: Error:Failed to resolve: com.android.support:support-annotations:26.0.2 <a href="ins ...

  4. 小白开学Asp.Net Core 开篇

    开学Asp.Net Core 开篇 一.准备工作 1.操作环境:Win10 2.开发工具:VS2019 3.运行环境:.Net Core 2.2 4.数据库:SqlServer2012 二.项目搭建 ...

  5. sql注入------基于时间延迟benchmark函数注入脚本

    #author:windy_2import requests urlx = 'http://127.0.0.1/?id= 1 and if((substr((select database()),' ...

  6. 使用 Spring Framework 时常犯的十大错误

    Spring 可以说是最流行的 Java 框架之一,也是一只需要驯服的强大野兽.虽然它的基本概念相当容易掌握,但成为一名强大的 Spring 开发者仍需要很多时间和努力. 在本文中,我们将介绍 Spr ...

  7. sessionID是如何在客户端和服务器端传递的?

    sessionID是如何在客户端和服务器端传递的? 服务器初次创建session的时候后返回session到客服端(在返回头(response)中有setCookie),浏览器会把sessionnam ...

  8. Linux操作系统和Windows操作系统的区别

    1.免费与收费 在中国,windows和linux都是免费的,至少对个人用户是如此,如果那天国内windows真的严打盗版了,那linux的春天就到了!但现在linux依然是任重道远,前路漫漫. 2. ...

  9. python 文件读写总结

    这是个人在项目中抽取的代码,自己写的utils的通用模块,使用的框架是tronado,包括了文件的读写操作,api格式的统一函数,如有特别需要可以联系我或者自己扩展,刚学python不久,仅供参考,例 ...

  10. MyBatis框架之关联查询

    概述:关联查询主要在<resultMap>元素中,用<association>配置一对一.用<collection> 配置一对多 一.一对一查询       1.使 ...