@Test publicvoidtestDelete() { // 通过id删除用户信息 // DELECT FROM user WHERE id=? intres= userMapper.deleteById(1545995108796071938L); System.out.println("res:" + res);
// 通过条件删除用户 // DELETE FROM user WHERE name = ? AND age = ? AND email = ? Map<String, Object> map = newHashMap<>(); map.put("name", "小明"); map.put("age", 19); map.put("email", "xxx.@gmail.com"); userMapper.deleteByMap(map);
// 通过多个id删除用户 // DELETE FROM user WHERE id IN ( ? , ? , ? )
// asList将里面的这三个数字转成一个list集合 List<Long> list = Arrays.asList(1L, 2L, 3L); res = userMapper.deleteBatchIds(list); System.out.println("res:" + res); }
Update
1 2 3 4
// 根据 whereWrapper 条件,更新记录 intupdate(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper); // 根据 ID 修改 intupdateById(@Param(Constants.ENTITY) T entity);
1 2 3 4 5 6 7 8 9 10 11
@Test publicvoidtestUpdate() { // 根据id更新数据 // UPDATE user SET name=?, email=? WHERE id=? Useruser=newUser(); user.setId(4L); user.setName("李四"); user.setEmail("xxx.@gemail.com"); intres= userMapper.updateById(user); System.out.println("res:" + res); }
@Test publicvoidtestSelect() { // 通过id查找用户 // SELECT id,name,age,email FROM user WHERE id=? Useruser=newUser(); user.setId(9L); Userres= userMapper.selectById(user.getId()); System.out.println(res);
// 根据id批量查询 // SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? ) List<Long> list = Arrays.asList(8L, 9L, 10L); List<User> users = userMapper.selectBatchIds(list); users.forEach(System.out::println);
// 通过map查询数据 // SELECT id,name,age,email FROM user WHERE name = ? AND age = ? Map<String, Object> map = newHashMap<>(); map.put("name","晴雯"); map.put("age",16); List<User> users1 = userMapper.selectByMap(map); users1.forEach(System.out::println);
// 查询所有数据 // SELECT id,name,age,email FROM user List<User> users2 = userMapper.selectList(null); users2.forEach(System.out::println); }
IService
Service接口及它的实现类需要这样写。ServiceImpl<M extends BaseMapper<T>, T> M 表示需要操作的实体类的Mapper,T 表示需要操作的实体类。
@Test publicvoidtest01() { // 查询用户名包含 雯,年龄在16到25之间,邮箱信息不为null的用户信息 // SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL) QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper.like("name", "雯") .between("age", 16, 25) .isNotNull("email"); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
1 2 3 4 5 6 7 8 9 10
@Test publicvoidtest02() { // 查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序 // SELECT id,name,age,email FROM user ORDER BY age DESC,id ASC QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper.orderByDesc("age") .orderByAsc("id"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
1 2 3 4 5 6 7 8 9 10 11
@Test publicvoidtest03() { // 删除邮箱地址为null的用户信息 // DELETE FROM user WHERE (email IS NULL) // 我并没有使用假删除 QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper.isNull("email"); intresult= userMapper.delete(queryWrapper); System.out.println(result > 0 ? "删除成功!" : "删除失败!"); System.out.println("受影响的行数为:" + result); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
@Test publicvoidtest04() { // 将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改 // UPDATE user SET name=?, email=? WHERE (age > ? AND name LIKE ? OR email IS NULL) UpdateWrapper<User> updateWrapper = newUpdateWrapper<>(); // gt是大于 updateWrapper.gt("age", 20) .like("name", "贝") .or() .isNull("email"); Useruser=newUser(); user.setName("Oz"); user.setEmail("test@oz6.com");
@Test publicvoidtest07() { // 子查询 // 查询id小于等于100的用户信息 // SELECT id,name,age,email FROM user WHERE (id IN (select id from user where id <= 100)) QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper.inSql("id", "select id from user where id <= 100"); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }