文章目录
- 一、数据库部分
- 1. 创建数据库
- 2. 初始化表结构
- 二、代码部分
- 2.1. controller
- 2.2. mapper接口
- 2.3. 映射文件
- 2.4. 参数封装
- 三、测试验证
- 3.1. 发起请求
- 3.2. 查看数据库
- 3.3. 配置文件部分
一、数据库部分
1. 创建数据库
创建more-insert
2. 初始化表结构
-- 一次性插入多张表测试
--oracle 表结构
create table HERO
(SNO VARCHAR2(20) not null,USER_NAME VARCHAR2(20),AGE NUMBER(3)
);alter table HEROadd primary key (SNO);create table HERO2
(SNO VARCHAR2(20) not null,USER_NAME VARCHAR2(20),AGE NUMBER(3)
);
alter table HERO2add primary key (SNO);
二、代码部分
声明:这里为了演示省略service层
2.1. controller
package com.gblfy.modular.order.controller;import com.gblfy.modular.order.mapper.MysqlMoreInsertMapper;
import com.gblfy.modular.order.model.request.OracleMoreInsertParam;
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 {@Autowiredprivate OraclelMoreInsertMapper oraclelMoreInsertMapper;@GetMapping("/oracleMoreInsert")public String oracleMoreInsert() {OracleMoreInsertParam oracleMoreInsertParam = new OracleMoreInsertParam();oracleMoreInsertParam.setSno("1");oracleMoreInsertParam.setUserName("Name1");oracleMoreInsertParam.setAge(1);oracleMoreInsertParam.setSno2("2");oracleMoreInsertParam.setUserName2("Name2");oracleMoreInsertParam.setAge2(2);oraclelMoreInsertMapper.oraclelmoreInsert(oracleMoreInsertParam);return "SUCCESS";}
}
2.2. mapper接口
package com.gblfy.modular.order.mapper;import com.baomidou.dynamic.datasource.annotation.DS;
import com.gblfy.modular.order.model.request.OracleMoreInsertParam;
import org.apache.ibatis.annotations.Param;/*** <p>* 通用Mapper 接口* </p>** @author gblfy* @since 2021-08-13*/
@DS("oracle_1")
public interface OraclelMoreInsertMapper {void oraclelmoreInsert(@Param("paramCondition") OracleMoreInsertParam paramCondition);}
mapper类上添加@DS注解是因为我使用了多数据源,不涉及可以删除此注解
2.3. 映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gblfy.modular.order.mapper.OraclelMoreInsertMapper"><insert id="oraclelmoreInsert" parameterType="com.gblfy.modular.order.model.request.OracleMoreInsertParam">begininsert into hero(SNO, USER_NAME, AGE) values (#{paramCondition.sno}, #{paramCondition.userName}, #{paramCondition.age});insert into hero2(SNO, USER_NAME, AGE) values (#{paramCondition.sno2}, #{paramCondition.userName2}, #{paramCondition.age2});end;</insert></mapper>
2.4. 参数封装
package com.gblfy.modular.order.model.request;import lombok.Data;import java.io.Serializable;/*** oracle演示对象** @author gblfy* @since 2021-08-13*/
@Data
public class OracleMoreInsertParam implements Serializable {private static final long serialVersionUID = 1L;private String sno;private String userName;private Integer age;private String sno2;private String userName2;private Integer age2;
}
三、测试验证
3.1. 发起请求
http://localhost/oracleMoreInsert
3.2. 查看数据库
3.3. 配置文件部分
########################################## 多数据源配置 ############################################
spring:datasource:dynamic:primary: master #设置默认的数据源或者数据源组,默认值即为masterstrict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源datasource:master:url: jdbc:mysql://localhost:3306/more-insert?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true&allowMultiQueries=trueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置filters: wall,mergeStatoracle_1:driver-class-name: oracle.jdbc.OracleDriverurl: jdbc:oracle:thin:@192.168.xxx.xxx:1521:orclusername: orclpassword: orclfilters: wall,mergeStat