Java實(shí)現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle
最近項(xiàng)目中遇到一個(gè)問(wèn)題:導(dǎo)入數(shù)據(jù)到后臺(tái)并將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中,導(dǎo)入的數(shù)據(jù)量有上萬(wàn)條數(shù)據(jù),考慮采用批量插入數(shù)據(jù)的方式;
結(jié)合網(wǎng)上資料,寫了個(gè)小demo,文章末尾附上demo下載地址
1、新建項(xiàng)目:項(xiàng)目目錄結(jié)構(gòu)如下圖所示,添加相應(yīng)的jar包
2、新建數(shù)據(jù)庫(kù)表: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實(shí)體類:
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 { /** * 查詢所有的數(shù)據(jù) * @return */ List<AccountInfo> queryAllAccountInfo(); /** * 批量插入數(shù)據(jù) * * @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的批量插入數(shù)據(jù)庫(kù)跟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:實(shí)體類的全類名 --> <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、編寫測(cè)試類:
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ORACLE大批量插入數(shù)據(jù)的詳細(xì)步驟
- Oracle批量插入數(shù)據(jù)的三種方式【推薦】
- C# Oracle批量插入數(shù)據(jù)進(jìn)度條的實(shí)現(xiàn)代碼
- MyBatis批量插入數(shù)據(jù)到Oracle數(shù)據(jù)庫(kù)中的兩種方式(實(shí)例代碼)
- 向Oracle數(shù)據(jù)庫(kù)的CLOB屬性插入數(shù)據(jù)報(bào)字符串過(guò)長(zhǎng)錯(cuò)誤
- .net實(shí)現(xiàn)oracle數(shù)據(jù)庫(kù)中獲取新插入數(shù)據(jù)的id的方法
- Oracle中帶條件插入數(shù)據(jù)的使用方法示例詳解
相關(guān)文章
Java并發(fā)編程之線程池實(shí)現(xiàn)原理詳解
池化思想是一種空間換時(shí)間的思想,期望使用預(yù)先創(chuàng)建好的對(duì)象來(lái)減少頻繁創(chuàng)建對(duì)象的性能開銷,java中有多種池化思想的應(yīng)用,例如:數(shù)據(jù)庫(kù)連接池、線程池等,下面就來(lái)具體講講2023-05-05springboot項(xiàng)目中controller層與前端的參數(shù)傳遞方式
這篇文章主要介紹了springboot項(xiàng)目中controller層與前端的參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10SpringBoot將所有依賴(包括本地jar包)打包到項(xiàng)目
這篇文章主要介紹了SpringBoot將所有依賴(包括本地jar包)打包到項(xiàng)目,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06Java設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解裝飾器模式
裝飾器模式又名包裝(Wrapper)模式。裝飾器模式以對(duì)客戶端透明的方式拓展對(duì)象的功能,是繼承關(guān)系的一種替代方案,本篇文章以虹貓藍(lán)兔生動(dòng)形象的為你帶來(lái)詳細(xì)講解2022-04-04SpringBoot實(shí)現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 查詢操作,文中有詳細(xì)的代碼示例和操作流程,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07深入探究SpringBoot中的Elasticsearch自動(dòng)配置原理及用法
SpringBoot中的Elasticsearch自動(dòng)配置為我們提供了一種快速集成Elasticsearch的方式,使我們可以在SpringBoot應(yīng)用程序中輕松地使用Elasticsearch,本文將介紹Spring Boot中的Elasticsearch自動(dòng)配置的作用、原理和使用方法2023-07-07分享幾個(gè)Java工作中實(shí)用的代碼優(yōu)化技巧
這篇文章主要給大家分享幾個(gè)Java工作中實(shí)用代碼優(yōu)化技巧,文章基于Java的相關(guān)資料展開對(duì)其優(yōu)化技巧的分享,需要的小伙伴可以參考一下2022-04-04java拼接字符串時(shí)去掉最后一個(gè)多余逗號(hào)的方法
這篇文章主要介紹了java拼接字符串時(shí)去掉最后一個(gè)多余逗號(hào)的方法,實(shí)例分析了java操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Java實(shí)現(xiàn)ftp文件上傳下載解決慢中文亂碼多個(gè)文件下載等問(wèn)題
這篇文章主要介紹了Java實(shí)現(xiàn)ftp文件上傳下載解決慢中文亂碼多個(gè)文件下載等問(wèn)題的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10