mybatis-plus

为什么要学mybatis-plus?

mybatis在持久层框架中还是比较火的,一般项目都是基于ssm。虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦。mybatis-plus就很好的解决了这个问题。

mybatis-plus简介

Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。这是官方给的定义,关于mybatis-plus的更多介绍及特性,可以参考mybatis-plus官网。那么它是怎么增强的呢?其实就是它已经封装好了一些crud方法,我们不需要再写xml了,直接调用这些方法就行,就类似于JPA。
mybatis-plus文档:https://mp.baomidou.com/guide/page.html

如何使用Mybatis-Plus?

第一步:导入依赖

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.3</version>
        </dependency>

第二步:编写操作实体类的 Mapper 类。直接继承 BaseMapper,这是 mybatis-plus 封装好的类。
Employee是你要操作的实体类

public interface EmplopyeeDao extends BaseMapper<Employee> {
}

第三步:启动类上添加mapper扫描,扫描mapper包

@MapperScan("com.mapper")

第四步:调用mapper中的方法
在这里插入图片描述
参数
entity:实体类,通过给实体类设置值,来作为条件。
如查找方法 EmplopyeeMapper.select(entity)

条件构造器Wrapper:

QueryWrapper:用于查询

QueryWrapper<User> wrapper = new QueryWrapper<>(); 
    wrapper.eq("id", 6);   // 查询的条件

UpdateWrapper:查询的条件以及更新的字段
// 查询的条件以及更新的字段
UpdateWrapper wrapper = new UpdateWrapper<>();
wrapper.eq(“id”, 6).set(“age”, 22);

1、allEq、eq 等于

allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)

个别参数说明:

params : key为数据库字段名,value为字段值
null2IsNull : 为true则在map的value为null时调用 isNull 方法,为false时则忽略value为null的

例子:allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null})--->name = '老王' and age is null

2、ne 不等于
3、gt 大于、ge大于等于

: gt("age", 18)--->age > 18

4、lt 小于、le 小于等于
5、between
年龄范围:30>=age>=18

例:between("age", 18, 30)--->age between 18 and 30

6、like 模糊查询

: like("name", "王")--->name like '%王%'

分页

@SpringBootApplication
@MapperScan("com.igeekhome.dao")
public class MybatisPlusDemo01Application {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusDemo01Application.class, args);
    }
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}

dao

public interface UserMapper extends BaseMapper<User> {
}

service

@Service
public class Userbiz extends ServiceImpl<UserMapper, User> {
}

测试类

@SpringBootTest
public class user {

    @Resource
    Userbiz userbiz;
    @Test
    void page(){
        Page<User> ipage =new Page(1,1);
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.orderByDesc("age");
        Page page = userbiz.page(ipage, queryWrapper);
        List records = page.getRecords();
        System.out.println(records);
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值