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 ...
随机推荐
- Android 编程:calledfromWrongThreadException 的原因
子线程更新UI会发生Android.view.ViewRoot$CalledFromWrongThreadException异常的解决方法 子线程更新UI 显然假如你的程序需要执行耗时的操作的话,假如 ...
- webservice的几个简单概念
1.什么是JAX-WS? http://baike.baidu.com/view/1865210.htm?fr=aladdin 2.什么是JAX-RPC? http://baike.baidu.com ...
- linux磁盘限额配置:quota命令
LINUX下也有类似WINDOWS NTFS所用的磁盘限额,用的是quota来实现通过rpm -q quota确定是否已安装用quota只能对patation做限额,要做到针对某个目录来做只能靠ln ...
- cf413E Maze 2D
E. Maze 2D time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforce 217 div2
C 假设每种颜色的个数都相同,可以用轮换的方式,让答案达到最大n,当不同的时候,可以每次从每种颜色中取出相同个数的手套来操作; 一直迭代下去直到只剩下1种颜色; 再将这一种颜色与之前交换过的交换就行了 ...
- Java中BigDecimal的8种舍入模式是怎样的
Java中BigDecimal的8种舍入模式是怎样的?下面长沙欧柏泰克软件学院和大家一起来学习下吧: java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecima ...
- Android Fragment详解(三): 实现Fragment的界面
为fragment添加用户界面: Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layout ...
- Java清洁:终结处理和垃圾回收
一般情况:Java有垃圾回收机制负责回收无用对象占据的内存资源. 特殊情况:假定你的对象(并非使用new)获得一块特殊的内存区域,由于垃圾回收器只知道释放那些经由new分配的内存,所以它不知道如何释放 ...
- scala中的implict
1.作为隐式参数 object Test { def main(args: Array[String]) { import FruitColor._ Fruit.get("apple&quo ...
- WCF相关
1.WCF初探-1:认识WCF(概览)2.WCF初探-2:手动实现WCF程序3.WCF精通系列4.无废话WCF系列教程