MyBatis-PLUS使用教程

引言

这是一篇mybatis-plus的使用记录博客,源码等分析就不做了,帮助学习mybatis-plus的人懂得使用技巧

在此之前我转载一篇文章让大家顺带学习一下题外知识,逼格拉满,规范开发,职场上的加分点, 懒得写我就直接丢传送门了, 领域模型
这里还有什么 失血模型 贫血模型 充血模型 胀血模型我就不说了,我也不太会哈哈哈哈

MyBatis-PLUS

介绍

官方骚话
愿景
我们的愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。

  • MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

  • 官网:https://baomidou.com

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑

  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作

  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求

  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错

  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题

  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作

  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )

  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用

  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询

  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库

  • 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询

  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库,具体支持情况如下,如果不在下列表查看分页部分教程 PR 您的支持。

  • MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb

  • 达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库

框架结构

开始使用

引入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0+ 版本</version>
    <relativePath/>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>最新版本</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

在 application.yml 配置文件中添加数据库的相关配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/databasName?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
    username: xxx
    password: xxx
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 0
      maximum-pool-size: 20
      idle-timeout: 10000
      auto-commit: true
      connection-test-query: select 1
# 显示SQL语句
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

配置mybatis-plus配置类,我就直接贴出来了:


/**
 * @author LGH
 */
@Configuration
@MapperScan({"com.**.**.**.dao"})
public class MybatisPlusConfig implements Condition {

    private RedisTemplate redisTemplate;

    /**
     * 分页插件
     * @return PaginationInterceptor
     */
    @Bean
    @ConditionalOnMissingBean
    public PaginationInterceptor paginationInterceptor() {
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("", "");
        return new PaginationInterceptor();
    }
    /**
     * 逻辑删除插件
     *
     * @return LogicSqlInjector
     */
    @Bean
    @ConditionalOnMissingBean
    public ISqlInjector sqlInjector() {
        return new DefaultSqlInjector();
    }

    /**
     * 乐观锁插件
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    @Bean
    @Conditional(MybatisPlusConfig.class)
    public RedisTemplate getRedisTemplate(){
        return new RedisTemplate();
    }

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        return true;
    }
}

实体类(领域模型):

@Data
@TableName("order")
public class OrderPO extends BasePO {
// 一般都会有一个BasePO里面记录固定规范属性,id,createTime等和实现Serializable接口
// BasePO也可以存在copy作用的方法,便于领域模型对象之间的转换
    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 日期格式化
    private Date createTime;

    /**
     * 店铺名称
     */
    @TableField(exist = false) // 申明不属于表的字段(一般不会用到)
    private String shopName;
}

使用规范

service:继承IService接口
mapper:继承BaseMapper接口

查询方式

创建查询条件

条件创建对象的方式
推荐使用逼格高的流式方式创建

Wrappers.lambdaQuery().eq() …

查询:

LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();

services:

this.list(wrapper);
this.getById(wrapper);
this.getOne(wrapper);

controller调用service对象, 同理自己带入就可以了
xxxService.list(wrapper);

更新:

LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();

方法同上

太简单不想写了

CRUD操作表格

功能自定义接口MP接口
新增boole save(T t)int inster(T t)
删除boole delete(int d)int deleteById(Serializable id
修改boole update(T t)int updatedById(T t)
根据id查询T getById(int id)T selectById(Serializable id)
查询全部List getAll()List<> selectList()
分页查询PageInfo getAll(int page, int sizeIPage selectPage(IPage page)
按照条件查询List getAll(Condition condition)IPage selectPage(Wrapper queryWrapper)

重点说一下分页查询

IPage<OrderPO> page = new Page<>(area.getCurrent() - 1,10);
orderService.page(page, new LambdaQueryWrapper<OrderPO>());
// 参数1是page对象,参数2是我们的条件wrapper对象

干货来了,核心教学

具体使用中我们会遇到很多场景,会让我们各种复杂语句的查询变得无从下手,举例几个我熟悉的复杂查询,剩下的让你们自己举一反三

<=,>=

<=

Children ge(boolean condition, R column, Object val)

>=

Children le(boolean condition, R column, Object val);

msg_type = xxx and (xxx = xx or xxx = xx)

有点子长

Wrappers.<Order>lambdaQuery()
.eq(ObjectUtils.isNotEmpty(orderDTO.getMsgType()), OrderPO::getMsgType, orderDTO.getMsgType())
.and(StringUtils.isNotEmpty(orderDTO.getProdName()), 
wrapper -> wrapper.eq(OrderPO::getProdName, orderDTO.getProdName())
.or().eq(OrderPO::getKeyword, orderDTO.getKeyword()))

and方法的第二个参数是个lambda表达式

连表查询动态条件

@Select("select * from 
    		order o 
            inner join order_sub s in o.id = s.order_id 
            ${qw.customSqlSegment}")
OrderPO getXxxx(@Param("qw") QueryWrapper wrapper);

${qw.customSqlSegment}
很重点, 这就是你wrapper对象创建后拼接的where和后面的条件部分,可以满足代码优美又能够动态条件的大sql使用场景,基本告别xml

结束吧,感觉太简单了懒得细聊,坚持写完就不错了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus 是一个基于 MyBatis 的增强工具,提供了很多便捷的功能和增强特性,可以简化开发过程。下面是一个简单的 MyBatis-Plus 使用教程: 1. 引入依赖:首先,在你的项目中引入 MyBatis-Plus 的依赖。你可以在 Maven 的 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>{version}</version> </dependency> ``` 请确保将 `{version}` 替换为你希望使用的 MyBatis-Plus 版本号。 2. 配置数据源:在项目的配置文件中,配置数据库连接信息和数据源。你可以使用 Spring Boot 的配置文件(如 application.properties 或 application.yml)来配置。 3. 创建实体类:创建与数据库表对应的实体类,并使用注解标识字段与表的映射关系。MyBatis-Plus 提供了 `@TableName`、`@TableId`、`@TableField` 等注解来实现这些映射关系。 4. 创建 Mapper 接口:创建一个继承自 `BaseMapper` 的接口,并且为该接口指定实体类类型。这个接口将用于定义数据库操作的方法。 5. 编写 SQL 语句:在 Mapper 接口中,使用 MyBatis 的注解或 XML 来编写 SQL 语句。你可以使用 MyBatis-Plus 提供的方法来完成常用的 CRUD 操作,也可以自定义 SQL 语句。 6. 使用 MyBatis-Plus 进行数据库操作:在 Service 层或其他地方,通过注入 Mapper 接口的对象来进行数据库的增删改查操作。MyBatis-Plus 提供了很多便捷的方法,如 `selectList`、`insert`、`update`、`delete` 等。 以上是一个简单的 MyBatis-Plus 使用教程。你可以按照这些步骤来使用 MyBatis-Plus 进行开发,根据具体需求进行配置和扩展。更详细的使用教程和示例代码可以参考 MyBatis-Plus 官方文档或者搜索其他在线教程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值