MyBatis Plus 使用示例一 (BaseMapper)

  1. 项目版本:

Spring Boot 版本:2.2.0
MyBatis Plus 版本:3.2.0
Mysql版本: 8.0

  1. 项目准备:

项目目录结构
MyBatis Plus 使用示例一 (BaseMapper)_第1张图片
MyBatis Plus 使用示例一 (BaseMapper)_第2张图片

  1. BaseMapper 使用示例:

这里是引用

package com.imooc.demo;

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 com.imooc.demo.entity.User;
import com.imooc.demo.mapper.UserMapper;
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 TestMapper {

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取所有用户
     */
    @Test
    public void selectList(){
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }

    /**
     * 根据指定条件 查询符合条件的用户 (selectList传参数的查询形式)
     */
    @Test
    public void selectAllList(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("age",23);
        List<User> userList = userMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
    }

    /**
     * 根据 id 查询指定用户
     */
    @Test
    public void selectById(){
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

    /**
     * 根据 ID 批量查询用户
     */
    @Test
    public void selectBatchIds(){
        List<Integer> integers = Arrays.asList(1,2,3);
        List<User> userList = userMapper.selectBatchIds(integers);
        userList.forEach(System.out::println);
    }

    /**
     * 根据 Map搜索条件查询指定条件下的用户
     */
    @Test
    public void selectByMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("name","Tom");
        List<User> userList = userMapper.selectByMap(map);
        userList.forEach(System.out::println);
    }

    /**
     * wrapper 查询一条数据
     */
    @Test
    public void selectOne(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name","Tom");
        User user = userMapper.selectOne(queryWrapper);
        System.out.println(user);
    }

    /**
     * 根据指定条件查询符合条件的记录数
     */
    @Test
    public void selectCount(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("age",20);
        Integer count = userMapper.selectCount(queryWrapper);
        System.out.println(count);
    }



    /**
     * 根据指定条件查询用户
     */
    @Test
    public void selectMaps(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("age",23);
        List<Map<String, Object>> mapList = userMapper.selectMaps(queryWrapper);
        mapList.forEach(System.out::println);
    }

    /**
     * 根据指定条件查询符合条件的用户 (只返回第一个字段值)
     */
    @Test
    public void selectObjs(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("age",23);
        List<Object> list = userMapper.selectObjs(queryWrapper);
        list.forEach(System.out::println);
    }

    /**
     * 查询所有用户并分页
     */
    @Test
    public void selectPage(){
        Page<User> page = new Page<>(1,2);
        IPage<User> userIPage = userMapper.selectPage(page, null);
        System.out.println("总记录数:"+userIPage.getTotal());
        System.out.println("总页数:"+userIPage.getPages());
        List<User> userList = userIPage.getRecords();
        userList.forEach(System.out::println);
    }

    /**
     * 查询所有用户并分页
     */
    @Test
    public void selectMapsPage(){
        Page<User> page = new Page<>(1,2);
        IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, null);
        System.out.println("总记录数:"+mapIPage.getTotal());
        System.out.println("总页数:"+mapIPage.getPages());
        List<Map<String, Object>> records = mapIPage.getRecords();
        records.forEach(System.out::println);
    }

    /**
     * 插入一条记录
     */
    @Test
    public void insert(){
        User user = new User();
        user.setUsername("zhangsan");
        user.setEmail("[email protected]");
        user.setAge(33);
        int i = userMapper.insert(user);
        System.out.println(i);
    }

    /**
     * 根据ID删除一条记录
     */
    @Test
    public void deleteById(){
        int i = userMapper.deleteById(8);
        System.out.println(i);
    }

    /**
     * 根据指定 Map 条件删除记录
     */
    @Test
    public void deleteByMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("age",23);
        int i = userMapper.deleteByMap(map);
        System.out.println(i);
    }

    /**
     * 根据指定 Wrapper 条件删除记录
     */
    @Test
    public void delete(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name","B");
        int i = userMapper.delete(queryWrapper);
        System.out.println(i);
    }

    /**
     * 根据指定ID批量删除记录
     */
    @Test
    public void deleteBatchIds(){
        List<Integer> integers = Arrays.asList(1, 2);
        int i = userMapper.deleteBatchIds(integers);
        System.out.println(i);
    }

    /**
     * 根据指定条件更新记录
     */
    @Test
    public void update(){
        User user = new User();
        user.setEmail("[email protected]");
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","Jack");
        int update = userMapper.update(user, updateWrapper);
        System.out.println(update);
    }

    /**
     * 根据 指定ID 更新记录
     */
    @Test
    public void updateById(){
        User user = new User();
        user.setId(1L);
        user.setEmail("[email protected]");
        int i = userMapper.updateById(user);
        System.out.println(i);
    }






}

你可能感兴趣的:(MyBatis,Plus,Java,MyBatis)