mybatis-spring-boot
要与 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
作为前缀,可用的配置项如下:
配置项 | 描述 |
---|---|
| MyBatis XML 配置文件的路径。 |
| 指定是否对 MyBatis XML 配置文件的存在进行检查。 |
| XML 映射文件的路径。 |
| 搜索类型别名的包名。(包使用的分隔符是 “ |
| 用于过滤类型别名的父类。如果没有指定,MyBatis 会将所有从 |
| 搜索类型处理器的包名。(包使用的分隔符是 “ |
| SQL 执行器类型: |
| 默认的脚本语言驱动(模板引擎),此功能需要与 mybatis-spring 2.0.2 以上版本一起使用。 |
| 可在外部配置的 MyBatis 配置项。指定的配置项可以被用作 MyBatis 配置文件和 Mapper 文件的占位符。 |
| 是否启用 mapper bean 的延迟初始化。设置 |
| 通过自动配置扫描的 mapper 组件的默认作用域。该功能需要与 mybatis-spring 2.0.6 以上版本一起使用。 |
| 设置是否注入 |
| MyBatis Core 提供的 |
| MyBatis |
| MyBatis |
| MyBatis |
Last updated