@Data @Accessors(fluent = true) publicclassUser{ private Long id; private String name; // 生成的getter和setter方法如下,方法体略 public Long id(){} public User id(Long id){} public String name(){} public User name(String name){} }
@SpringBootTest publicclassMybatisPlusTest{ @Autowired private UserMapper userMapper; @Test publicvoidtestSelectList(){ // 通过构造器查询一个list集合,若无需判断条件就写null List<User> list = userMapper.selectList(null); list.forEach(System.out::println); } @Test publicvoidtestInsert(){ // 新增 // INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? ) User user = new User(); user.setName("张三"); user.setAge(18); user.setEmail("zhangsan@123.com"); int res = userMapper.insert(user); System.out.println("result:"+res); // id号很长,是通过雪花算法自动添加的唯一值 System.out.println("id:"+user.getId()); } @Test publicvoidtestDelete(){ // 通过id删除用户信息 // DELETE FROM user WHERE id=? // userMapper.deleteById(1557240268732936193L); // id号超出int范围了,加L变为长整型 // 根据map集合中的条件删除用户信息 // DELETE FROM user WHERE name = ? AND age = ? // Map<String, Object> map = new HashMap<>(); // map.put("name","张三"); // map.put("age",18); // userMapper.deleteByMap(map); // 通过多个id实现批量删除 // DELETE FROM user WHERE id IN ( ? , ? , ? ) List<Long> list = Arrays.asList(1L, 2L, 3L); // 数据库里id设的是Long型 userMapper.deleteBatchIds(list); } @Test publicvoidtestUpdate(){ // 修改用户信息 // UPDATE user SET name=?, age=? WHERE id=? User user = new User(); user.setId(2L); user.setName("李斯"); user.setAge(20); userMapper.updateById(user); } @Test publicvoidtestSelect(){ // 通过id查询 // SELECT id,name,age,email FROM user WHERE id=? // userMapper.selectById(1L); // 通过多个id查询多个用户信息 // SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? ) // List<Long> list = Arrays.asList(1L, 2L, 3L); // userMapper.selectBatchIds(list); // 根据map集合中的条件查询信息 // SELECT id,name,age,email FROM user WHERE name = ? AND age = ? // Map<String, Object> map = new HashMap<>(); // map.put("name","Jack"); // map.put("age",20); // userMapper.selectByMap(map); // 自定义的方法 userMapper.selectMapById(1L); } }
@SpringBootTest publicclassMyBatisPlusWrapperTest{ @Autowired private UserMapper userMapper; @Test publicvoidtest01(){ // 查询用户名包含a,年龄在20~30之间,邮箱不为空的 // SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age BETWEEN ? AND ? AND email IS NULL) QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.like("name","a") // column的内容是数据库的字段名 .between("age",20,30) // 链式编程的写法 . isNotNull("email"); userMapper.selectList(queryWrapper); } @Test publicvoidtest02(){ // 查询用户信息,按照年龄的降序排序,若年龄相同,按id升序排序 // SELECT id,name,age,email FROM user ORDER BY age DESC,id ASC QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByDesc("age") .orderByAsc("id"); userMapper.selectList(queryWrapper); } @Test publicvoidtest03(){ // 删除邮箱地址为空的用户信息 // DELETE FROM user WHERE (email IS NULL) // 如果有isDeleted字段,则该语句是自动执行修改语句 QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.isNull("email"); userMapper.delete(queryWrapper); } @Test publicvoidtest04(){ // 修改(年龄大于20并且名字中包含a的)或邮箱为空的 // UPDATE user SET name=?, email=? WHERE (age > ? AND name LIKE ? OR email IS NULL) QueryWrapper<User> queryWrapper = new QueryWrapper<>(); // gt: > lt: < ge: >= le: <= queryWrapper.gt("age",20) .like("name","a") .or() .isNull("email"); User user = new User(); user.setName("小明"); user.setEmail("test@qq.com"); userMapper.update(user,queryWrapper); } @Test publicvoidtest05(){ // 修改用户命中包含a并且(年龄大于20或邮箱为null)的用户信息 // UPDATE user SET name=?, email=? WHERE (name LIKE ? AND (age > ? OR email IS NULL)) QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.like("name","a") // lambda表达式的内容会优先执行 .and(i->i.gt("age",20).or().isNull("email")); User user = new User(); user.setName("小hong"); user.setEmail("test@qq.com"); userMapper.update(user,queryWrapper); } @Test publicvoidtest07(){ // 修改用户命中包含a并且(年龄大于20或邮箱为null)的用户信息 // UPDATE user SET name=?,email=? WHERE (name LIKE ? AND (age > ? OR email IS NULL)) UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); updateWrapper.like("name","a") .and(i -> i.gt("age",20).or().isNull("email")); updateWrapper.set("name","小黑").set("email","abc@123.com"); userMapper.update(null,updateWrapper); } @Test publicvoidtest10(){ // 与上面实现相同,lambda的作用只是避免写错字段名 // UPDATE user SET name=?,email=? WHERE (name LIKE ? AND (age > ? OR email IS NULL)) LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.like(User::getName,"a") .and(i -> i.gt(User::getAge,20).or().isNull(User::getEmail)); updateWrapper.set(User::getName,"小黑").set(User::getEmail,"abc@123.com"); userMapper.update(null,updateWrapper); } @Test publicvoidtest06(){ // 查询用户名 // SELECT name FROM user QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.select("name"); userMapper.selectMaps(queryWrapper); } @Test publicvoidtest08(){ // 公司业务中常用 // 先判断条件是否符合,再组装相对应的条件到sql语句中 // SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age <= ?) String name = "a"; Integer ageBegin = null; Integer ageEnd = 30; QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.like(StringUtils.isNotBlank(name),"name","name") // isNotBlank 不为空字符串,不为null .ge(ageBegin != null, "age", ageBegin) .le(ageEnd != null, "age", ageEnd); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } @Test publicvoidtest09(){ // 和上面执行是一样的,只是写法不同;可防止把字段名写错 // SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age <= ?) String name = "a"; Integer ageBegin = null; Integer ageEnd = 30; LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.like(StringUtils.isNotBlank(name),User::getName, name) .ge(ageBegin != null, User::getAge, ageBegin) .le(ageEnd != null, User::getAge, ageEnd); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } }
<!-- Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);--> <selectid="selectPageVo"resultType="User"> select * from user where age > #{age} </select>