Skip to content

Commit d6c7f5f

Browse files
committed
改为纯 starter 项目
1 parent 43c8048 commit d6c7f5f

File tree

7 files changed

+84
-271
lines changed

7 files changed

+84
-271
lines changed

README.md

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,68 @@
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+
```
2229

23-
虽然MyBatis官方提供了`mybatis-spring-boot-starter`,但是该配置的可以控制的地方太少,因此短时间不会直接使用该`starter`
30+
## application.yml 配置
2431

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 相关的部分配置如下
2633

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
3338

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
4744

48-
}
45+
pagehelper:
46+
helperDialect: mysql
47+
reasonable: true
48+
supportMethodsArguments: true
49+
params: count=countSql
4950
```
5051
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 能自动提示,看自动提示即可。
5653
57-
###http://www.mybatis.tk
54+
注意 pagehelper 配置,因为分页插件根据自己的扩展不同,支持的参数也不同,所以不能用固定的对象接收参数,所以这里使用的 `Map<String,String>`,因此参数名是什么这里就写什么,IDE 也不会有自动提示。
5855

59-
##推荐使用Mybatis通用Mapper3
56+
## SSM集成的基础项目
57+
>https://github.com/abel533/Mybatis-Spring
6058

61-
###https://github.com/abel533/Mapper
59+
## MyBatis工具 http://www.mybatis.tk
6260

63-
##推荐使用Mybatis分页插件PageHelper
61+
- 推荐使用 Mybatis 通用 Mapper3 https://github.com/abel533/Mapper
6462

65-
###https://github.com/pagehelper/Mybatis-PageHelper
63+
- 推荐使用 Mybatis 分页插件 PageHelper https://github.com/pagehelper/Mybatis-PageHelper
6664

67-
##作者信息
65+
## 作者信息
6866

6967
- 作者博客:http://blog.csdn.net/isea533
7068

pom.xml

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
<parent>
1212
<groupId>org.springframework.boot</groupId>
1313
<artifactId>spring-boot-starter-parent</artifactId>
14-
<version>1.3.0.RELEASE</version>
14+
<version>1.4.3.RELEASE</version>
1515
</parent>
1616

1717
<properties>
1818
<java.version>1.8</java.version>
1919
<!-- 依赖版本 -->
2020
<mybatis.version>3.3.1</mybatis.version>
2121
<mybatis.spring.version>1.2.4</mybatis.spring.version>
22-
<mapper.version>3.3.6</mapper.version>
23-
<pagehelper.version>5.0.0-rc2</pagehelper.version>
2422
</properties>
2523

2624
<dependencies>
@@ -73,38 +71,24 @@
7371
<version>1.0.11</version>
7472
</dependency>
7573

76-
<!--Mybatis-->
74+
<!--mybatis-->
7775
<dependency>
78-
<groupId>org.mybatis</groupId>
79-
<artifactId>mybatis</artifactId>
80-
<version>${mybatis.version}</version>
76+
<groupId>org.mybatis.spring.boot</groupId>
77+
<artifactId>mybatis-spring-boot-starter</artifactId>
78+
<version>1.1.1</version>
8179
</dependency>
80+
<!--mapper-->
8281
<dependency>
83-
<groupId>org.mybatis</groupId>
84-
<artifactId>mybatis-spring</artifactId>
85-
<version>${mybatis.spring.version}</version>
86-
</dependency>
87-
<!-- Mybatis Generator -->
88-
<dependency>
89-
<groupId>org.mybatis.generator</groupId>
90-
<artifactId>mybatis-generator-core</artifactId>
91-
<version>1.3.2</version>
92-
<scope>compile</scope>
93-
<optional>true</optional>
82+
<groupId>tk.mybatis</groupId>
83+
<artifactId>mapper-spring-boot-starter</artifactId>
84+
<version>1.0.0</version>
9485
</dependency>
95-
<!--分页插件-->
86+
<!--pagehelper-->
9687
<dependency>
9788
<groupId>com.github.pagehelper</groupId>
98-
<artifactId>pagehelper</artifactId>
99-
<version>${pagehelper.version}</version>
89+
<artifactId>pagehelper-spring-boot-starter</artifactId>
90+
<version>1.0.0</version>
10091
</dependency>
101-
<!--通用Mapper-->
102-
<dependency>
103-
<groupId>tk.mybatis</groupId>
104-
<artifactId>mapper</artifactId>
105-
<version>${mapper.version}</version>
106-
</dependency>
107-
10892
</dependencies>
10993

11094
<build>

src/main/java/tk/mybatis/autoconfigure/MyBatisConfig.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

src/main/java/tk/mybatis/autoconfigure/MyBatisMapperScannerConfig.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/main/java/tk/mybatis/springboot/Application.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package tk.mybatis.springboot;
22

3+
import org.mybatis.spring.annotation.MapperScan;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56
import org.springframework.stereotype.Controller;
67
import org.springframework.web.bind.annotation.RequestMapping;
78
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
89
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import tk.mybatis.springboot.util.MyMapper;
911

1012
/**
1113
* @author liuzh
@@ -14,6 +16,7 @@
1416
@Controller
1517
@EnableWebMvc
1618
@SpringBootApplication
19+
@MapperScan(basePackages = "tk.mybatis.springboot.mapper", markerInterface = MyMapper.class)
1720
public class Application extends WebMvcConfigurerAdapter {
1821

1922
public static void main(String[] args) {

src/main/resources/META-INF/spring.factories

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)