Mybatis和MyBatisPlus共存(老项目升级mybatis-plus)
泛域名ssl证书 239元1年送1个月、单域名39元1年,Sectigo(原Comodo证书)全球可信证书,强大的兼容性,高度安全性,如有问题7天内可退、可开发票
加微信VX 18718058521 备注SSL证书
【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价
一、整合Mybatis
1-1、引入pom文件
<!-- MySQL 连接驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <!-- mysql数据库连接池 pool --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> <!-- Spring Boot Mybatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency>
1-2、添加application.yml配置文件
添加MySql配置文件
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://ip:prot/database?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC username: root password: 123456 dbcp2: min-idle: 5 # 数据库连接池最小维持连接数 initial-size: 5 # 初始连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待链接获取的最大超时时间
添加Mybatis配置文件
mybatis: type-aliases-package: com.xdx97.frame # 所有Entity别名类所在包 mapper-locations: classpath:mappers/**/*Mapper.xml # mapper映射文件 - classpath:mybatis/mapper/**/*.xml
1-3、测试
Frame
public class Frame { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }
TestMapper
import com.xdx97.frame.bean.Frame; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface TestMapper{ Frame selectById(@Param("idq") int id); }
TestMapper.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="com.xdx97.frame.mapper.TestMapper"> <select id="selectById" parameterType="java.lang.Integer" resultType="com.xdx97.frame.bean.Frame"> SELECT id,name FROM frame WHERE id = #{idq} </select> </mapper>
TestController
import com.xdx97.frame.bean.Frame; import com.xdx97.frame.mapper.TestMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private TestMapper testMapper; @GetMapping("/test") public Frame fun(){ return testMapper.selectById(1); } }
二、整合Mybatis-Plus
2-1、引入pom
<!-- Spring Boot Mybatis 增强插件 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency>
2-2、修改yml文件
mybatis: type-aliases-package: com.xdx97.frame # 所有Entity别名类所在包
mybatis-plus: mapper-locations: classpath:mappers/**/*Mapper.xml # mapper映射文件 - classpath:mybatis/mapper/**/*.xml
2-3、修改TestMapper
继承BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.xdx97.frame.bean.Frame; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface TestMapper extends BaseMapper<Frame> { Frame selectById(@Param("idq") int id); }
这样,baseMapper里面的方法我们就可以直接使用了
三、其它
3-1、如果你需要用到枚举,那么你需要多配置一个handle
配置位置如下,具体handle如果写,百度一下
mybatis-plus: mapper-locations: classpath:mappers/**/*Mapper.xml # mapper映射文件 - classpath:mybatis/mapper/**/*.xml type-handlers-package:
3-2、如果引入Mbatis-Plus后出现了如下异常
Invalid bound statement (not found):
如果你的xml等一些配置关联没有写错的话,那么请考虑下面这种情况
如果引用mybatis-plus-boot-starter 依赖,需要配置 mybatis-plus.mapper-locations
如果引用mybatis-plus 依赖,需要配置 mybatis.mapper-locations