Mybatis-plus 数据库操作测试

package cn.itcast.mp;

import cn.itcast.mp.mapper.UserMapper;
import cn.itcast.mp.pojo.User;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestUserMapper {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testInsert() {
        User user = new User();
        user.setEmail("2@qq.com");
        user.setAge(28);
        user.setUserName("lele");
        user.setName("乐乐");
        user.setPassword("123456");
        int insert = this.userMapper.insert(user);
        System.out.println(insert);
        System.out.println("id=>" + user.getId());
    }

    @Test
    public void testUpdateById() {
        User user = new User();
        user.setId(1L);
        user.setAge(19);
        user.setPassword("6666");
        this.userMapper.updateById(user);
    }

    @Test
    public void testUpdate() {
        User user = new User();
        user.setAge(20);
        user.setPassword("8888");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", "zhangsan");
        int update = this.userMapper.update(user, wrapper);
        System.out.println(update);
    }

    @Test
    public void testUpdate2() {

        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.set("age", 21).set("password", 9999).eq("user_name", "zhangsan"); //流失操作
        int update = this.userMapper.update(null, wrapper);
        System.out.println(update);
    }

    @Test
    public void testDeleteByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_name", "zhangsan");
        map.put("password", 9999);
        this.userMapper.deleteByMap(map);
    }

    @Test
    public void testDelete() {

//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_name","lisi").eq("password", "123456");

        User user = new User();
        user.setPassword("123456");
        user.setUserName("wangwu");
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);
        int delete = this.userMapper.delete(wrapper);
        System.out.println(delete);
    }

    @Test
    public void testDeleteBatchIds() {
        int ids = this.userMapper.deleteBatchIds(Arrays.asList(7L, 8L));
        System.out.println("ids=>" + ids);
    }

    @Test
    public void testSelectById() {
        User user = this.userMapper.selectById(4L);
        System.out.println(user);
    }

    @Test
    public void testSelectBatchIds() {
        List<User> userList = this.userMapper.selectBatchIds(Arrays.asList(4L, 5L, 6L));
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testSelectOne() {
        //查询的结果超过一条会报错
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", "zhaoliu");
        User user = this.userMapper.selectOne(wrapper);
        System.out.println(user);
    }

    @Test
    public void testSelectCount() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 20); //年龄大于20
        Long count = this.userMapper.selectCount(wrapper);
        System.out.println(count);
    }

    @Test
    public void testSelectList() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("email", "itcast");
        List<User> userList = this.userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testSelectPage() {

        Page<User> page = new Page<>(3, 1);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("email", "itcast");
        Page<User> userPage = this.userMapper.selectPage(page, wrapper);
        System.out.println("totalItems=>" + userPage.getTotal());
        System.out.println("totalPages=>" + userPage.getPages());
        System.out.println("currentPage" + userPage.getCurrent());
        List<User> records = userPage.getRecords();
        for (User record : records) {
            System.out.println(record);
        }
    }

    @Test
    public void testFindById() {
        User user = this.userMapper.findById(5L);
        System.out.println(user);
    }

    @Test
    public void testAllEq() {

        Map<String, Object> params = new HashMap<>();
        params.put("name", "赵六");
        params.put("age", "21");
        params.put("password", null);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.allEq(params);
//        wrapper.allEq(params,false);
        wrapper.allEq((k,v) -> (k.equals("age") || k.equals("id")),params);

        List<User> userList = this.userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testLike(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.likeLeft("name","六");
        List<User> userList = this.userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testOrderByAge(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("age");
        List<User> userList = this.userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testOR(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","赵六").or().eq("age",24);
        List<User> userList = this.userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test   //指定查询的字段,锁定我们感兴趣的数据
    public void testSelect(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","赵六").or().eq("age",24).select("id","name","age");
        List<User> userList = this.userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }
}

上一篇:3级分类菜单数据删除


下一篇:III-MybatisPlus、ORM思想