springboot-mybatis

Author doingdone

简介

springboot集成mybaits时,有mybatis官方提供的mybatis-spring-boot-starter,但mapper不可通用,开发工作量大,
因此使用mapper-spring-boot-starter的通用mapper。

通用Mapper都可以极大的方便开发人员。
可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。
极其方便的使用MyBatis单表的增删改查。
支持单表操作,不支持通用的多表联合查询。

github
wiki

pom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-starter.version}</version>
</dependency>

<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper-starter.version}</version>
</dependency>

配置

  1. 数据源配置

    参考druid-spring-boot-starter配置即可

  2. mybatis配置

config-location与configuration只可配置一项

1
2
3
4
5
6
config-location: classpath:mybatis-config.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package:
mapper-locations:
  1. mapper配置

详见wiki

mappers配置项注意

如果只使用通用mapper,可以不配置mappers,如果使用了MysqlMapper等其他mapper,而且自己定义了BaseMapper,要设置mappers。(一般BaseMapper继承Mapper和其他mapper,比如MysqlMapper)

1
2
3
mapper:
mappers:
- com.daijk.demo.dao.base.BaseMapper

开撸

关于@MapperScan

包名必填,并且mappers配置的类不能在包下

对象关系映射

  • 主键 @Id
  • 列 @Column @ColumnType
请我吃糖~
打赏二维码