您好,登錄后才能下訂單哦!
這篇文章主要介紹了MyBatis的批量查詢方法有哪些的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇MyBatis的批量查詢方法有哪些文章都會有所收獲,下面我們一起來看看吧。
@RestController @RequestMapping("/mybatis3/user") @RequiredArgsConstructor public class UserController { private final IUserService iUserService; @GetMapping("/one") public Long one(){ return iUserService.add(); } } Long add(); @Service @RequiredArgsConstructor public 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多
@RestController @RequestMapping("/mybatis3/user") @RequiredArgsConstructor public class UserController { private final IUserService iUserService; @GetMapping("/one") public Long one(){ return iUserService.add(); } } Long add2(); @Service @RequiredArgsConstructor public 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
@RestController @RequestMapping("/mybatis3/user") @RequiredArgsConstructor public class UserController { private final IUserService iUserService; @GetMapping("/one3") public Long one3(){ return iUserService.add3(); } } Long add3(); @Service @RequiredArgsConstructor public 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>
@RestController @RequestMapping("/mybatis3/user") @RequiredArgsConstructor public class UserController { private final IUserService iUserService; @GetMapping("/one4") public Long one4(){ return iUserService.add4(); } } Long add4(); @Service @RequiredArgsConstructor public 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); } }
直接報錯:
看報錯信息:
長串: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 ‘id’ at row 1
; Data truncation: Out of range value for column ‘id’ at row 1; nested exception is java.sql.BatchUpdateException: Data truncation: Out of range value for column ‘id’ at row 1] with root cause短串:Data truncation: Out of range value for column ‘id’ at row 1
翻譯一下:
可以發現就是我們的id超出范圍:
int類型改為bigint即可
故此我們可以得出一個結論:設置數據庫id的時候設置為bigint還是蠻好的哈
平均時間:0.2s
InsertBatchSomeColumn方法了解
這個類的注解就寫的很明白
擴展這個InsertBatchSomeColumn方法
@Slf4j public 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容器
/** * @author Stone * @date 2023/1/3 * @apiNote */ @Configuration public class MybatisPlusConfig { @Bean public EasySqlInjector sqlInjector(){ return new EasySqlInjector(); } }
創建一個Mapper去實現我們的擴展的飛方法
public interface EasySqlInjectMapper<T> extends BaseMapper<T> { /** * 批量插入 僅適用于mysql * * @param entityList 實體列表 * @return 影響行數 */ 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的批量查詢方法有哪些”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。