mybatis 批处理 oracle

2017-01-03 19:12:56来源:CSDN作者:yuhushangwei人点击

1:定义接口;

public interface Test {    public void insertTest(TestBean testBean);}

2:定义bean;

public class TestBean {    private int name;    private String pass;    public int getName() {        return name;    }    public void setName(int name) {        this.name = name;    }    public String getPass() {        return pass;    }    public void setPass(String pass) {        this.pass = pass;    }    @Override    public String toString() {        return "TestBean [name=" + name + ", pass=" + pass + "]";    }}

3:定义映射文件;

<?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="bingan.Test">    <insert id="insertTest" parameterType="testBean">        insert into test(name, pass) values (#{name}, #{pass, jdbcType=CLOB})    </insert></mapper>

4:定义主函数;

public class Main {    public static void main(String[] args) {        SqlSessionFactory sqlSessionFactory = null;        Reader reader=null;        try{            reader = Resources.getResourceAsReader("mybatis-config2.xml");            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);        }catch(Exception e){            e.printStackTrace();        }        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);        Test test = sqlSession.getMapper(Test.class);        long startTime = System.currentTimeMillis();        int batchCount = 500;        for(int i=0; i<1500;++i){            TestBean bean = new TestBean();            bean.setName(i);            bean.setPass("yuanyuhu"+i);            test.insertTest(bean);            if(i!=0 && i%batchCount==0){                sqlSession.commit();            }        }        sqlSession.commit();        long endtime = System.currentTimeMillis()-startTime;        System.out.println("耗时"+endtime+"ms");        sqlSession.close();    }}

6:配置文件;

<?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>    <properties resource="jdbc.properties"/>    <typeAliases>        <typeAlias alias="testBean" type="bingan.TestBean" />    </typeAliases>    <environments default="oracle">        <environment id="oracle">            <transactionManager type="JDBC" />            <dataSource type="POOLED">                <property name="driver" value="${driver}" />                <property name="url" value="${url}" />                <property name="username" value="${username}" />                <property name="password" value="${password}" />            </dataSource>        </environment>    </environments>    <mappers>        <mapper resource="bingan/Test.xml" />    </mappers></configuration>

7:资源文件jdbc.properties;

#oracle data sourcedriver=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@127.0.0.1:1521:orclusername=binganpassword=yyh123

配置完就可以使用了。

补充建表语句:

create table TEST(  name NUMBER,  pass CLOB);

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台