比较JDBC和Mybatis进行批处理时的性能比较

执行批量操作时,做了个比较Mybatis和JDBC执行时间的记录,JDBC还是强大太多了!


jdbc.properties文件

jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true
jdbc.driverName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123

其中,数据库连接中的参数rewriteBatchedStatements相当重要,如果不开启rewriteBatchedStatements=true,那么jdbc会把批量插入当做一行行的单条处理,也即没有达到批量插入的效果。


JDBC原生批处理


    @Resource(name="dataSource")
    private DataSource dataSource;

    /* JDBC 的批量操作 */
    @Test
    public void insertBatch()  {
        Connection connection=dataSource.getConnection();
        connection.setAutoCommit(false);
        String sql="insert into notify_record(aa,bb, cc, dd, ee, ff, gg, hh, ii) " +
                "   values(?,?,?,?,?,?,?,?,?) ";

        PreparedStatement statement=connection.prepareStatement(sql);
        for (int i = 0; i < 1000 ; i++) {
            statement.setString(1, "aaa"+i);
            statement.setString(2, "bbb"+i);
            statement.setInt(3, i);
            statement.setInt(4, i);
            statement.setString(5, ""+i);
            statement.setString(6, "ddd"+i);
            statement.setString(7, "eee"+i);
            statement.setString(8, "fff"+i);
            statement.setString(9, "ggg"+i);
            statement.addBatch();
        }
        long start=System.currentTimeMillis();
        statement.executeBatch();
        connection.commit();
        connection.close();
        statement.close();
        System.out.print("耗时:");
        System.out.println(System.currentTimeMillis()-start);
    }

耗时情况:

记录数 耗时(ms)
100 210
1000 551
10000 1963
100000 10280

Mybatis进行批处理一。(ExecutorType=BATCH

(1)、SQL拼接(此方式还需要控制SQL到长度,否则会报错。当数据量太大的时候需要分批处理)

java实现:

        Map<String,Object> param = Maps.newHashMap();
        param.put("recordList", recordList);
        recordDao.insertList(param);

mapper.xml如下:

<insert id="insertList" parameterType="java.util.Map">
        insert into notify_record
        (aa,bb,cc,dd,ee,ff,gg,hh,ii)
        values
        <foreach collection="recordList" item="recordList" open="" close=""
                 separator=",">
            (
            null,
            #{recordList.aa,jdbcType=VARCHAR},
            #{recordList.bb,jdbcType=VARCHAR},
            #{recordList.cc,jdbcType=VARCHAR},
            #{recordList.dd,jdbcType=VARCHAR},
            #{recordList.ee,jdbcType=VARCHAR},
            #{recordList.ff,jdbcType=VARCHAR},
            #{recordList.gg,jdbcType=VARCHAR},
            #{recordList.hh,jdbcType=VARCHAR},
            #{recordList.ii,jdbcType=VARCHAR},
            )
        </foreach>
        ;
    </insert>

耗时情况:

记录数 耗时(ms)
100 414
1000 1035
10000 4899
100000 62752

(2)、for 循环插入,一条条的执行插入 (实现省略,这种肯定是最差的效果)

耗时情况:

记录数 耗时(ms)
100 9624
1000 102275
10000 失去耐心
100000 失去耐心

猜你喜欢

转载自blog.csdn.net/qq_33404395/article/details/84844166