mybatis-plus使用配置
代码仓库:https://github.com/Kingsea442/mybatis-plus-demo
使用mybatis-plus简化数据库操作
1. 增加记录
@Autowiredprivate UserMapper userMapper;@Testpublic void testInsertUser() {User user = new User();user.setUserName("wanglianhai");user.setAge(18);user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);Assertions.assertNotEquals(user.getId(), 0);}
2. 查询
@Autowiredprivate UserMapper userMapper;@Testpublic void selectById() {LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(User::getId, 1);User user = userMapper.selectOne(queryWrapper);System.out.println(user);Assertions.assertNotEquals(user, null);}
3. 分页查询
@Testpublic void page() {LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.gt(User::getId, 0);Page<User> page = new Page();page.setSize(10);page.setCurrent(2);Page<User> userPage = userMapper.selectPage(page, queryWrapper);for (User record : userPage.getRecords()) {System.out.println(record);}System.out.println(userPage.getPages());}
4. 列表查询
@Testpublic void selectName() {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.select("distinct user_name");List<User> users = userMapper.selectList(queryWrapper);for (User user : users) {System.out.println(user);}}