mybatisplus⼀次性执⾏多条SQL语句插⼊(Mysql篇)⽂章⽬录
⼀、数据库部分
1. 创建数据库
创建more-insert
2. 初始化表结构
-- ⼀次性插⼊多张表测试
--mysql 表结构
CREATE TABLE `sys_user2`
(
`user_id` bigint NOT NULL COMMENT '主键id',
`account` varchar(45) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '账号',
`name`    varchar(45) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '名字',
PRIMARY KEY (`user_id`) USING BTREE
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC COMMENT='管理员表2'; CREATE TABLE `sys_user5`
(
`user_id` bigint NOT NULL COMMENT '主键id',
`account` varchar(45) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '账号',
`name`    varchar(45) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '名字',
PRIMARY KEY (`user_id`) USING BTREE
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC COMMENT='管理员表5';
⼆、代码部分
声明:这⾥为了演⽰省略service层
2.1. controller
package ller;
import der.mapper.MysqlMoreInsertMapper;
import quest.MysqlMoreInsertParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MoreController {
@Autowired
private MysqlMoreInsertMapper mysqlmoreInsert;
@GetMapping("/mysqlMoreInsert")
public String mysqlMoreInsert(){
MysqlMoreInsertParam moreInsertParam = new MysqlMoreInsertParam();
moreInsertParam.setUserId2(2L);
moreInsertParam.setAccount2("Account2");
moreInsertParam.setName2("name2");
moreInsertParam.setUserId5(5L);
moreInsertParam.setAccount5("Account5");
moreInsertParam.setName5("name5");
return"SUCCESS";
}
}
2.2. mapper接⼝
package der.mapper;
import quest.MysqlMoreInsertParam;
import org.apache.ibatis.annotations.Param;
/**
* <p>
* 英雄信息表通⽤ Mapper 接⼝
* </p>
param name
*
* @author gblfy
* @since 2021-08-13
*/
public interface MysqlMoreInsertMapper {
void mysqlmoreInsert(@Param("paramCondition") MysqlMoreInsertParam paramCondition);
}
2.3. 映射⽂件
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"> <mapper namespace="der.mapper.MysqlMoreInsertMapper">
<insert id="mysqlmoreInsert"parameterType="quest.MysqlMoreInsertParam">        insert into sys_user2(user_id, account, name)
values (#{paramCondition.userId2}, #{paramCondition.account2}, #{paramCondition.name2});
insert into sys_user5(user_id, account, name)
values (#{paramCondition.userId5}, #{paramCondition.account5}, #{paramCondition.name5});
</insert>
</mapper>
2.4. 参数封装
package quest;
import lombok.Data;
import java.io.Serializable;
/**
* <p>
* Mysql通⽤对象
* </p>
*
* @author gblfy
* @since 2021-08-13
*/
@Data
public class MysqlMoreInsertParam implements Serializable {
private static final long serialVersionUID = 1L;
private Long userId2;
private String account2;
private String name2;
private Long userId5;
private String account5;
private String name5;
}
三、测试验证
3.1. 发起请求
3.2. 查看数据库
3.3. 配置⽂件部分
>>>>>>>>## 多数据源配置 >>>>>>>>####
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使⽤默认数据源
datasource:
master:
url: jdbc:mysql://localhost:3306/more-insert?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_T O_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true&allowMultiQueries=true
username: root
password: 123456
driver-class-name: sql.cj.jdbc.Driver # 3.2.0开始⽀持SPI可省略此配置
filters: wall,mergeStat
oracle_1:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@x:1521:orcl
username: orcl
password: orcl
filters: wall,mergeStat

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论