02 - 集成MybatisPlus
约 739 字大约 2 分钟
2025-04-08
程序集成
修改pom引入依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
<exclusions>
<exclusion>
<artifactId>mybatis-spring</artifactId>
<groupId>org.mybatis</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
问题记录
正常直接引入mybatis-plus-boot-starter即可,但是SpringBoot3.0以上版本,启动报错:
Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
解决办法:
参考【mybatis-spring-issues 855】得知:
在 Spring Boot 3.0后的 版本中FactoryBeanRegistrySupport#getTypeForFactoryBeanFromAttributes方法已变更,如果 factoryBeanObjectType 不是 ResolvableType 或 Class 类型会抛出 IllegalArgumentException 异常。
此时因为 factoryBeanObjectType 是 String 类型,不符合条件而抛出异常。
项目中使用的 mybatis-plus-boot-starter 是当前最新版本 3.5.5,但 mybatis-spring 为2.1.2
但版本已不兼容,
兼容对照表:
MyBatis-Spring-Boot-Starter、MyBatis-Spring、Spring Boot、Java 版本兼容对照如下表:
所以引入Mybatis-plus 时需要排除 Spring-mybatis ,并自行指定版本。
参考文档: https://blog.csdn.net/Hello_World_QWP/article/details/135771075
Bean named ‘ddlApplicationRunner‘ is expected to be of type ‘org.sprin
参考文档: https://blog.csdn.net/weixin_46211609/article/details/135552632
升级mybatis-plus版本
FAQ: 待处理 当前Spring-boot 应该可以不指定MySQL 驱动版本。
修改配置文件
spring:
...
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spads_ai?serverTimezone=GMT%2B8
username: root
password: root
mybatis-plus:
# 开启日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 开启sqlrunner
global-config:
enable-sql-runner: true
编写相关代码
entity
package cn.spads.ai.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value="s_third_config")
public class ThirdConfig {
@TableId(value="id", type = IdType.AUTO)
private Integer id;
@TableField(value="name")
private String name;
@TableField(value="base_url")
private String baseUrl;
@TableField(value="api_token")
private String apiToken;
@TableField(value="category")
private byte category;
}
mapper
package cn.spads.ai.mapper;
import cn.spads.ai.entity.ThirdConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ThirdMapper extends BaseMapper<ThirdConfig> {
}
service
interface
package cn.spads.ai.service;
import cn.spads.ai.entity.ThirdConfig;
import com.baomidou.mybatisplus.extension.service.IService;
public interface ThirdConfigService extends IService<ThirdConfig> {
}
class
package cn.spads.ai.service.impl;
import cn.spads.ai.entity.ThirdConfig;
import cn.spads.ai.mapper.ThirdMapper;
import cn.spads.ai.service.ThirdConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class ThirdConfigServiceImpl extends ServiceImpl<ThirdMapper, ThirdConfig> implements ThirdConfigService {
}
controller
package cn.spads.ai.controller;
import cn.spads.ai.entity.ThirdConfig;
import cn.spads.ai.service.ThirdConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class ThirdConfigController {
@Autowired
private ThirdConfigService thirdConfigService;
@GetMapping("/config/third/list")
@ResponseBody
public List<ThirdConfig> getThirdConfigs(){
return this.thirdConfigService.list();
}
}
访问测试
application.yml 配置应用访问端口为 8080
server:
port: 8080
所以访问 http://localhost:8080/config/third/list 查看结果
为指定对象增加逻辑删除过程
修改表结构
ALTER TABLE s_ai_session
ADD COLUMN is_deleted TINYINT DEFAULT 0 COMMENT '逻辑删除标识,0表示未删除,1表示已删除';
修改类属性
// 逻辑删除字段,0表示未删除,1表示已删除
@TableLogic(value = "0", delval = "1")
private Integer isDeleted;
修改Mybatis plus 配置
#开启日志
mybatis-plus:
...
global-config:
db-config:
logic-not-delete-value: 0 # 未删除值
logic-delete-value: 1 # 已删除值
检查和修改自定义SQL
@Mapper
public interface AiSessionMapper extends BaseMapper<AiSession> {
@Select("SELECT * FROM s_ai_session where is_deleted = 0 ")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "messages", column = "id", many = @Many(select = "cn.spads.ai.mapper.AiMessageMapper.findMessagesBySessionId"))
})
List<AiSession> findAll();
}
注:默认的方法,查询、修改会自动应用逻辑删除的过滤和修改。