关闭

mybatis 批处理 oracle

标签: mybatis批处理oracle
257人阅读 评论(0) 收藏 举报
分类:

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 source
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=bingan
password=yyh123

配置完就可以使用了。

补充建表语句:

create table TEST
(
  name NUMBER,
  pass CLOB
)
;
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:58316次
    • 积分:1511
    • 等级:
    • 排名:千里之外
    • 原创:89篇
    • 转载:57篇
    • 译文:0篇
    • 评论:0条