Spring boot 集成通用mapper

1.3 spring boot

abel533 edited this page on 7 Mar ·  2 revisions

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>

Maven central

最新版本号如上所示,你也可以从下面地址查看:

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 !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值