mybatis plus 增删改查

//mybatis plus 增删改查
@SpringBootTest
public class MapperTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        HashMap map = new HashMap();
        map.put("name","Sandy");
        map.put("age","21");
        List<User> userList = userMapper.selectByMap(map);
//        Assert.isTrue(1 == userList.size(), "预判成功");
        userList.forEach(System.out::println);
    }
    @Test
    public void testSelectWrapper() {
        System.out.println(("----- selectAll method test ------"));
        QueryWrapper<User> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("name","Sandy").or().eq("age","21");
        queryWrapper.like("name","n");
        List<User> userList = userMapper.selectList(queryWrapper);
//        Assert.isTrue(1 == userList.size(), "预判成功");
        for (User user : userList) {
            System.out.println("用户编号:"+user.getId());
            System.out.println("用户名:"+user.getName());
            System.out.println("用户年龄:"+user.getAge());
            System.out.println("用户邮箱:"+user.getEmail());
        }
    }
    @Test
    public void testInsert() {
        User user = new User();
        user.setId(6L);
        user.setName("张三");
        user.setAge(18);
        user.setEmail("[email protected]");
        int count = userMapper.insert(user);
        System.out.println("成功插入:"+count+"条数据");
    }
    @Test
    public void testUpdate() {
        User user = new User();
        user.setId(6L);
        user.setName("李四");
//        user.setAge(18);
//        user.setEmail("[email protected]");
        int count = userMapper.updateById(user);
        System.out.println("成功更新:"+count+"条数据");
    }
    @Test
    public void testDelete() {
//        User user = new User();
//        user.setId(6L);
//        user.setName("李四");
//        user.setAge(18);
//        user.setEmail("[email protected]");
        int count = userMapper.deleteById(6L);
        System.out.println("成功删除:"+count+"条数据");
    }
    @Test
    public void testPage() {
        System.out.println(("----- selectAll method test ------"));
        Page<User> param = new Page<>();
        param.setCurrent(2);
        param.setSize(3);
        IPage<User> page = userMapper.page(param);
//        Assert.isTrue(1 == userList.size(), "预判成功");
        System.out.println("总记录数:"+page.getTotal());
        for (User user : page.getRecords()) {
            System.out.println("用户编号:"+user.getId());
            System.out.println("用户名:"+user.getName());
            System.out.println("用户年龄:"+user.getAge());
            System.out.println("用户邮箱:"+user.getEmail());
        }
    }
}

你可能感兴趣的:(mybatis,java)