批量插入:

  <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)的更多相关文章

  1. mybatis批量update操作的写法,及批量update报错的问题解决方法

    mybatis的批量update操作写法很简单,如下: public interface YourMapper extends BaseMapper<YourExt> { void upd ...

  2. mybatis批量update,返回行数为-1

    mybatis批量更新返回结果为-1,是由于mybatis的defaultExExecutorType引起的,    它有三个执行器:SIMPLE 就是普通的执行器:REUSE 执行器会重用预处理语句 ...

  3. mybatis 批量update报语法错误解决方法

    1.为什么会报语法错误 原因:在 *.xml文件内使用了循环,在mybatis中默认是不允许使用批量修改. <update id="setMaxMin" parameterT ...

  4. mybatis 批量update两种方法对比

    <!-- 这次用resultmap接收输出结果 --> <select id="findByName" parameterType="string&qu ...

  5. mybatis批量更新策略

    我们知道循环中操作db会导致连接数满,严重影响数据库性能.所以在对db进行DQL与DML时,根据业务逻辑尽量批量操作,这里我们介绍下使用mybatis批量更新mysql的两种方式. 方式一: < ...

  6. mybatis批量更新 UPDATE mysql

    oracle和mysql数据库的批量update在mybatis中配置不太一样: oracle数据库: <update id="batchUpdate" parameterT ...

  7. MyBatis魔法堂:各数据库的批量Update操作

    一.前言   MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...

  8. Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案

    转自http://www.cnblogs.com/fnz0/p/5713102.html 不知道自己什么时候才有这种钻研精神- -. 1      背景 系统中需要批量生成单据数据到数据库表,所以采用 ...

  9. 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 ...

随机推荐

  1. rpm -qs查看包信息

    rpm -qs mysql-connector-c-devel Query options (with -q or --query):  -c, --configfiles               ...

  2. T-SQL函数类别统计

  3. 【转】Math.Atan2 方法

    原文网址:https://msdn.microsoft.com/zh-cn/library/system.math.atan2.aspx 返回正切值为两个指定数字的商的角度. 命名空间:  Syste ...

  4. easyui获取日期datebox中的值

    <input type="text" class="easyui-datebox" id="CTIME" style="wi ...

  5. HDU 3697 Selecting courses(贪心)

    题目链接:pid=3697" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=3697 Prob ...

  6. 绘制更Smooth的UI

    以前很长一段时间,在自定义控制绘制时,只是简单的定义一个QPainter对象而开始绘画.经常会画一些圆角矩形,甚至是一些不规则的图形.对于不规则的图形来说,如果PS技术不好,或者mask制作的不好,常 ...

  7. io系统

    一.浅谈io系统 io系统的结构化思想是:输入-转换流-装饰器-输出. 对于字节流来说,常见的结构类为: package com.handchina.yunmart.middleware.servic ...

  8. JavaScript 函数方法 - bind()

    Function.prototype.bind() ECMAScript5中新增的方法,但是在ECMAScript3可以通过模仿实现其方法作用 作用: bind() 方法会创建一个新函数,当这个新函数 ...

  9. web.config中的profile

    aspnet_regsql命令创建需要的表结构 public class UserProfile:ProfileBase { [SettingsAllowAnonymous(true)] //默认匿名 ...

  10. 服务器安装VMware ESXI5.5

    一.VMware ESXI5.5 vSphere5.5 VIMSetup下载http://blog.sina.com.cn/s/blog_61c07ac50101gy64.html(1)VMware安 ...