@Test publicvoidtestDeleteById(){ //通过id删除用户信息 //DELETE FROM user WHERE id=? intresult= userMapper.deleteById(1475754982694199298L); System.out.println("受影响行数:"+result); }
2、通过id批量删除记录
1 2 3 4 5 6 7
@Test publicvoidtestDeleteBatchIds(){ //通过多个id批量删除 //DELETE FROM user WHERE id IN ( ? , ? , ? ) List<Long> idList = Arrays.asList(1L, 2L, 3L); intresult= userMapper.deleteBatchIds(idList); System.out.println("受影响行数:"+result); }
3、通过map条件删除记录
1 2 3 4 5 6 7 8 9 10
@Test publicvoidtestDeleteByMap(){ //根据map集合中所设置的条件删除记录 //DELETE FROM user WHERE name = ? AND age = ? Map<String, Object> map = newHashMap<>(); map.put("age", 23); map.put("name", "张三"); intresult= userMapper.deleteByMap(map); System.out.println("受影响行数:"+result); }
4、修改
1 2 3 4 5 6 7
@Test publicvoidtestUpdateById(){ Useruser=newUser(4L, "admin", 22, null); //UPDATE user SET name=?, age=? WHERE id=? intresult= userMapper.updateById(user); System.out.println("受影响行数:"+result); }
5、查询
1、根据id查询用户信息
1 2 3 4 5 6 7
@Test publicvoidtestSelectById(){ //根据id查询用户信息 //SELECT id,name,age,email FROM user WHERE id=? Useruser= userMapper.selectById(4L); System.out.println(user); }
2、根据多个id查询多个用户信息
1 2 3 4 5 6 7 8
@Test publicvoidtestSelectBatchIds(){ //根据多个id查询多个用户信息 //SELECT id,name,age,email FROM user WHERE id IN ( ? , ? ) List<Long> idList = Arrays.asList(4L, 5L); List<User> list = userMapper.selectBatchIds(idList); list.forEach(System.out::println); }
3、通过map条件查询用户信息
1 2 3 4 5 6 7 8 9 10
@Test publicvoidtestSelectByMap(){ //通过map条件查询用户信息 //SELECT id,name,age,email FROM user WHERE name = ? AND age = ? Map<String, Object> map = newHashMap<>(); map.put("age", 22); map.put("name", "admin"); List<User> list = userMapper.selectByMap(map); list.forEach(System.out::println); }
4、查询所有数据
1 2 3 4 5 6
@Test publicvoidtestSelectList(){ //查询所有用户信息 //SELECT id,name,age,email FROM user List<User> list = userMapper.selectList(null); list.forEach(System.out::println); }