跳到主要内容

MyBatis Plus 插件

集成

Maven

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

常用配置

SpringBoot 常用的配置

application.yml 中配置

mybatis-plus:
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapper-locations: classpath:com/xxx/xxx/mapper/*.xml
# 注册别名
type-aliases-package: com.xxx.xxx.model
configuration:
# 日志实现
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
# 删除逻辑字段名
logic-delete-field: deleteStatus
# 逻辑未删除值
logic-delete-value: 0
# 逻辑删除值
logic-not-delete-value: 1

使用过程中遇到的问题

使用 BaseMapper 中的 selectById 方法报错

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxxxxx.selectById

未在实体类中主键字段添加 @TableId 字段

Inferred type ‘E‘ for type parameter ‘E‘ is not within its bound;

xxxMapper.xml 重写了该方法,删除即可

Invalid bound statement (not found)

mybatis 未找到对应的 mapper.xml 文件。 mapper.xml 文件的名称需要与接口相同。

在 springboot mybatis-plus 项目中,通过 来指定 mapper.xml 文件的位置。

mybatis-plus:
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapper-locations: classpath:xxx/xxx/xxx/mapper/*.xml

updateById 时,更新字段为 null

  1. 自定义 SQL

    • *Mapper.xml
    • @Update 注解
  2. 设置全局 FieldStrategy 为 IGNORED

mybatis-plus:
global-config:
#字段策略 0:"忽略判断",1:"非 NULL 判断",2:"非空判断"
field-strategy: 0
  1. 对指定字段单独设置 FieldStrategy

在实体类中要想设置为 null 的字段添加注解 @TableField(updateStrategy = FieldStrategy.IGNORED)