文末视频讲解
SpringBoot的版本是2.2.0
一、整合Mybatis
1-1、引入pom文件
<dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.19version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druid-spring-boot-starterartifactId> <version>1.1.23version> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>2.1.3version> 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;@Mapperpublic interface TestMapper{ Frame selectById(@Param("idq") int id);}
TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <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;@RestControllerpublic class TestController { @Autowired private TestMapper testMapper; @GetMapping("/test") public Frame fun(){ return testMapper.selectById(1); }}
二、整合Mybatis-Plus
2-1、引入pom
<dependency> <groupId>com.baomidougroupId> <artifactId>mybatis-plus-boot-starterartifactId> <version>3.2.0version> 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;@Mapperpublic interface TestMapper extends BaseMapper { 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