SpringBoot整合MyBatis

SpringBoot整合MyBatis

整合的前提是,必须先配置好了数据源

依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis-spring-boot-starter.version}</version>
</dependency>

目前最新版本: 2.1.0

yam核心l配置(配置类参考: MybatisProperties

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations:
    - classpath*:mapper/**/*-mapper.xml
    - classpath*:mapper/**/*-mapper-ext.xml
  • config-location
    指定类路径下mybatis配置文件的地址
  • mapper-locations
    指定 mapper.xml 映射文件的地址,可以有多个。可以是类路径,或者文件系统。** 表示匹配多级目录。

classpath:classpath*:
前者只会在当前工程的类路径下检索mapper文件。后者会在所有类路径下检索mapper文件。包括第三方的依赖库。

Mapper接口的扫描配置

@SpringBootApplication
@MapperScan(basePackages = { "io.springboot.mapper" }, annotationClass = Mapper.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@MapperScan
通过 basePackages 指定要mapper接口所在包,可以有多个
annotationClass属性 不是必须的,可以指定一个注解类。表示仅仅加载该注解标识的Mapper接口。

@MapperScans
可以通过该注解来定义多个 @MapperScan