首页 > 数据库 >mybatis批量update(mysql)

mybatis批量update(mysql)

时间:2022-12-28 10:33:17浏览次数:45  
标签:String tableName update Param param mysql mybatis originalTableName newTableName

批量插入:

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

这样写总是报错,调试了很长时间也没找到问题原因

最后找到这里 找到了答案

 

数据库的链接必须加上但是数据库连接必须加上 allowMultiQueries=true

url="jdbc:mysql://localhost:3306/testDatabase?allowMultiQueries=true" />


利用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语法要求的一部分,而不是参数


标签:String,tableName,update,Param,param,mysql,mybatis,originalTableName,newTableName
From: https://blog.51cto.com/u_15147537/5973851

相关文章

  • Mybatis 的<trim>标签用法 prefix prefixOverrides suffix suffixOverrides
    <trimprefix=""suffix=""suffixOverrides=""prefixOverrides=""></trim>prefix:在trim标签内sql语句加上前缀。prefixOverrides:指定去除多余的前缀内容如:prefixOve......
  • 【SSM框架】MyBatis核心配置文件详解
    1.MyBatis核心配置文件之environments<!--environments:配置多个连接数据库的环境属性:default:设置默认使用的环境的id--><environmentsdefault="development"><!......
  • MySql索引下推知识分享
    作者:刘邓忠Mysql是大家最常用的数据库,下面为大家带来mysql索引下推知识点的分享,以便巩固mysql基础知识,如有错误,还请各位大佬们指正。1什么是索引下推索引下推(In......
  • docker搭建Elasticsearch、Kibana、Logstash 同步mysql数据到ES
    一、前言在数据量大的企业级实践中,Elasticsearch显得非常常见,特别是数据表超过千万级后,无论怎么优化,还是有点力不从心!使用中,最首先的问题就是怎么把千万级数据同步到Elast......
  • mysql相关优化配置
    [mysqld]#mysql数据存储目录datadir=/data/mysql#socket存储socket=/tmp/mysqld.sock#当一个磁盘或分区空间不够时,可以将数据存储到其他的磁盘或分区symbolic-links=......
  • 基于KeepAlived的MySQL主主互备模式的高可用
    架构图主要设计思路是通过MySQLReplication技术将两台MySQLServer互相将对方作为自己的Master,自己又同时作为对方的Slave来进行复制。这样就实现了高可用构架中的数据......
  • Mysql数据库备份工具xtrabackup
    安装xtrabackup工具包几个percona官方yum源http://repo.percona.com/centos/https://www.percona.com/downloads/percona-release/下载yum源https://www.percona.......
  • mybatis报错
     经研究发现是少了依赖:<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><......
  • mysql授权
     授权被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户 mysql>GRANTALLON*.*TO'root'@'192.168.59.129'IDENTIFIED......
  • MySQL 之 单表查询
    一.简单查询--创建表DROPTABLEIFEXISTS`person`;CREATETABLE`person`(`id`int(11)NOTNULLAUTO_INCREMENT,`name`varchar(50)NOTNULL,`age`tinyint(4)......