mybatis-spring-boot

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>

要与 Spring 一起使用 MyBatis,至少需要一个 SqlSessionFactory 和一个 mapper 接口。

MyBatis-Spring-Boot-Starter 将会:

  • 自动探测存在的 DataSource

  • 使用 SqlSessionFactoryBean 创建并注册一个 SqlSessionFactory 的实例,并将探测到的 DataSource 作为数据源

  • 创建并注册一个从 SqlSessionFactory 中得到的 SqlSessionTemplate 的实例

  • 自动扫描 mapper,将它们与 SqlSessionTemplate 相关联,并将它们注册到Spring 的环境(context)中去,这样它们就可以被注入到你的 bean 中

“扫描”的进阶用法

MyBatis-Spring-Boot-Starter 将默认搜寻带有 @Mapper 注解的 mapper 接口。

使用 @MapperScan 注解指定扫描的 mapper 接口,此时可以不用添加@Mapper 注解,两者并不会冲突,因此可以同时使用。

配置

MyBatis 使用 mybatis 作为前缀,可用的配置项如下:

配置项描述

config-location

MyBatis XML 配置文件的路径。

check-config-location

指定是否对 MyBatis XML 配置文件的存在进行检查。

mapper-locations

XML 映射文件的路径。

type-aliases-package

搜索类型别名的包名。(包使用的分隔符是 “,; \t\n”)

type-aliases-super-type

用于过滤类型别名的父类。如果没有指定,MyBatis 会将所有从type-aliases-package搜索到的类作为类型别名处理。

type-handlers-package

搜索类型处理器的包名。(包使用的分隔符是 “,; \t\n”)

executor-type

SQL 执行器类型: SIMPLE, REUSE, BATCH

default-scripting-language-driver

默认的脚本语言驱动(模板引擎),此功能需要与 mybatis-spring 2.0.2 以上版本一起使用。

configuration-properties

可在外部配置的 MyBatis 配置项。指定的配置项可以被用作 MyBatis 配置文件和 Mapper 文件的占位符。

lazy-initialization

是否启用 mapper bean 的延迟初始化。设置 true 以启用延迟初始化。此功能需要与 mybatis-spring 2.0.2 以上版本一起使用。

mapper-default-scope

通过自动配置扫描的 mapper 组件的默认作用域。该功能需要与 mybatis-spring 2.0.6 以上版本一起使用。

inject-sql-session-on-mapper-scan

设置是否注入 SqlSessionTemplateSqlSessionFactory 组件 (如果你想回到 2.2.1 或之前的行为,请指定 false )。如果你和 spring-native 一起使用,应该设置为 true (默认)。

configuration.*

MyBatis Core 提供的Configuration 组件的配置项。注:此属性不能与 config-location 同时使用。

scripting-language-driver.thymeleaf.*

MyBatis ThymeleafLanguageDriverConfig 组件的 properties keys。

scripting-language-driver.freemarker.*

MyBatis FreemarkerLanguageDriverConfig 组件的 properties keys。这个特性需要与 mybatis-freemarker 1.2.0 以上版本一起使用。

scripting-language-driver.velocity.*

MyBatis VelocityLanguageDriverConfig 组件的 properties keys。这个特性需要与 mybatis-velocity 2.1.0 以上版本一起使用。

Last updated