前面一节,我们讲解了在MySQL下如何通过foreach批量插入数据,这一节,我们将介绍在Oracle下批量插入数据,也有两种方式。
完整的工程目录结构如下
在Oracle下创建数据库表t_emp,并创建序列
CREATE TABLE t_emp (
id number(10) NOT NULL,
emp_name varchar(50) DEFAULT NULL,
emp_email varchar(50) DEFAULT NULL,
dept_id number(10) DEFAULT NULL,
PRIMARY KEY (id)
);
==========================
create sequence SEQ_T_EMP_ID
minvalue 1
maxvalue 9999999999999999999999999999
start with 1
increment by 1
cache 20;
在mybatis-config.xml文件中增加Oracle数据源配置
修改db.properties配置文件,增加Oracle配置信息
oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=SCOTT
oracle.password=SCOTT
Emp实体类
public class Emp {
// ID,唯一性
private Integer id;
// 用户名
private String empName;
// 用户名
private String empEmail;
// 部门ID
private Integer deptId;
public Emp() {
}
public Emp(Integer id, String empName, String empEmail, Integer deptId) {
this.id = id;
this.empName = empName;
this.empEmail = empEmail;
this.deptId = deptId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpEmail() {
return empEmail;
}
public void setEmpEmail(String empEmail) {
this.empEmail = empEmail;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
@Override
public String toString() {
return "Emp [id=" + id + ", empName=" + empName + ", empEmail="
+ empEmail + ", deptId=" + deptId + "]";
}
}
EmpMapper接口类
public interface EmpMapper {
/**
* 根据部门ID查询该部门下的所有员工信息
* @param id
* @return
*/
public Emp batchSaveEmps(Emp emp);
}
新建UserMapper.xml文件,批量操作的主要核心代码配置在这个文件里面
新建MyBatisTest文件,增加testBatchSaveEmps方法
@Test
public void testBatchSaveEmps() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
List empList = new ArrayList();
empList.add(new Emp(null,"Lucy","[email protected]",1));
empList.add(new Emp(null,"Lily","[email protected]",2));
empList.add(new Emp(null,"Lizy","[email protected]",3));
mapper.batchSaveEmps(empList);
openSession.commit();
} finally {
openSession.close();
}
}
测试控制台打印结果如下
2017-08-13 18:12:17,569 [main] [com.queen.mybatis.mapper.EmpMapper.batchSaveEmps]-[DEBUG] ==> Preparing: begin insert into t_emp(id,emp_name,emp_email,dept_id) values (SEQ_T_EMP_ID.nextval, ?, ?, ?); insert into t_emp(id,emp_name,emp_email,dept_id) values (SEQ_T_EMP_ID.nextval, ?, ?, ?); insert into t_emp(id,emp_name,emp_email,dept_id) values (SEQ_T_EMP_ID.nextval, ?, ?, ?); end;
2017-08-13 18:12:17,722 [main] [com.queen.mybatis.mapper.EmpMapper.batchSaveEmps]-[DEBUG] ==> Parameters: Lucy(String), [email protected](String), 1(Integer), Lily(String), [email protected](String), 2(Integer), Lizy(String), [email protected](String), 3(Integer)
查询数据库看是否插入成功
SQL> select * from T_EMP t;
ID EMP_NAME EMP_EMAIL DEPT_ID
---- ---------------------------------- -----------------------
4 Lucy [email protected] 1
5 Lily [email protected] 2
6 Lizy [email protected] 3
如上,是Oracle的第一种批量插入方式,接下来我们介绍第二种批量插入方式
第二种方式,这个过程中要采用中间表的方式
修改UserMapper.xml文件
测试,控制台打印结果如下
2017-08-13 18:29:07,103 [main] [com.queen.mybatis.mapper.EmpMapper.batchSaveEmps]-[DEBUG] ==> Preparing: insert into t_emp(id,emp_name,emp_email,dept_id) select SEQ_T_EMP_ID.nextval, empName, empEmail, deptId from( select ? empName, ? empEmail, ? deptId from dual union select ? empName, ? empEmail, ? deptId from dual union select ? empName, ? empEmail, ? deptId from dual )
2017-08-13 18:29:07,262 [main] [com.queen.mybatis.mapper.EmpMapper.batchSaveEmps]-[DEBUG] ==> Parameters: Lucy123(String), [email protected](String), 1(Integer), Lily123(String), [email protected](String), 2(Integer), Lizy123(String), [email protected](String), 3(Integer)
2017-08-13 18:29:07,267 [main] [com.queen.mybatis.mapper.EmpMapper.batchSaveEmps]-[DEBUG] <== Updates: 3
注意:pom文件中要加入Oracle的maven依赖
com.oracle
ojdbc14
10.2.0.1.0
=======欢迎大家拍砖,小手一抖,多多点赞哟!=======
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。