mybatis结合mysql批量操作及查询sql
MySQL数据库
批量操作主要使用的是Mybatis的foreach,遍历参数列表执行相应的操作,所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已。MySql批量操作需要数据库连接配置allowMultiQueries=true才可以。
(0)批量查询:
<select id="selectUserDataList" parameterType="list" resultType="String">
select userData from tbl_hbb_user_info where mobile in (
<foreach collection="list" item="item" index="index"
separator=",">
#{item.mobile}
</foreach>
)
</select>
(1)批量插入
<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">
<foreach close="" collection="list" index="index" item="item" open="" separator=";">
insert into user (name, age,dept_code) values
(#{item.name,jdbcType=VARCHAR},
#{item.age,jdbcType=INTEGER},
#{item.deptCode,jdbcType=VARCHAR}
)
</foreach>
</insert>
上面演示的是MySql的写法(表主键自增的写法),因为MySql支持主键自增,所以直接设置useGeneratedKeys=true,即可在插入数据时自动实现主键自增;不需要自增时就不需要设置useGeneratedKeys,而且插入SQL包含所有字段即可。实际Mysql还有另外一种写法,就是拼接values的写法,这种方法我测试过比多条insert语句执行的效率会高些。不过需要注意一次批量操作的数量做一定的限制。具体写法如下:
<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">
insert into user (name, age,dept_code) values
<foreach collection="list" index="index" item="item" open="" close="" separator=",">
(#{item.name,jdbcType=VARCHAR},
#{item.age,jdbcType=INTEGER},
#{item.deptCode,jdbcType=VARCHAR}
)
</foreach>
</insert>
对于Oracle不支持主键自增,需要序列替换,所以在SQL写法上略有不同,需要在insert语句前加个
<selectKey>...</selectKey>告知Mybatis主键如何生成(selectKey中间的内容有省略,实际是生成主键的SQL)。
(2)批量更新
<update id="batchUpdate" parameterType="java.util.List">
<foreach close="" collection="list" index="index" item="item" open="" separator=";">
update user set name=#{item.name,jdbcType=VARCHAR},age=#{item.age,jdbcType=INTEGER}
where id=#{item.id,jdbcType=INTEGER}
</foreach>
</update>
(3)批量删除
<delete id="batchDelete" parameterType="java.util.List">
<foreach close="" collection="list" index="index" item="item" open="" separator=";">
delete from user
where id=#{item.id,jdbcType=INTEGER}
</foreach>
</delete>
二、模糊查询
<select id="selectLikeName" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where name like CONCAT('%',#{name},'%' )
</select>
上面的模糊查询语句是Mysql数据库的写法示例,用到了Mysql的字符串拼接函数CONCAT,其它数据库使用相应的函数即可。
三、多条件查询
多条件查询常用到Mybatis的if判断,这样只有条件满足时,才生成对应的SQL。
<select id="selectUser" parameterType="map" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
<where>
<if test="name != null">
name = #{name,jdbcType=VARCHAR}
</if>
<if test="age != null">
and age = #{age,jdbcType=INTEGER}
</if>
</where>
</select>
四、联表查询
联表查询在返回结果集为多张表的数据时,可以通过继承resultMap,简化写法。例如下面的示例,结果集在User表字段的基础上添加了Dept的部门名称
<resultMap id="ExtResultMap" type="com.research.mybatis.generator.model.UserExt" extends="BaseResultMap">
<result column="name" jdbcType="VARCHAR" property="deptName" />
</resultMap> <select id="selectUserExt" parameterType="map" resultMap="ExtResultMap">
select
u.*, d.name
from user u inner join dept d on u.dept_code = d.code
<where>
<if test="name != null">
u.name = #{name,jdbcType=VARCHAR}
</if>
<if test="age != null">
and u.age = #{age,jdbcType=INTEGER}
</if>
</where>
</select>

<update id="stockDetailOkBatchUpdate" parameterType="map">
<foreach collection="items" index="index" item="item" open="begin" close=";end;" separator=";">
update T_MM_ADD_STOCK_DETAIL t
set
t.REMARK=#{item.remark},
t.modify_time=sysdate,
t.modify_user_code=#{currentUser}
where t.id=#{item.id}
<if test="index==items.size-1">
;
update T_MM_ADD_STOCK t
set
t.modify_time=sysdate,
t.modify_user_code=#{currentUser},
t.remark=#{remark},
t.STORAGE_STATE='待录价'
where t.id=#{mainId}
</if>
</foreach>
</update>

mybatis结合mysql批量操作及查询sql的更多相关文章
- Mybatis使用MySQL进行模糊查询时输入中文检索不到结果
Mybatis使用MySQL进行模糊查询时输入中文检索时,需要在jdbcURL后增加参数 ?useUnicode=true&characterEncoding=UTF-8
- MySQL 的分页查询 SQL 语句
MySQL一般使用 LIMIT 实现分页.基本语句为: SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT ... 在中小数据量的情况下,这样的SQL足够 ...
- mysql表名查询sql
select table_schema,table_name,engine from information_schema.tables where table_schema not in('info ...
- MySQL锁表查询SQL
// 查看进程 SHOW PROCESSLIST; // 查看是否锁表 SHOW OPEN TABLES WHERE In_use > 0; // 查看正在锁的事务 SELECT * FROM ...
- 使用mybatis从mysql里进行模糊查询的编码问题
关于这个问题,记录下我的解决方法,希望对有同样困惑的朋友,有所帮助. 问题描述: 我在做mybatis从mysql里模糊查询时,如果模糊的关键词是字母的话,可以查出来.如果模糊的关键词是汉字的话,查不 ...
- Mybatis使用MySQL模糊查询时输入中文检索不到结果怎么办--转自http://www.jb51.net/article/88236.htm
这篇文章主要介绍了Mybatis使用MySQL模糊查询时输入中文检索不到结果的解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下 项目开发中,在做Mybatis动态查询时,遇到了 ...
- mybatis 多表查询sql
在使用spring,spring mvc,mybatis时,mybatis链接数据库做多表查询的时候,sql语句中直接使用left join等链接字符就可以 链接多个表,参数类型是parameterT ...
- 步步深入:MySQL架构总览->查询执行流程->SQL解析顺序
前言: 一直是想知道一条SQL语句是怎么被执行的,它执行的顺序是怎样的,然后查看总结各方资料,就有了下面这一篇博文了. 本文将从MySQL总体架构--->查询执行流程--->语句执行顺序来 ...
- MySQL、Oracle和SQL Server的分页查询语句
假设当前是第PageNo页,每页有PageSize条记录,现在分别用Mysql.Oracle和SQL Server分页查询student表. 1.Mysql的分页查询: SELECT * FROM s ...
随机推荐
- MySql 查询表中字段的数据类型
[1]MySQL中查询某表中字段的数据类型 (1)DESC 表名: (2)DESCRIBE 表名: (3)SHOW COLUMNS FROM 表名: 应用示例: DESC cfg_acct_free_ ...
- qDeleteAll 之后必须清空容器
[1]qDeleteAll应用示例 qDeleteAll源码如下: template <typename ForwardIterator> Q_OUTOFLINE_TEMPLATE voi ...
- py3中reduce
前因 因为 Guido 先生讨厌 reduce.(Because Guido hates it. ) 详细过程请翻阅原文:http://www.artima.com/forums/flat.jsp?f ...
- springboot报错Whitelabel Error Page
第一次使用springboot没有问题.隔了两天继续看.一直报错Whitelabel Error Page. 重新搭建试了任何方法都错了. 报的就是一个404错误,犯了一个习惯性错误,一般都是loca ...
- Spring整合Shiro
apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的 认证和授权方式.其提供的 native-session(即把用户认证后的授权信息保存 ...
- Fiddler4入门--手机抓包工具安装和使用说明
Fiddler4入门--手机抓包工具安装和使用说明.电脑最好是笔记本连同一个wifi,这样能和手机保持统一局域网内. 很多区块链dapp项目方风控做的很差,利用fiddler抓包分析找一些漏洞,然后利 ...
- Spring cloud开发内存占用过高解决方法
https://blog.csdn.net/wanhuiguizong/article/details/79289986 版权声明:本文为博主原创文章,转载请声明文章来源和原文链接. https:// ...
- echo 命令详解
echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串. 选项 -e:激活转义字符. 使用-e选项时,若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出: \a ...
- Linux:编译安装boost 1.69库
Boost库是为C++语言标准库提供扩展的一些C++程序库的总称,由Boost社区组织开发.维护.在C++的地位感觉可以和Spring在Java中相比. boost向来有准标准库之称,很多新特性例如智 ...
- 删去k个数字后的最小值
public static String removeKDigits(String num,int k) { //新整数的最终长度=原长度 - k int newLength=num.length() ...