MyBatis-Spring-Boot-Starter
Introduction
What is MyBatis-Spring-Boot-Starter?
The MyBatis-Spring-Boot-Starter help you build quickly MyBatis applications on top of the Spring Boot.
MyBatis-Spring-Boot-Starter帮助你快速构建基于SpringBoot的Mybatis应用;
Requirements
Installation
To use the MyBatis-Spring-Boot-Starter module, you just need to include the mybatis-spring-boot-autoconfigure.jar
file and its dependencies(mybatis.jar
, mybatis-spring.jar
and etc …) in the classpath.
为了使用MyBatis-Spring-Boot-Starter,你仅需要将mybatis-spring-boot-autoconfigure依赖加入;
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency>
Quick Setup
As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory
and at least one mapper interface.
正如你所知,在Spring中使用Mybatis,你需要至少一个SqlSessionFactory 、至少一个Mapper;
MyBatis-Spring-Boot-Starter will:
1、Autodetect an existing DataSource
自动检测已存在的DataSource
2、Will create and register an instance of a SqlSessionFactory
passing that DataSource
as an input using the SqlSessionFactoryBean
通过DataSource,使用SqlSessionFactoryBean创建一个SqlSessionFactory
3、Will create and register an instance of a
SqlSessionTemplate
got out of the SqlSessionFactory
通过SqlSessionFactory 创建 SqlSessionTemplate;
4、Auto-scan your mappers, link them to the SqlSessionTemplate
and register them to Spring context so they can be injected into your beans
自动扫描Mapper,并将Mapper与SqlSessionTemplate链接 ,注入到Spring 的Bean;
Advanced scanning
The MyBatis-Spring-Boot-Starter will search, by default, for mappers marked with the @Mapper
annotation.
MyBatis-Spring-Boot-Starter将会默认查找被@Mapper标记的Mapper;
You may want to specify a custom annotation or a marker interface for scanning. If so, you must use the @MapperScan
annotation.
如果你想要自定义mapper注解,必须使用@MapperScan进行扫描;
The MyBatis-Spring-Boot-Starter will not start the scanning process if it finds at least one MapperFactoryBean
in the Spring’s context so if you want to stop the scanning at all you should register your mappers explicitly with @Bean
methods.
如果在Spring中找到一个以上的MapperFactoryBean,MyBatis-Spring-Boot-Starter 将不会开启扫描,你必须显式注册Mapper;
Configuration
As any other Spring Boot application a MyBatis-Spring-Boot-Application configuration parameters are stored inside the application.properties
(or application.yml
).
与其他SpringBoot应用一样,MyBatis-Spring-Boot应用配置参数也是在application.properties
(or application.yml
);
MyBatis uses the prefix mybatis
for its properties.
在配置时,Mybatis使用mybatis前缀;
config-location |
Location of MyBatis xml config file. mybatis配置文件路径 |
check-config-location |
Indicates whether perform presence check of the MyBatis xml config file. |
mapper-locations |
Locations of Mapper xml config file. mapper.xml路径 |
type-aliases-package |
Packages to search for type aliases. (Package delimiters are “,; \t\n ”) |
type-aliases-super-type |
The super class for filtering type alias. If this not specifies, the MyBatis deal as type alias all classes that searched from type-aliases-package . |
type-handlers-package |
Packages to search for type handlers. (Package delimiters are “,; \t\n ”) |
executor-type |
Executor type: SIMPLE , REUSE , BATCH |
default-scripting-language-driver |
The default scripting language driver class. This feature requires to use together with mybatis-spring 2.0.2+. |
configuration-properties |
Externalized properties for MyBatis configuration. Specified properties can be used as placeholder on MyBatis config file and Mapper file. For detail see the MyBatis reference page. |
lazy-initialization |
Whether enable lazy initialization of mapper bean. Set true to enable lazy initialization. This feature requires to use together with mybatis-spring 2.0.2+. |
mapper-default-scope |
Default scope for mapper bean that scanned by auto-configure. This feature requires to use together with mybatis-spring 2.0.6+. |
mybatis.inject-sql-session-on-mapper-scan |
Set whether inject a SqlSessionTemplate or SqlSessionFactory bean (If you want to back to the behavior of 2.2.1 or before, specify false ). If you use together with spring-native, should be set true (default). |
configuration.* |
Property keys for Configuration bean provided by MyBatis Core. About available nested properties see the MyBatis reference page. NOTE: This property cannot be used at the same time with the config-location . |
scripting-language-driver.thymeleaf.* |
Property keys for ThymeleafLanguageDriverConfig bean provided by MyBatis Thymeleaf. About available nested properties see the MyBatis Thymeleaf reference page. |
scripting-language-driver.freemarker.* |
Properties keys for FreeMarkerLanguageDriverConfig bean provided by MyBatis FreeMarker. About available nested properties see the MyBatis FreeMarker reference page. This feature requires to use together with mybatis-freemarker 1.2.0+. |
scripting-language-driver.velocity.* |
Properties keys for VelocityLanguageDriverConfig bean provided by MyBatis Velocity. About available nested properties see the MyBatis Velocity reference page. This feature requires to use together with mybatis-velocity 2.1.0+. |