최근 프로젝트에서 문제가 발생했습니다. 데이터를 백그라운드로 가져 와서 데이터베이스에 데이터를 삽입했습니다. 가져온 데이터 금액에는 수만 개의 데이터가 있으며 배치에 데이터를 삽입하는 것을 고려합니다.
온라인 정보를 기반으로 작은 데모를 썼고 데모 다운로드 주소는 기사 끝에 첨부되었습니다.
1. 새 프로젝트 만들기 : 프로젝트 디렉토리 구조가 아래 그림에 표시되어 있으며 해당 JAR 패키지 추가
2. 새 데이터베이스 테이블을 만듭니다 : Account_Info
테이블 ac
3. AccountInfo Entity 클래스 작성 :
패키지 com.oracle.entity; import java.sql.date; public class AccountInfo {private long id; 개인 문자열 사용자 이름; 개인 문자열 비밀번호; 개인 문자열 성별; 개인 문자열 이메일; 생성 된 개인 날짜; 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 (문자열 비밀번호) {this.password = password; } public String getGender () {return gender; } public void setgender (문자열 성별) {this.gender = 성별; } public String getEmail () {반환 이메일; } public void seteMail (문자열 이메일) {this.email = 이메일; } public Date getCreatedate () {return createate; } public void setCreatedate (날짜 createate) {this.createdate = createate; } @override public String toString () {return "return"return "ad }} 4. 새 인터페이스 매핑 클래스를 만듭니다 : AccountInfomapper.java
package com.oracle.mapper; import java.util.list; import com.oracle.entity.accountinfo; public interface ac / ** * 배치 삽입 데이터 * * @param AccountInfolist * @return */ int batchinsertaccountinfo (list <coundinfo> accountinfolist);}
5. MyBatis 구성 파일 생성 : MyBatis-configuration.xml
<? xml version = "1.0"alcoding = "utf-8"?> <! doctype configuration public "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> configuration> <환경 ""> 개발 "> 개발"> <transactionManager type = "jdbc" /> <dataSource type = "pooled"> <property name = "driver"value = "oracle.jdbc.driver.oracledriver" /> <property name = "url"value = "jdbc : oracle : thin : @localhost : 1521 : orcl" /> <sporation name = "username" "xxx" " value = "xxx"/> </datasource> </환경> </환경> <mappers> <Mapper resource = "config/accountinfomapper.xml"/> </mappers> </configuration>
6. 인터페이스 매핑 구성 파일을 만듭니다 : AccountInfomapper.xml
Oracle의 배치 삽입 데이터베이스는 MySQL과 다릅니다.
mysql :
코드 사본은 다음과 같습니다. 컨퍼런스 _info (id, username, 암호, 성별, 이메일, create_date) 값 (,,,,) (,,,,,,)에 삽입
신탁:
다음과 같이 코드를 복사하십시오. ac
<? xml version = "1.0"alcoding = "utf-8"?> <! doctype mapper public "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namper"somplaspom.mapper.mapper.mapper.mapper.mapper.mapper. 인터페이스의 이름-> <!-유형 : Entity 클래스의 전체 클래스 이름-> <resultmap id = "baseresultMap"type = "com.oracle.entity.accountInfo"> <id "property ="id "jdbctype ="decimal " /> <result column ="username "propertion ="username ""username ""username ""username " 속성 = "비밀번호"jdbctype = "varchar" /> <result column = "gender"propertion = "gender"jdbctype = "char" /> <result column = "email"property = "email"jdbctype = "varchar" /> <결과 열 = "create_date"propert = "crade" 인터페이스-> <select id = "QueryAllAcCountInfo"QueryAllAcCountInfo "resultMap ="baserEsultMap "> select id, username, password, gender, email, select> <insert id ="batchinsertaccountInfo "parameterType ="java.util.list "> insert in and useS, insert" 이메일, create_date) (<foreach collection = "list"index = ""item = "ac 듀얼 </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 java.util.list; import org.apache.apache.io.resources; import org.apache.ibatis.sqlsesionession import; org.apache.ibatis.session.sqlsessionfactorybuilder; import com.oracle.entity.accountinfo; import com.oracle.mapper.accountinfomapper; public class mybatistest {public static void main (strings) 예외 {string resource = "config/mybatis-configuration. inputStream inputStream = resources.getResourCeasStream (resource); sqlsessionFactory sessionFactory = 새로운 SQLSESSIONFACTORYBUILDER () .BUILD (inputStream); sqlsession session = sessionfactory.opensession (); AccountInfomapper Mapper = session.getMapper (AccountInfomapper.class); 목록 <coundinfo> AccountInfolist = Mapper.queryAllAccountInfo (); if (ac } else {for (ac }} mapper.batchinsertaccountinfo (gendationata ()); 세션 .commit (); } static list <coundinfo> gendationata () {list <countinfo> result = new ArrayList <AccountInfo> (); AccountInfo Account = New AccountInfo (); 계정 .setId (3L); ac 계정 .setPassword ( "123456"); 계정 .SetGender ( "1"); 계정 .seteMail ( "[email protected]"); ac result.add (계정); 계정 = new AccountInfo (); 계정 .setId (4L); ac 계정 .setPassword ( "Zhangwuji"); 계정 .SetGender ( "0"); 계정 .seteMail ( "[email protected]"); ac result.add (계정); 계정 = new AccountInfo (); 계정 .setId (5L); account.setusername ( "zhaomin"); 계정 .setPassword ( "Zhangwuji"); 계정 .SetGender ( "0"); 계정 .seteMail ( "[email protected]"); ac result.add (계정); 반환 결과; }}소스 코드 다운로드 : http://xiazai.vevb.com/201606/yuanma/java-oracle(Vevb.com).rar
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.