mybatis-plus基本操作大全

条件构造器AbstractWrapper(公共父类)

  • QueryWrapper

  • UpdateWrapper

    用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件
    

allEq:all等于

allEq(Map params)
allEq(Map params, boolean null2IsNull)
allEq(boolean condition, Map params, boolean null2IsNull)

//例1: allEq({id:1,name:"老王",age:null})--->id = 1 and name = '老王' and age is null

//例2: allEq({id:1,name:"老王",age:null}, false)--->id = 1 and name = '老王'
    
//个别参数说明:
//params : key为数据库字段名,value为字段值
//null2IsNull : 为true则在map的value为null时调用 isNull 方法,为false时则忽略value为null的

eq:等于

eq(R column, Object val)
eq(boolean condition, R column, Object val)
//eq("name", "老王")--->name = '老王'

ne :不等于<>

ne(R column, Object val)
ne(boolean condition, R column, Object val)
//ne("name", "老王")--->name <> '老王'

ge:大于等于>=

ge(R column, Object val)
ge(boolean condition, R column, Object val)
//ge("age", 18)--->age >= 18

It:小于<

lt(R column, Object val)
lt(boolean condition, R column, Object val)
//lt("age", 18)--->age < 18

Ie:小于等于<=

le(R column, Object val)
le(boolean condition, R column, Object val)
//le("age", 18)--->age <= 18

between:值1 <=xx<= 值2

between(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)
//between("age", 18, 30)--->age between 18 and 30

notBetween:NOT BETWEEN 值1 AND 值2

notBetween(R column, Object val1, Object val2)
notBetween(boolean condition, R column, Object val1, Object val2)
//notBetween("age", 18, 30)--->age not between 18 and 30

like:LIKE '%值%'

like(R column, Object val)
like(boolean condition, R column, Object val)
//like("name", "王")--->name like '%王%'

notLike:NOT LIKE '%值%'

notLike(R column, Object val)
notLike(boolean condition, R column, Object val)
//notLike("name", "王")--->name not like '%王%'

likeLeft:LIKE '%值'

likeLeft(R column, Object val)
likeLeft(boolean condition, R column, Object val)
//likeLeft("name", "王")--->name like '%王'

likeRight:LIKE '值%'

likeRight(R column, Object val)
likeRight(boolean condition, R column, Object val)
//likeRight("name", "王")--->name like '王%'

isNull

isNull(R column)
isNull(boolean condition, R column)
    //isNull("name")--->name is null

isNotNull

isNotNull(R column)
isNotNull(boolean condition, R column)
    //isNotNull("name")--->name is not null

in:字段 IN (value.get(0), value.get(1), ...)

in(R column, Collection value)
in(boolean condition, R column, Collection value)
//用法1:in("age",{1,2,3})--->age in (1,2,3)
//用法2:in("age", 1, 2, 3)--->age in (1,2,3)推荐

notIn:字段 NOT IN (value.get(0), value.get(1), ...)

notIn(R column, Collection value)
notIn(boolean condition, R column, Collection value)

//用法1:notIn("age",{1,2,3})--->age not in (1,2,3)
//用法2:notIn("age", 1, 2, 3)--->age not in (1,2,3)

inSql:字段 IN ( sql语句 )

inSql(R column, String inValue)
inSql(boolean condition, R column, String inValue)

//用法1:inSql("age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)
//用法2:inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)

notInSql:字段 NOT IN ( sql语句 )

notInSql(R column, String inValue)
notInSql(boolean condition, R column, String inValue)

//用法1:notInSql("age", "1,2,3,4,5,6")--->age not in (1,2,3,4,5,6)
//用法2:notInSql("id", "select id from table where id < 3")--->id not in (select id from table where id < 3)

or:拼接 OR

or()
or(boolean condition)

//queryWrapper.eq("id",2);
//queryWrapper.or();主动调用or表示紧接着下一个方法不是用and连接!(不调用or,默认使用and连接)
//queryWrapper.eq("name","小红");
//where id = 2 or name = '小红'  :写了or
//where id = 2 and name = '小红'  :没写or默认为and

plus,分页

  • 写一个配置类,并注册到ioc容器中

//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")//这里扫描了之后,就不用再在启动类上面重复扫描了
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

  • 在dao层需要加上分页方法

    public interface UserMapper {//可以继承或者不继承BaseMapper
        /**
         * 

    * 查询 : 根据state状态查询用户列表,分页显示 *

    * * @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承Page实现自己的分页对象) * @param state 状态 * @return 分页对象 */ IPage selectPageVo(Page page, Integer state); }
  • UserMapper.xml 等同于编写一个普通 list 查询,mybatis-plus 自动替你分页

    
    
  • UserServiceImpl.java 调用分页方法

    public IPage selectUserPage(Page page, Integer state) {
        // 不进行 count sql 优化,解决 MP 无法自动优化 SQL 问题,这时候你需要自己查询 count 部分
        // page.setOptimizeCountSql(false);
        // 当 total 为小于 0 或者设置 setSearchCount(false) 分页插件不会进行 count 查询
        // 要点!! 分页返回的对象与传入的对象是同一个
        return userMapper.selectPageVo(page, state);
    }
    

简单的介绍一下什么是Page

//源码
public class Page implements IPage {

    private static final long serialVersionUID = 8545996863226528798L;

    //查询数据列表
    protected List records = Collections.emptyList();

    //总数
    protected long total = 0;
    //每页显示条数,默认 10
    protected long size = 10;

    //当前页
    protected long current = 1;

    //排序字段信息
    @Getter
    @Setter
    protected List orders = new ArrayList<>();

    //自动优化 COUNT SQL
    protected boolean optimizeCountSql = true;
    
    //是否进行 count 查询
    protected boolean isSearchCount = true;
    
    //是否命中count缓存
    protected boolean hitCount = false;
    
    //countId
    @Getter
    @Setter
    protected String countId;
    
    // countId
    @Getter
    @Setter
    protected Long maxLimit;
/**
 *说明具体的意思
 *records 用来存放查询出来的数据
 *total 返回记录的总数
 *size 每页显示条数,默认 10
 *current 当前页,默认1
 *orders 排序字段信息
 *optimizeCountSql 自动优化 COUNT SQL,默认true
 *isSearchCount 是否进行 count 查询,默认true
 *hitCount 是否命中count缓存,默认false
 */
    
 //构建一个Page
  Page page = new Page(pageNum,pageSize)

你可能感兴趣的:(mybatis-plus基本操作大全)