mybatis批量update(mysql)
来源:互联网 发布:dnf网络出现异常09 编辑:程序博客网 时间:2024/06/01 03:58
批量插入:
<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
阅读全文
0 0
- mysql mybatis 批量update
- mybatis批量update(mysql)
- mybatis批量update(mysql)
- mybatis批量执行mysql的update操作
- mybatis 批量insert,update
- mybatis批量update
- mybatis批量update
- mybatis 批量insert , update
- mysql数据库的批量update,insert 在mybatis中
- oracle和mysql数据库的批量update在mybatis
- mysql 批量update
- mysql批量update
- mybatis执行批量更新update
- mybatis执行批量更新update
- Mybatis学习--批量update语句
- mybatis批量update,insert,delete
- mybatis 批量更新update详解
- mybatis+oracle+批量insert批量update
- 如何在tomcat里为多个应用配置不同的访问端口
- java反射机制判断对象所有属性是否全部为空
- http常见状态码以及其代表的含义
- 操作系统学习指南(一)
- Android热修复(一):Tinker的使用(一)命令行接入
- mybatis批量update(mysql)
- git 分支合并问题:refusing to merge unrelated histories
- JSP标签详细使用
- Android获取控件坐标为0的原因及取得控件位置和两控件距离的方法
- 继承TextView来写一个BadgeView(数字提醒)
- 数据结构JavaScript描述(二)
- windows10下安装opencv3.2+QT Creator+MingGW
- 数字0-9的数量
- 如何在Delphi中用代码来完成计算字段的创建