mybatis批量update(mysql)
批量插入:
<insert id="batchInsert">
insert into testTable (id,content)
values
<foreach collection="list" item="item" index="index" separator="," >
(
#{item.id},
#{item.content},
)
</foreach>
</insert>
Mapper文件中的写法
<insert id="batchUpdateTjData">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
UPDATE test_table
SET c_a = #{item.ca},
c_b = #{item.cb}
WHERE id = #{item.id}
</foreach>
</insert>
这样写总是报错,调试了很长时间也没找到问题原因
最后找到这里http://my.oschina.net/jsonavaj/blog/265112 找到了答案
数据库的链接必须加上但是数据库连接必须加上 allowMultiQueries=true
url="jdbc:mysql://localhost:3306/testDatabase?allowMultiQueries=true" />
http://www.cnblogs.com/modprobe/p/4683274.html
利用MyBatis对数据库进行DDL(create table,drop table等等)操作
【具体代码】
1、mapper接口文件内容如下
/**
* 执行备份数据库相关表的Mapper
*/
public interface BackupDataMapper { /**
* 修改数据库的表名字
* @param originalTableName
* @param newTableName
* @return
*/
int alterTableName(@Param("originalTableName") String originalTableName,
@Param("newTableName") String newTableName); /**
* truncate指定数据库表的数据
* @param tableName
* @return
*/
int truncateTable(@Param("tableName") String tableName); /**
* 根据传入的表明,创建新的表并且将原表的数据插入到新的Occur表中
* @param newTableName
* @param originalTableName
*/
void createNewTableAndInsertData(@Param("newTableName") String newTableName,
@Param("originalTableName") String originalTableName); /**
* 统计某张表中的总数据条数。
* @param tableName
* @return 指定表中的总记录条数。
*/
int getRecordCount(@Param("tableName") String tableName); /**
* 获得当前数据库的名字
* @return
*/
String getCurDataBaseName(); /**
* 从指定数据库中,查询是否存在某张表
* @param dataBaseName
* @param tableName
* @return
*/
String isTargetTableExistInDB(@Param("dataBaseName") String dataBaseName,
@Param("tableName") String tableName);
}
2、mapper.xml文件内容如下
<mapper namespace="com.dao.BackupDataMapper">
<update id="alterTableName">
alter table ${originalTableName} rename ${newTableName}
</update>
<update id="truncateTable">
truncate table ${tableName}
</update>
<update id="createNewTableAndInsertData">
create table ${newTableName} as select * from ${originalTableName}
</update>
<select id="getRecordCount" resultType="int">
select count(1) from ${tableName}
</select>
<select id="getCurDataBaseName" resultType="string">
select database();
</select>
<select id="isTargetTableExistInDB" resultType="string">
SELECT table_name FROM information_schema.tables WHERE table_schema = #{dataBaseName} and TABLE_NAME = #{tableName}
</select>
</mapper>
3、注意点
在传入“表名”作为参数时,一定要使用“${tableName}”的格式,而不能使用“#{tableName}”的格式。因为表名是sql语法要求的一部分,而不是参数
http://www.cnblogs.com/zjrodger/p/5567085.html
mybatis批量update(mysql)的更多相关文章
- mybatis批量update操作的写法,及批量update报错的问题解决方法
mybatis的批量update操作写法很简单,如下: public interface YourMapper extends BaseMapper<YourExt> { void upd ...
- mybatis批量update,返回行数为-1
mybatis批量更新返回结果为-1,是由于mybatis的defaultExExecutorType引起的, 它有三个执行器:SIMPLE 就是普通的执行器:REUSE 执行器会重用预处理语句 ...
- mybatis 批量update报语法错误解决方法
1.为什么会报语法错误 原因:在 *.xml文件内使用了循环,在mybatis中默认是不允许使用批量修改. <update id="setMaxMin" parameterT ...
- mybatis 批量update两种方法对比
<!-- 这次用resultmap接收输出结果 --> <select id="findByName" parameterType="string&qu ...
- mybatis批量更新策略
我们知道循环中操作db会导致连接数满,严重影响数据库性能.所以在对db进行DQL与DML时,根据业务逻辑尽量批量操作,这里我们介绍下使用mybatis批量更新mysql的两种方式. 方式一: < ...
- mybatis批量更新 UPDATE mysql
oracle和mysql数据库的批量update在mybatis中配置不太一样: oracle数据库: <update id="batchUpdate" parameterT ...
- MyBatis魔法堂:各数据库的批量Update操作
一.前言 MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...
- Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案
转自http://www.cnblogs.com/fnz0/p/5713102.html 不知道自己什么时候才有这种钻研精神- -. 1 背景 系统中需要批量生成单据数据到数据库表,所以采用 ...
- mybatis批量更新update-设置多个字段值 报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
mybatis批量更新update-设置多个字段值 2016年08月01日 12:49:26 姚一号 阅读数:29539 标签: mysql mybatis批量更新批量更新allowMultiQuer ...
随机推荐
- usaco silver
大神们都在刷usaco,我也来水一水 1606: [Usaco2008 Dec]Hay For Sale 购买干草 裸背包 1607: [Usaco2008 Dec]Patting Heads 轻 ...
- 为什么新建的管理员账号权限没有Administrator大?
Administrator是超级管理员,UAC不用确认,跟关了一样. 新建隶属于administrator组的用户,可以关掉UAC. 控制面板>系统和安全>操作中心>更改用户帐户控制 ...
- <php>统计目录数和文件数
$dirn = 0; //目录数 $filen = 0; //文件数 //用来统计一个目录下的文件和目录的个数 function getdirnum($file) { global $dirn; gl ...
- poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))
Description The SUM problem can be formulated . In the following, we assume that all lists have the ...
- 阿里云安装docker
选centos6.5输入操作系统 yum install docker-io docker -d 提示没有备用IP地址可以用来桥接卡 接下来的网卡中编辑eth0 DEVICE=eth0 ONBOOT ...
- 支持多QQ登录的软件
支持多QQ登录,批量加好友,批量回复QQ消息,当然也能接收 下载链接:多QQ登录软件
- ubuntu16.04 server安装小记
由于本人有一台闲置的thinkpad电脑,所以打算在上边安装一个ubuntu16.04 server版本,其中遇到主要问题,做一下记录: 安装过程中出现“ubuntu16.04 server64 bu ...
- Stm32高级定时器(一)
Stm32高级定时器(一) 1 定时器的用途 2 高级定时器框图 3 时基单元 4 通道 1 定时器的用途 已知一个波形求另一个未知波形(信号长度和占空比) 已知波形的信号长度和占空比产生一个相应的波 ...
- c# 鼠标操作
1#region 3using System; 4using System.Runtime.InteropServices; 6#endregion 8namespace Windows.Forms. ...
- HTML与CSS入门——第九章 使用颜色
知识点: 1.为网站选择颜色的方法 2.颜色在Web上的工作方式 3.使用十六进制颜色值的方法 4.使用CSS设置背景.文本和边框颜色的方法 9.1 选择颜色的最佳方法: 直白地说:根据用户群体找到最 ...