广告
返回顶部
首页 > 资讯 > 精选 >MyBatis的批量查询方法有哪些
  • 582
分享到

MyBatis的批量查询方法有哪些

2023-07-05 06:07:25 582人浏览 薄情痞子
摘要

这篇文章主要介绍了mybatis的批量查询方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇MyBatis的批量查询方法有哪些文章都会有所收获,下面我们一起来看看吧。一.直接循环插入@RestContro

这篇文章主要介绍了mybatis的批量查询方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇MyBatis的批量查询方法有哪些文章都会有所收获,下面我们一起来看看吧。

一.直接循环插入

@RestController@RequestMapping("/mybatis3/user")@RequiredArgsConstructorpublic class UserController {    private final IUserService iUserService;    @GetMapping("/one")    public Long one(){       return iUserService.add();    }}  Long add();@Service@RequiredArgsConstructorpublic class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {    private final UserMapper userMapper;    @Override    public Long add() {        long start = System.currentTimeMillis();        for (int i = 0; i < 10000; i++) {            User user = new User();            user.setUsername("name"+i);            user.setPassword("passWord"+i);            userMapper.insertUsers(user);        }        long end = System.currentTimeMillis();        System.out.println("耗时:"+( end - start ) + "ms");        return (end-start);    }    } Integer insertUsers(User user); <insert id="insertUsers" >        insert into user(username,password)        values (#{username}, #{password})    </insert>

最终耗时:14s多

MyBatis的批量查询方法有哪些

二.关闭MySql自动提交,手动进行循环插入提交

@RestController@RequestMapping("/mybatis3/user")@RequiredArgsConstructorpublic class UserController {    private final IUserService iUserService;        @GetMapping("/one")    public Long one(){       return iUserService.add();    }} Long add2();@Service@RequiredArgsConstructorpublic class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {    private final UserMapper userMapper;//    手动开启sql的批量提交    private final   SqlSessionTemplate sqlSessionTemplate;    @Override    public Long add2(){        //关闭自动提交        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);        UserMapper mapper = sqlSession.getMapper(UserMapper.class);        long start = System.currentTimeMillis();        for (int i = 0; i < 10000; i++) {            User user = new User();            user.setUsername("name"+i);            user.setPassword("password"+i);            mapper.insertUsers(user);        }        //自动提交SQL        sqlSession.commit();        long end = System.currentTimeMillis();        System.out.println("耗时:"+( end - start ) + "ms");        return (end-start);    }    }

平均:0.12s

MyBatis的批量查询方法有哪些

第三种:用List集合的方式插入数据库(推荐)

@RestController@RequestMapping("/mybatis3/user")@RequiredArgsConstructorpublic class UserController {    private final IUserService iUserService;       @GetMapping("/one3")    public Long one3(){        return iUserService.add3();    }}  Long add3();@Service@RequiredArgsConstructorpublic class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {    private final UserMapper userMapper;  @Override    public Long add3(){        long start = System.currentTimeMillis();        List<User> userList = new ArrayList<>();        User user;        for (int i = 0; i < 10000; i++) {            user = new User();            user.setUsername("name"+i);            user.setPassword("password"+i);            userList.add(user);        }        userMapper.insertUsersThree(userList);        long end = System.currentTimeMillis();        System.out.println("耗时:"+( end - start ) + "ms");        return (end-start);    }    } Integer insertUsersThree(List<User> userList);<insert id="insertUsersThree">        insert into user(username,password)        values        <foreach collection="userList" item="user" separator=",">            (#{user.username},#{user.password})        </foreach>    </insert>

第四种: MyBatis-Plus提供的SaveBatch方法

@RestController@RequestMapping("/mybatis3/user")@RequiredArgsConstructorpublic class UserController {    private final IUserService iUserService;@GetMapping("/one4")    public Long one4(){        return iUserService.add4();    }}  Long add4();@Service@RequiredArgsConstructorpublic class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {    private final UserMapper userMapper;@Override    public Long add4() {        long start = System.currentTimeMillis();        List<User> userList= new ArrayList<>();        User user ;        for (int i = 0; i < 10000; i++) {            user = new User();            user.setUsername("name"+i);            user.setPassword("password"+i);            userList.add(user);        }        saveBatch(userList);        long end = System.currentTimeMillis();        System.out.println("耗时:"+( end - start ) + "ms");        return (end-start);    }    }

直接报错:

MyBatis的批量查询方法有哪些

看报错信息:

长串:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: com.huang.mybatis3.mapper.UserMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: Data truncation: Out of range value for column &lsquo;id&rsquo; at row 1
; Data truncation: Out of range value for column &lsquo;id&rsquo; at row 1; nested exception is java.sql.BatchUpdateException: Data truncation: Out of range value for column &lsquo;id&rsquo; at row 1] with root cause

短串:Data truncation: Out of range value for column &lsquo;id&rsquo; at row 1

翻译一下:

MyBatis的批量查询方法有哪些

可以发现就是我们的id超出范围:

MyBatis的批量查询方法有哪些

int类型改为bigint即可

故此我们可以得出一个结论:设置数据库id的时候设置为bigint还是蛮好的哈

平均时间:0.2s

MyBatis的批量查询方法有哪些

第五种 MyBatis-Plus提供的InsertBatchSomeColumn方法(推荐)

InsertBatchSomeColumn方法了解

MyBatis的批量查询方法有哪些

这个类的注解就写的很明白

扩展这个InsertBatchSomeColumn方法

@Slf4jpublic class EasySqlInjector extends DefaultSqlInjector {    @Override    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {        // 注意:此SQL注入器继承了DefaultSqlInjector(默认注入器),调用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);        methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));        log.info("扩展的getMethodList方法被框架调用了");        return methodList;    }}

扩展的方法注入bean容器

@Configurationpublic class MybatisPlusConfig {    @Bean    public  EasySqlInjector sqlInjector(){        return new EasySqlInjector();    }}

创建一个Mapper去实现我们的扩展的飞方法

public interface EasySqlInjectMapper<T> extends BaseMapper<T> {        Integer insertBatchSomeColumn(Collection<T> entityList);}

业务层

@Override    public Long add5() {        long start = System.currentTimeMillis();        List<User> userList= new ArrayList<>();        User user ;        for (int i = 0; i < 10000; i++) {            user = new User();            user.setUsername("name"+i);            user.setPassword("password"+i);            userList.add(user);        }        userMapper.insertBatchSomeColumn(userList);        long end = System.currentTimeMillis();        System.out.println("耗时:"+( end - start ) + "ms");        return (end-start);    }

耗时: 0.2 s

MyBatis的批量查询方法有哪些

关于“MyBatis的批量查询方法有哪些”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“MyBatis的批量查询方法有哪些”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。

--结束END--

本文标题: MyBatis的批量查询方法有哪些

本文链接: https://www.lsjlt.com/news/350271.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MyBatis的批量查询方法有哪些
    这篇文章主要介绍了MyBatis的批量查询方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇MyBatis的批量查询方法有哪些文章都会有所收获,下面我们一起来看看吧。一.直接循环插入@RestContro...
    99+
    2023-07-05
  • MyBatis-Plus分页查询的方法有哪些
    本文小编为大家详细介绍“MyBatis-Plus分页查询的方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“MyBatis-Plus分页查询的方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。方法:...
    99+
    2023-06-29
  • Mybatis批量插入大量数据的方法有哪些
    本文小编为大家详细介绍“Mybatis批量插入大量数据的方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“Mybatis批量插入大量数据的方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。Mybat...
    99+
    2023-07-05
  • Mybatis动态SQL foreach批量操作方法有哪些
    本篇内容主要讲解“Mybatis动态SQL foreach批量操作方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mybatis动态SQL foreach批量操作方法...
    99+
    2023-07-05
  • mybatis接收以逗号分隔的字符串批量查询方法
    本篇内容介绍了“mybatis接收以逗号分隔的字符串批量查询方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!接收以逗号分隔的字符串批量查询...
    99+
    2023-06-28
  • 在线域名批量查询工具有哪些
    1. 域名批量查询工具2. Whois批量查询工具3. 域名信息查询工具4. 域名解析查询工具5. 域名注册信息查询工具6. 域名服...
    99+
    2023-06-12
    域名批量查询 域名
  • mybatis批量新增、删除、查询和修改方式
    目录前期说明:主要有一下3种情况:(1)mybatis批量新增(2)mybatis批量删除(3)mybatis批量查询(4)mybatis批量修改mySql Case函数动态批量修改...
    99+
    2022-11-12
  • MyBatis的五种批量查询实例总结
    目录一.直接循环插入二.关闭MySql自动提交,手动进行循环插入提交第三种:用List集合的方式插入数据库(推荐)第四种: MyBatis-Plus提供的SaveBatch方法第五种...
    99+
    2023-02-28
    mybatis批量查询语句 mybatis 批量操作 mybatis 批量查询
  • Mybatis和Mybatis-Plus时间范围查询方式有哪些
    小编给大家分享一下Mybatis和Mybatis-Plus时间范围查询方式有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、mysql1.传入时间范围参数类型是字符串 <if test=&q...
    99+
    2023-06-20
  • SQL查询方法有哪些
    SQL查询方法有哪些,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、 简单查询   简单的Transact-SQL查询只包括选择列表、FRO...
    99+
    2022-10-18
  • 域名批量查询的方法是什么
    域名批量查询的方法可以通过以下步骤实现:1. 准备一个包含所有要查询的域名的列表。2. 找到一个支持批量查询的域名查询工具,例如WH...
    99+
    2023-06-10
    域名批量查询 域名
  • Java批量执行的方法有哪些
    Java中批量执行的方法有以下几种:1. 使用for循环进行批量执行:通过for循环遍历一个集合或数组,然后依次执行相同的操作。``...
    99+
    2023-08-09
    Java
  • 域名查询的方法有哪些
    1. WHOIS查询:WHOIS是一种查询域名所有者、注册商、注册日期等信息的协议,可以在WHOIS查询网站上进行查询。2. DNS...
    99+
    2023-06-17
    域名查询 域名
  • 【mybatis】mapper.xml中foreach的用法,含批量查询、插入、修改、删除方法的使用
    一、xml文件中foreach的主要属性 foreach元素的属性主要有 collection,item,index,separator,open,close。 collection: 表示集合,数据源 item :表示集合中的每一个元素 ...
    99+
    2023-08-30
    xml java 数据库 mysql
  • sql批量模糊查询的方法是什么
    在SQL中,批量模糊查询可以通过使用通配符和IN子句来实现。以下是一种常见的方法:1. 使用通配符进行模糊查询:通配符是用来匹配搜索...
    99+
    2023-08-24
    sql
  • Java中Mybatis分页查询的传参方式有哪些
    这篇文章主要介绍了Java中Mybatis分页查询的传参方式有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java中Mybatis分页查询的传参方式有哪些文章都会有所收获,下面我们一起来看看吧。一、顺序传...
    99+
    2023-07-05
  • mybatis接收以逗号分隔的字符串批量查询方式
    目录接收以逗号分隔的字符串批量查询如何相互转换逗号分隔的字符串和List将逗号分隔的字符串转换为List将List转换为逗号分隔符接收以逗号分隔的字符串批量查询 <IF tes...
    99+
    2022-11-13
  • 域名批量查询工具有哪些及怎么使用
    域名批量查询工具有很多,其中比较常用的有以下几种:1. Whois批量查询工具:可以查询多个域名的Whois信息,包括域名注册人、注...
    99+
    2023-06-14
    域名批量查询 域名
  • mysql去重查询的方法有哪些
    本篇内容介绍了“mysql去重查询的方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、插入测试数据下图测试数据中user_name...
    99+
    2023-07-05
  • gitlab权限查询的方法有哪些
    这篇文章主要介绍“gitlab权限查询的方法有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“gitlab权限查询的方法有哪些”文章能帮助大家解决问题。通过Web界面查询权限GitLab的Web界...
    99+
    2023-07-05
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作