Recently, I encountered a problem in the project: import data into the background and insert data into the database. The imported data amount has tens of thousands of pieces of data, and consider inserting data in batches;
Based on the online information, I wrote a small demo, and the demo download address was attached at the end of the article
1. Create a new project: The project directory structure is shown in the figure below, add the corresponding jar package
2. Create a new database table: 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. Create AccountInfo entity class:
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. Create a new interface mapping class: AccountInfoMapper.java
package com.oracle.mapper;import java.util.List;import com.oracle.entity.AccountInfo;public interface AccountInfoMapper { /** * Query all data* @return */ List<AccountInfo> queryAllAccountInfo(); /** * Batch insert data* * @param accountInfoList * @return */ int batchInsertAccountInfo(List<AccountInfo> accountInfoList);}5. Create mybatis configuration file: 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. Create an interface mapping configuration file: AccountInfoMapper.xml
Oracle's batch insertion database is different from MySQL.
MySQL:
The code copy is as follows: INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)values(,,,,,)(,,,,,,)
Oracle:
Copy the code as follows: INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE) (select 1,,,,,, from dual union all select 1,,,,,, from dual)
<?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"><!-- Full class name of interface--> <!-- type: Full class name of entity class--> <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 is consistent with the method name in the interface--> <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. Write test classes:
package com.oracle.test;import java.io.InputStream;import java.sql.Date;import java.util.ArrayList;import java.util.List;import java.util.List;import org.apache.ibatis.io.Resources;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("[email protected]"); 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("[email protected]"); 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("[email protected]"); account.setCreateDate(new Date(System.currentTimeMillis())); result.add(account); return result; }}Source code download: http://xiazai.VeVB.COM/201606/yuanma/java-oracle(VeVB.COM).rar
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.