MybatisPlus入门之BaseMapper使用讲解

package cn.learn.mybatisplus.mapper;

import cn.learn.mybatisplus.pojo.User;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.List;

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

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect(){
        List users = this.userMapper.selectList(null);
        for (User user : users) {
            System.out.println(user);
        }
    }

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

    @Test
    public void testSelectByLike(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name", "o");
        List list = this.userMapper.selectList(wrapper);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testSelectByLe(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.le("age", 20);
        List list = this.userMapper.selectList(wrapper);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testSave(){
        User user = new User();
        user.setAge(25);
        user.setEmail("[email protected]");
        user.setName("zhangsan");
        int count = this.userMapper.insert(user);
        System.out.println("新增数据成功! count => " + count);
    }

    @Test
    public void testDelete(){
        this.userMapper.deleteById(7L);
        System.out.println("删除成功!");
    }

    @Test
    public void testUpdate(){
        User user = new User();
        user.setId(6L);
        user.setName("lisi");
        this.userMapper.updateById(user);
        System.out.println("修改成功!");
    }

    @Test
    public void testSelectPage() {
        Page page = new Page<>(2, 2);
        IPage userIPage = this.userMapper.selectPage(page, null);
        System.out.println("总条数 ------> " + userIPage.getTotal());
        System.out.println("当前页数 ------> " + userIPage.getCurrent());
        System.out.println("当前每页显示数 ------> " + userIPage.getSize());
        List records = userIPage.getRecords();
        for (User user : records) {
            System.out.println(user);
        }
    }

}
package cn.learn.mybatisplus.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class User {

    @TableId(value = "ID", type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;

}
package cn.learn.mybatisplus;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@MapperScan("cn.learn.mybatisplus.mapper") //设置mapper接口的扫描包
@SpringBootApplication
public class MyApplication {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

 

你可能感兴趣的:(MybatisPlus)