Mybatis进阶--批量新增数据

一、传统JDBC进行批处理操作

package jdbc;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class jdbcUtil {
    private static class JdbcUtil {
    }

    //处理数据库事务,提交事务
    public static void commit(Connection conn) {
        if (null != conn) {
            try {
                conn.commit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //事务的回滚
    public static void rollback(Connection conn) {
        if (null != conn) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //事务的开始
    public static void begin(Connection conn) {
        if (null != conn) {
            try {
                conn.setAutoCommit(false);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    /**
     * 获取数据库的连接
     */
    public static Connection getConnection() throws Exception {
        Properties properties = new Properties();
        //速度快的方式
        //new fileinputStream()
        InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        //jdbc文件 内容 以流的方式加载到properties文件中
        properties.load(is);
        String driver = properties.getProperty("driver");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        Class.forName(driver);

        return DriverManager.getConnection(url, username, password);
    }

    public static void closeResources(Connection conn, Statement statement, ResultSet resultSet){
        if (null!=resultSet){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (null!=statement){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


    }
}

for循环插入1万条数据

方法二:addBatch()

package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class BatchTestOne  {
    public static void main(String[] args) throws Exception{
        Connection conn = null;
        PreparedStatement preparedStatement = null;

        conn = jdbcUtil.getConnection();

        //设置手动提交
        jdbcUtil.begin(conn);

        String sql = "insert into t_user (username,password) values(?,?)";

        preparedStatement = conn.prepareStatement(sql);
        long beginTime = System.currentTimeMillis();
        //当每达到1000条就提交
        for(int i=0;i<10000;i++){
            preparedStatement.setString(1,"hello"+i);
            preparedStatement.setString(2,"world"+i);
            preparedStatement.executeUpdate();
        }
        jdbcUtil.commit(conn);

        long endTime = System.currentTimeMillis();

        System.out.println(endTime-beginTime);//1537

    }
}

传统利用for循环进行插入,存在严重效率问题,需要频繁获取Session,获取连接

使用批处理,代码和SQL的耦合,代码量较大

Mybatis批量插入

  1. 借助foreach标签使用 insert into table values()

借助mysql这样的方式插入数据。

方法二 借助MySQL数据库连接属性allowMultiQueries=true

借助mysql这样的方式插入数据

直接把insert into移到里边

基于SqlSession的ExecutorType进行批量添加

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值