1.3 spring boot
abel533 edited this page on 7 Mar ·
2 revisions
Pages 21
Clone this wiki locally
1.3 Spring Boot 集成
Spring Boot 在微服务领域中已经成为主流。
这里介绍通用 Mapper 如何同 Spring Boot 进行集成。
为了能适应各种情况的用法,这里也提供了多种集成方式,基本上分为两大类。
- 基于 starter 的自动配置
- 基于
@MapperScan
注解的手工配置
1.3.1 mapper-spring-boot-starter
你只需要添加通用 Mapper 提供的 starter 就完成了最基本的集成,依赖如下:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>版本号</version>
</dependency>
最新版本号如上所示,你也可以从下面地址查看:
http://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter
注意:引入该 starter 时,和 MyBatis 官方的 starter 没有冲突,但是官方的自动配置不会生效!
如果你需要对通用 Mapper 进行配置,你可以在 Spring Boot 的配置文件中配置 mapper.
前缀的配置。
例如在 yml 格式中配置:
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper
- tk.mybatis.mapper.common.Mapper2
notEmpty: true
在 properties 配置中:
mapper.mappers=tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.Mapper2
mapper.notEmpty=true
由于 Spring Boot 支持 Relax 方式的参数,因此你在配置 notEmpty
时更多的是用 not-empty
,也只有在 Spring Boot 中使用的时候参数名不必和配置中的完全一致。
如果你对 Spring Boot 的 Environment 了解,你可以用 Spring Boot 支持的所有方式进行配置。
关于通用 Mapper 支持的所有配置请看后续的文档。
1.3.2 @MapperScan
注解配置
你可以给带有 @Configuration
的类配置该注解,或者直接配置到 Spring Boot 的启动类上,如下:
@tk.mybatis.spring.annotation.MapperScan(basePackages = "扫描包")
@SpringBootApplication
public class SampleMapperApplication implements CommandLineRunner {
注意:这里使用的 tk.mybatis.spring.annotation.MapperScan
!