|
1 |
| -#Spring Boot集成MyBatis的基础项目 |
2 |
| - |
3 |
| -#MyBatis3.3.0 |
4 |
| - |
5 |
| -#Spring Boot 1.3.0.RELEASE |
6 |
| - |
7 |
| -项目使用Spring Boot 1.3.0.RELEASE + Mybatis3.3.0 |
8 |
| - |
9 |
| -项目集成了Mybatis分页插件和通用Mapper插件 |
10 |
| - |
11 |
| -项目使用的mysql数据库,根据需要可以切换为其他数据库 |
12 |
| - |
13 |
| -##重要提示 |
14 |
| - |
15 |
| -本项目一开始对 `@AutoConfigureAfter` 的用法有问题,详细内容请看: |
16 |
| - |
17 |
| ->[Spring Boot - 配置排序依赖技巧](http://blog.csdn.net/isea533/article/details/53975720) |
18 |
| -
|
19 |
| -本项目已经按照上文的技巧对包和用法做了修改,基本上可以保证 100% 没错。 |
20 |
| - |
21 |
| -##说明 |
| 1 | +# Spring Boot 集成 MyBatis, 分页插件 PageHelper, 通用 Mapper |
| 2 | + |
| 3 | +- Spring Boot 1.4.3.RELEASE |
| 4 | +- mybatis-spring-boot-starter 1.1.1 |
| 5 | +- mapper-spring-boot-starter 1.0.0 |
| 6 | +- pagehelper-spring-boot-starter |
| 7 | + |
| 8 | +## 项目依赖 |
| 9 | +```xml |
| 10 | +<!--mybatis--> |
| 11 | +<dependency> |
| 12 | + <groupId>org.mybatis.spring.boot</groupId> |
| 13 | + <artifactId>mybatis-spring-boot-starter</artifactId> |
| 14 | + <version>1.1.1</version> |
| 15 | +</dependency> |
| 16 | +<!--mapper--> |
| 17 | +<dependency> |
| 18 | + <groupId>tk.mybatis</groupId> |
| 19 | + <artifactId>mapper-spring-boot-starter</artifactId> |
| 20 | + <version>1.0.0</version> |
| 21 | +</dependency> |
| 22 | +<!--pagehelper--> |
| 23 | +<dependency> |
| 24 | + <groupId>com.github.pagehelper</groupId> |
| 25 | + <artifactId>pagehelper-spring-boot-starter</artifactId> |
| 26 | + <version>1.0.0</version> |
| 27 | +</dependency> |
| 28 | +``` |
22 | 29 |
|
23 |
| -虽然MyBatis官方提供了`mybatis-spring-boot-starter`,但是该配置的可以控制的地方太少,因此短时间不会直接使用该`starter`。 |
| 30 | +## application.yml 配置 |
24 | 31 |
|
25 |
| -在集成MyBatis配置`MapperScannerConfigurer`需要特别注意,将该类单独放在一个配置文件中,例如本项目中的`MyBatisMapperScannerConfig`类: |
| 32 | +完整配置可以参考 [src/main/resources/application.yml](https://github.com/abel533/MyBatis-Spring-Boot/blob/master/src/main/resources/application.yml) ,和 MyBatis 相关的部分配置如下: |
26 | 33 |
|
27 |
| -```java |
28 |
| -@Configuration |
29 |
| -//注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解 |
30 |
| -//MyBatisConfig.class是一个包含了SqlSessionFactory配置的类 |
31 |
| -@AutoConfigureAfter(MyBatisConfig.class) |
32 |
| -public class MyBatisMapperScannerConfig { |
| 34 | +```yaml |
| 35 | +mybatis: |
| 36 | + type-aliases-package: tk.mybatis.springboot.model |
| 37 | + mapper-locations: classpath:mapper/*.xml |
33 | 38 |
|
34 |
| - @Bean |
35 |
| - public MapperScannerConfigurer mapperScannerConfigurer() { |
36 |
| - MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); |
37 |
| - mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); |
38 |
| - mapperScannerConfigurer.setBasePackage("tk.mybatis.springboot.mapper"); |
39 |
| - Properties properties = new Properties(); |
40 |
| - properties.setProperty("mappers", "tk.mybatis.springboot.util.MyMapper"); |
41 |
| - properties.setProperty("notEmpty", "false"); |
42 |
| - properties.setProperty("IDENTITY", "MYSQL"); |
43 |
| - //这里使用的通用Mapper的MapperScannerConfigurer,所有有下面这个方法 |
44 |
| - mapperScannerConfigurer.setProperties(properties); |
45 |
| - return mapperScannerConfigurer; |
46 |
| - } |
| 39 | +mapper: |
| 40 | + mappers: |
| 41 | + - tk.mybatis.springboot.util.MyMapper |
| 42 | + not-empty: false |
| 43 | + identity: MYSQL |
47 | 44 |
|
48 |
| -} |
| 45 | +pagehelper: |
| 46 | + helperDialect: mysql |
| 47 | + reasonable: true |
| 48 | + supportMethodsArguments: true |
| 49 | + params: count=countSql |
49 | 50 | ```
|
50 | 51 |
|
51 |
| -##SSM集成的基础项目 |
52 |
| - |
53 |
| -###https://github.com/abel533/Mybatis-Spring |
54 |
| - |
55 |
| -##MyBatis工具 |
| 52 | +注意 mapper 配置,因为参数名固定,所以接收参数使用的对象,按照 Spring Boot 配置规则,大写字母都变了带横线的小写字母。针对如 IDENTITY(对应i-d-e-n-t-i-t-y)提供了全小写的 identity 配置,如果 IDE 能自动提示,看自动提示即可。 |
56 | 53 |
|
57 |
| -###http://www.mybatis.tk |
| 54 | +注意 pagehelper 配置,因为分页插件根据自己的扩展不同,支持的参数也不同,所以不能用固定的对象接收参数,所以这里使用的 `Map<String,String>`,因此参数名是什么这里就写什么,IDE 也不会有自动提示。 |
58 | 55 |
|
59 |
| -##推荐使用Mybatis通用Mapper3 |
| 56 | +## SSM集成的基础项目 |
| 57 | +>https://github.com/abel533/Mybatis-Spring |
60 | 58 |
|
61 |
| -###https://github.com/abel533/Mapper |
| 59 | +## MyBatis工具 http://www.mybatis.tk |
62 | 60 |
|
63 |
| -##推荐使用Mybatis分页插件PageHelper |
| 61 | +- 推荐使用 Mybatis 通用 Mapper3 https://github.com/abel533/Mapper |
64 | 62 |
|
65 |
| -###https://github.com/pagehelper/Mybatis-PageHelper |
| 63 | +- 推荐使用 Mybatis 分页插件 PageHelper https://github.com/pagehelper/Mybatis-PageHelper |
66 | 64 |
|
67 |
| -##作者信息 |
| 65 | +## 作者信息 |
68 | 66 |
|
69 | 67 | - 作者博客:http://blog.csdn.net/isea533
|
70 | 68 |
|
|
0 commit comments