Mybatis Plus 使用学习八 mapper的增删改查 示例方法

基于前面几节,我们已经演示了Mybatis Plus中mapper提供的增删改查方法,我们在这里把完整演示方法的代码贴出来,你可以直接复制运行。

package com.itzhimei.mybatis.plus.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzhimei.mybatis.plus.mapper.UserMapper;
import com.itzhimei.mybatis.plus.model.User;
import org.junit.Assert;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * www.itzhimei.com
 * 编程技术之美
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test www.itzhimei.com------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

    @Test
    public void testSelectPage() {
        System.out.println(("----- selectAll method test ------"));
        Page<User> userPage = userMapper.selectPage(new Page<User>(1, 2), null);
        List<User> records = userPage.getRecords();
        records.forEach(System.out::println);
    }

    @Test
    public void testSelectById() {
        System.out.println(("----- selectById method test ------"));
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

    @Test
    public void testSelectOne() {
        System.out.println(("----- selectOne method test ------"));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Name", "Jack");
        User user = userMapper.selectOne(wrapper);
        System.out.println(user);
    }

    @Test
    public void testSelectBatchIds() {
        System.out.println(("----- selectBatchIds method test ------"));
        List<Integer> ids =  new ArrayList<>();
        ids.add(1);
        ids.add(2);
        List<User> users = userMapper.selectBatchIds(ids);
        users.forEach(System.out::println);
    }

    @Test
    public void testSelectByMap() {
        System.out.println(("----- selectByMap method test ------"));
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("Name","Jack");
        columnMap.put("Name","Jone");
        columnMap.put("Age",20);
        List<User> users = userMapper.selectByMap(columnMap);
        users.forEach(System.out::println);
    }

    @Test
    public void testSelectCount() {
        System.out.println(("----- selectCount method test www.itzhimei.com------"));
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("Name","Jack");
        columnMap.put("Name","Jone");
        columnMap.put("Age",20);
        Integer integer = userMapper.selectCount(null);
        System.out.println(("----- 总记录数: "+integer));
    }


    @Test
    public void testSelectMaps() {
        System.out.println(("----- selectMaps method test ------"));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.gt("Age", 21);

        List<Map<String, Object>> list = userMapper.selectMaps(wrapper);


        list.forEach(user -> user.forEach((k,v)->{
            // 打印键,打印值
            System.out.println(k + "----" +v);
        }));
    }


    @Test
    public void testSelectObjs() {
        System.out.println(("----- selectObjs method test www.itzhimei.com------"));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.gt("Age", 21);
        List users = userMapper.selectObjs(wrapper);
        users.forEach(System.out::println);
    }

    @Test
    public void testInsert() {
        System.out.println(("----- insert method test ------"));
        User user = new User();
        user.setId(6L);
        user.setAge(20);
        user.setEmail("123@163.com");
        user.setName("张三");
        userMapper.insert(user);
        System.out.println("插入的Id:"+user.getId());
    }

    @Test
    public void testUpdate() {
        System.out.println(("----- update method test ------"));
        User user = userMapper.selectById(5);
        System.out.println("修改前数据:" +user);

        User user2 = new User();
        user2.setEmail("12346@163.com");
        user2.setName("张三");
        QueryWrapper qw = new QueryWrapper();
        qw.eq("ID",5L);
        int result = userMapper.update(user2,qw);
        System.out.println("更新记录数:" + result);

        User user3 = userMapper.selectById(5);
        System.out.println("修改后数据:" +user3);
    }

    @Test
    public void testUpdateById() {
        System.out.println(("----- updateById method test ------"));
        User user = userMapper.selectById(5);
        System.out.println("修改前数据:" +user);

        User user2 = new User();
        user2.setEmail("12346@163.com");
        user2.setName("张三");
        user2.setId(5L);
        int result = userMapper.updateById(user2);
        System.out.println("更新记录数:" + result);

        User user3 = userMapper.selectById(5);
        System.out.println("修改后数据:" +user3);
    }

    @Test
    public void testDeleteById() {
        System.out.println(("----- deleteById method test www.itzhimei.com------"));

        List<User> userList = userMapper.selectList(null);
        System.out.println("-----删除前数据:");
        userList.forEach(System.out::println);

        userMapper.deleteById(5L);
        System.out.println("-----删除后剩余数据:");
        List<User> userList2 = userMapper.selectList(null);
        userList2.forEach(System.out::println);
    }


    @Test
    public void testDeleteByMap() {
        System.out.println(("----- deleteByMap method test ------"));

        List<User> userList = userMapper.selectList(null);
        System.out.println("-----删除前数据:");
        userList.forEach(System.out::println);

        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("Name","Tom");
        userMapper.deleteByMap(columnMap);
        System.out.println("-----删除后剩余数据:");
        List<User> userList2 = userMapper.selectList(null);
        userList2.forEach(System.out::println);
    }

    @Test
    public void testDelete() {
        System.out.println(("----- delete method test ------"));

        List<User> userList = userMapper.selectList(null);
        System.out.println("-----删除前数据:");
        userList.forEach(System.out::println);

        userMapper.delete(null);
        System.out.println("-----删除后剩余数据:");
        List<User> userList2 = userMapper.selectList(null);
        userList2.forEach(System.out::println);
    }

    @Test
    public void testDeleteBatchIds() {
        System.out.println(("----- deleteBatchIds method test www.itzhimei.com------"));

        List<User> userList = userMapper.selectList(null);
        System.out.println("-----删除前数据:");
        userList.forEach(System.out::println);

        List<Long> list = new ArrayList();
        list.add(4L);
        list.add(5L);
        userMapper.deleteBatchIds(list);
        System.out.println("-----删除后剩余数据:");
        List<User> userList2 = userMapper.selectList(null);
        userList2.forEach(System.out::println);
    }

}