Java實現(xiàn)mybatis批量插入數據到Oracle
最近項目中遇到一個問題:導入數據到后臺并將數據插入到數據庫中,導入的數據量有上萬條數據,考慮采用批量插入數據的方式;
結合網上資料,寫了個小demo,文章末尾附上demo下載地址
1、新建項目:項目目錄結構如下圖所示,添加相應的jar包

2、新建數據庫表:ACCOUNT_INFO
CREATE TABLE ACCOUNT_INFO ( "ID" NUMBER(12) NOT NULL , "USERNAME" VARCHAR2(64 BYTE) NULL , "PASSWORD" VARCHAR2(64 BYTE) NULL , "GENDER" CHAR(1 BYTE) NULL , "EMAIL" VARCHAR2(64 BYTE) NULL , "CREATE_DATE" DATE NULL )
3、創(chuàng)建AccountInfo實體類:
package com.oracle.entity;
import java.sql.Date;
public class AccountInfo {
private Long id;
private String userName;
private String password;
private String gender;
private String email;
private Date createDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "AccountInfo [id=" + id + ", userName=" + userName
+ ", password=" + password + ", gender=" + gender + ", email="
+ email + ", createDate=" + createDate + "]";
}
}
4、新建接口映射類:AccountInfoMapper.java
package com.oracle.mapper;
import java.util.List;
import com.oracle.entity.AccountInfo;
public interface AccountInfoMapper {
/**
* 查詢所有的數據
* @return
*/
List<AccountInfo> queryAllAccountInfo();
/**
* 批量插入數據
*
* @param accountInfoList
* @return
*/
int batchInsertAccountInfo(List<AccountInfo> accountInfoList);
}
5、創(chuàng)建mybatis配置文件:mybatis-configuration.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="xxx" /> <property name="password" value="xxx" /> </dataSource> </environment> </environments> <mappers> <mapper resource="config/AccountInfoMapper.xml" /> </mappers> </configuration>
6、創(chuàng)建接口映射配置文件:AccountInfoMapper.xml
Oracle的批量插入數據庫跟MySQL不一樣,
MySQL:
Oracle:
<?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.oracle.mapper.AccountInfoMapper"><!-- 接口的全類名 -->
<!-- type:實體類的全類名 -->
<resultMap id="BaseResultMap" type="com.oracle.entity.AccountInfo">
<id column="ID" property="id" jdbcType="DECIMAL" />
<result column="USERNAME" property="userName" jdbcType="VARCHAR" />
<result column="PASSWORD" property="password" jdbcType="VARCHAR" />
<result column="GENDER" property="gender" jdbcType="CHAR" />
<result column="EMAIL" property="email" jdbcType="VARCHAR" />
<result column="CREATE_DATE" property="createDate" jdbcType="DATE" />
</resultMap>
<!-- id 跟接口中的方法名稱保持一致 -->
<select id="queryAllAccountInfo" resultMap="BaseResultMap">
select ID,
USERNAME,PASSWORD,
GENDER, EMAIL, CREATE_DATE from ACCOUNT_INFO
</select>
<insert id="batchInsertAccountInfo" parameterType="java.util.List">
INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)
(
<foreach collection="list" index="" item="accountInfo"
separator="union all">
select
#{accountInfo.id},
#{accountInfo.userName},
#{accountInfo.password},
#{accountInfo.gender},
#{accountInfo.email},
#{accountInfo.createDate}
from dual
</foreach>
)
</insert>
</mapper>
7、編寫測試類:
package com.oracle.test;
import java.io.InputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.oracle.entity.AccountInfo;
import com.oracle.mapper.AccountInfoMapper;
public class MybatisTest {
public static void main(String[] args) throws Exception {
String resource = "config/mybatis-configuration.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
SqlSession session = sessionFactory.openSession();
AccountInfoMapper mapper = session.getMapper(AccountInfoMapper.class);
List<AccountInfo> accountInfoList = mapper.queryAllAccountInfo();
if (accountInfoList == null) {
System.out.println("The result is null.");
} else {
for (AccountInfo personInfo : accountInfoList) {
System.out.println(personInfo);
}
}
mapper.batchInsertAccountInfo(generateData());
session.commit();
}
static List<AccountInfo> generateData(){
List<AccountInfo> result = new ArrayList<AccountInfo>();
AccountInfo account = new AccountInfo();
account.setId(3L);
account.setUserName("zhangsanfeng");
account.setPassword("123456");
account.setGender("1");
account.setEmail("zhangsanfeng@wudang.com");
account.setCreateDate(new Date(System.currentTimeMillis()));
result.add(account);
account = new AccountInfo();
account.setId(4L);
account.setUserName("zhouzhiruo");
account.setPassword("zhangwuji");
account.setGender("0");
account.setEmail("zhouzhiruo@emei.com");
account.setCreateDate(new Date(System.currentTimeMillis()));
result.add(account);
account = new AccountInfo();
account.setId(5L);
account.setUserName("zhaomin");
account.setPassword("zhangwuji");
account.setGender("0");
account.setEmail("zhaomin@yuan.com");
account.setCreateDate(new Date(System.currentTimeMillis()));
result.add(account);
return result;
}
}
源碼下載:http://xiazai.jb51.net/201606/yuanma/java-oracle(jb51.net).rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
springboot項目中controller層與前端的參數傳遞方式
這篇文章主要介紹了springboot項目中controller層與前端的參數傳遞方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
SpringBoot將所有依賴(包括本地jar包)打包到項目
這篇文章主要介紹了SpringBoot將所有依賴(包括本地jar包)打包到項目,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
SpringBoot實現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 
這篇文章主要給大家介紹了SpringBoot如何實現(xiàn)elasticsearch 查詢操作,文中有詳細的代碼示例和操作流程,具有一定的參考價值,需要的朋友可以參考下2023-07-07
深入探究SpringBoot中的Elasticsearch自動配置原理及用法
SpringBoot中的Elasticsearch自動配置為我們提供了一種快速集成Elasticsearch的方式,使我們可以在SpringBoot應用程序中輕松地使用Elasticsearch,本文將介紹Spring Boot中的Elasticsearch自動配置的作用、原理和使用方法2023-07-07
Java實現(xiàn)ftp文件上傳下載解決慢中文亂碼多個文件下載等問題
這篇文章主要介紹了Java實現(xiàn)ftp文件上傳下載解決慢中文亂碼多個文件下載等問題的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10

