Mybatis--逐条插入和批量插入---效率对比

首先,设置一下mysql的max_allowed_packet大小,当数据量大的时候,批量插入会报的错。修改mysql下的my.ini文件
在在[mysqld]段或者mysql的server配置段进行修改。默认大小是1M(1048576bit)。

max_allowed_packet = 100M

保存好之后,重启mysql.

### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4642540 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
; ]; Packet for query is too large (4642540 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.; nested exception is com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4642540 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:110)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy92.insert(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy105.batchInsertStone(Unknown Source)
    at com.nenu.serviceImpl.StoneAnalysisServiceImpl.readExcelValue(StoneAnalysisServiceImpl.java:585)
    at com.nenu.serviceImpl.StoneAnalysisServiceImpl.excel2sql(StoneAnalysisServiceImpl.java:213)
    at com.nenu.controller.AdminAnalysisController.excel2sql(AdminAnalysisController.java:314)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:60)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4642540 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3681)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2512)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
    at sun.reflect.GeneratedMethodAccessor82.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
    ... 65 more

application.properties
批量操作,要加:allowMultiQueries=true

spring.jpa.show-sql=true
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3308/insertdatabase?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.type-aliases-package=com.nenu.domain
mybatis.mapper-locations=classpath:Mapper/*.xml
spring.mvc.static-path-pattern=/static/**

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false

domain实体层

package com.nenu.domain;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class User implements Serializable{
    
    private static final long serialVersionUID = 6777870445002691891L;
    @Id
    @GeneratedValue
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

controller控制层

package com.nenu.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nenu.domain.User;
import com.nenu.service.UserService;

@Controller
public class UserController {
    long startTime =0;
    @Autowired
    private UserService userService;
    @RequestMapping(value="insert",method=RequestMethod.GET)
    public String show() {
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            User user = new User();
            user.setName("name"+i);
            list.add(user);
        }
        
        startTime = System.currentTimeMillis();//记录当前时间(毫秒)
        for (int i = 0; i <list.size(); i++) {
            userService.insertUser(list.get(i));
        }
        System.out.println("逐条插入 "+list.size()+" 条,耗时:"+(System.currentTimeMillis()-startTime)/1000);
        userService.clearAllUser();//删除数据
        startTime = System.currentTimeMillis();//记录当前时间(毫秒)
        userService.batchInsertUser(list);
        System.out.println("批量插入 "+list.size()+" 条,耗时:"+(System.currentTimeMillis()-startTime)/1000);
        userService.clearAllUser();//删除数据
        list.clear();
        for (int i = 0; i < 10000; i++) {
            User user = new User();
            user.setName("name"+i);
            list.add(user);
        }
        startTime = System.currentTimeMillis();//记录当前时间(毫秒)
        for (int i = 0; i <list.size(); i++) {
            userService.insertUser(list.get(i));
        }
        System.out.println("逐条插入 "+list.size()+" 条,耗时:"+(System.currentTimeMillis()-startTime)/1000);
        userService.clearAllUser();//删除数据
        startTime = System.currentTimeMillis();//记录当前时间(毫秒)
        userService.batchInsertUser(list);
        System.out.println("批量插入 "+list.size()+" 条,耗时:"+(System.currentTimeMillis()-startTime)/1000);
        userService.clearAllUser();//删除数据
        list.clear();
        for (int i = 0; i < 100000; i++) {
            User user = new User();
            user.setName("name"+i);
            list.add(user);
        }
        startTime = System.currentTimeMillis();//记录当前时间(毫秒)
        for (int i = 0; i <list.size(); i++) {
            userService.insertUser(list.get(i));
        }
        System.out.println("逐条插入 "+list.size()+" 条,耗时:"+(System.currentTimeMillis()-startTime)/1000);
        userService.clearAllUser();//删除数据
        startTime = System.currentTimeMillis();//记录当前时间(毫秒)
        userService.batchInsertUser(list);
        System.out.println("批量插入 "+list.size()+" 条,耗时:"+(System.currentTimeMillis()-startTime)/1000);
        userService.clearAllUser();//删除数据   
        return "index";
    }
}

dao层

package com.nenu.dao;

import java.util.List;

import com.nenu.domain.User;

public interface UserDao {
    int insertUser(User user);
    int batchInsertUser(List<User> list);
    void clearAllUser();
}

service层

package com.nenu.service;

import java.util.List;

import com.nenu.domain.User;

public interface UserService {
    int insertUser(User user);
    int batchInsertUser(List<User> list);
    void clearAllUser();
}

serviceImpl层

package com.nenu.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nenu.dao.UserDao;
import com.nenu.domain.User;
import com.nenu.service.UserService;
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;

    @Override
    public int insertUser(User user) {
        // TODO Auto-generated method stub
        return userDao.insertUser(user);
    }

    @Override
    public int batchInsertUser(List<User> list) {
        // TODO Auto-generated method stub
        return userDao.batchInsertUser(list);
    }

    @Override
    public void clearAllUser() {
        // TODO Auto-generated method stub
        userDao.clearAllUser();
    }
}

userMapper.xml

<?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 ="com.nenu.dao.UserDao">
    <resultMap type="com.nenu.domain.User" id="UserMap">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
    </resultMap>
    
    <parameterMap type="com.nenu.domain.User" id="User"/>
    
    <!-- 插入一条记录 -->
    <insert id="insertUser" parameterMap="User" useGeneratedKeys="true" keyProperty="id">
        insert into
        tb_user(id,name)
        values 
        (#{id},#{name})
    </insert>
    <!-- 批量插入记录 -->
    <insert id="batchInsertUser" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into
        tb_user(id,name)
        values 
        <foreach collection="list" item="user" separator=",">
        (#{user.id},#{user.name})
        </foreach>
        
    </insert>
        <!-- 删除表中全部记录 -->
    <update id="clearAllUser">
        TRUNCATE table tb_user
    </update>
</mapper>
对比.png

可以看出,批量插入比逐条插入速度快的不止千万倍。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容