mybatis-plus整合springboot

准备工作

  • JDK 8+
  • Maven

引入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

配置 MapperScan 注解

@Configuration
@MapperScan("com.mgyboom.stmp.mapper") // 设置mapper接⼝的扫描包
public class MybatisPlusConfig {
  // ...省略其他配置
}

配置application.properties

# datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

# mybatis-plus
# 加载全局配置文件,mybatis原生支持的属性可配置在sqlMapConfig.xml
#mybatis-plus.config-location=classpath:sqlMapConfig.xml
# 加载映射配置文件,Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)
mybatis-plus.mapper-locations=classpath*:mapper/*.xml
# 起别名
mybatis-plus.type-aliases-package=com.mgyboom.stmp.pojo
# 自动驼峰映射,默认true,该参数不能和mybatis-plus.config-location同时存在
mybatis-plus.configuration.map-underscore-to-camel-case=true
# 全局开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认true,该参数不能和mybatis-plus.config-location同时存在
mybatis-plus.configuration.cache-enabled=true
# 全局配置主键生成策略为自增长,默认assign_id
mybatis-plus.global-config.db-config.id-type=auto
# 全局配置表名前缀
mybatis-plus.global-config.db-config.table-prefix=tb_
# 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置@TableLogic)
mybatis-plus.global-config.db-config.logic-delete-field=id_del
# 逻辑已删除值(默认为 1)
mybatis-plus.global-config.db-config.logic-delete-value=1
# 逻辑未删除值(默认为 0)
mybatis-plus.global-config.db-config.logic-not-delete-value=0

代码示例

1、BaseMapper通用CRUD

2、常用配置

3、条件构造器QueryWrapper

4、领域模型ActiveRecord

5、常用插件

6、SQL注入器

7、自动填充

8、逻辑删除

9、代码生成器

功能示例见代码:https://gitee.com/mgyboom/springboot-mp

posted @ 2021-01-24 18:45  mgyboom  阅读(164)  评论(0编辑  收藏  举报