一时兴起,准备使用3.0的SpringBoot,跟着网上的教程在整合MyBatis时出错了,搞了许久也没整好,要么是项目启动报错,要么是接口莫名其妙404,无奈之下前往官网,没想到被我找到了解决方法。
相信大家找到的大部分教程中,导入的依赖都是:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>版本号</version>
</dependency>
这个确实没错,但这是针对spring boot2.x版本的,你如果使用的是spring boot3.x的版本 ,应该导入的是这个:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
<!-- 下面这俩版本根据自己的来 -->
<!--Mysql依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- druid数据源驱动 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.20</version>
</dependency>
具体可以查看安装 | MyBatis-Plus
ps:有的朋友可能不会查对应依赖的版本号,可以去:https://mvnrepository.com 这是maven官方仓库的搜索,直接搜对应的artifactId就行了,这里我们找“mybatis-plus-spring-boot3-starter”的版本,一般出来的第一个就是,如果搜出来有好几个相同的,那就注意前面的。
找到对应的版本号替换掉就行了,比如我用的就是最新的3.5.5.
剩下的就是常规的配置了,首先修改配置文件
mybatis-plus:
# 指定 Mapper XML 文件的位置,使用 classpath 通配符指定路径
mapper-locations: classpath:mapper/${project.database}/**/*.xml,classpath:mapper/*.xml
# 指定实体类的包路径,用于自动扫描并注册类型别名
type-aliases-package: top.kirisamemarisa.onebotspring.entity
global-config:
db-config:
id-type: input
# 驼峰下划线转换(将数据库字段的下划线命名规则转换为 Java 实体类属性的驼峰命名规则)
db-column-underline: true
# 刷新 mapper
# refresh-mapper: true
configuration:
# 将 Java 实体类属性的驼峰命名规则转换为数据库字段的下划线命名规则
map-underscore-to-camel-case: true
# 查询结果中包含空值的列,在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 开启 sql 日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 关闭 sql 日志
# log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
创建实体类
package top.kirisamemarisa.onebotspring.entity.system;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class BotConfig {
@TableId(type = IdType.AUTO)
private String id;
private String userId;
private String context;
}
创建Mapper
package top.kirisamemarisa.onebotspring.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import top.kirisamemarisa.onebotspring.entity.system.BotConfig;
@Mapper
public interface BotConfigMapper extends BaseMapper<BotConfig> {
}
创建Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.kirisamemarisa.onebotspring.entity.system.BotConfig">
</mapper>
创建Service
package top.kirisamemarisa.onebotspring.service;
import com.baomidou.mybatisplus.extension.service.IService;
import top.kirisamemarisa.onebotspring.entity.system.BotConfig;
public interface IBotConfigService extends IService<BotConfig> {
BotConfig getBotConfigByUserId(String userId);
}
创建ServiceImpl
package top.kirisamemarisa.onebotspring.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import top.kirisamemarisa.onebotspring.entity.system.BotConfig;
import top.kirisamemarisa.onebotspring.mapper.BotConfigMapper;
import top.kirisamemarisa.onebotspring.service.IBotConfigService;
@Service
public class BotConfigServiceImpl extends ServiceImpl<BotConfigMapper, BotConfig> implements IBotConfigService {
@Resource
private BotConfigMapper botConfigMapper;
@Override
public BotConfig getBotConfigByUserId(String userId) {
QueryWrapper<BotConfig> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("USER_ID", userId);
BotConfig config = botConfigMapper.selectOne(queryWrapper);
if (ObjectUtils.isEmpty(config)) return null;
return config;
}
}
创建Controller
package top.kirisamemarisa.onebotspring.controller;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.kirisamemarisa.onebotspring.entity.system.BotConfig;
import top.kirisamemarisa.onebotspring.service.IBotConfigService;
@CrossOrigin
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private IBotConfigService botConfigService;
@GetMapping("/getConfigById")
public String test() {
System.out.println(">>>>>>>>>");
BotConfig config = botConfigService.getBotConfigByUserId("1001");
System.out.println(config);
return config.getContext();
}
}
最后别忘了配置MapperScan
@SpringBootApplication
@MapperScan("top.kirisamemarisa.onebotspring.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如果你的mapper.xml扫描不到,记得也去pom.xml中排除一下。
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
不出意外的话应该就正常了。