0.搭建环境
0.1 IDE:IntelliJ IDEA 2017.3.2 x64
0.2 database:MySQL 5.7.20
0.3 JDK:1.8.131
1.Create New Project–>Spring Initlizr
- 选中相关配置
2.1 Web–>Web
2.2 SQL–>MySQL&JDBC&Mybatis
2.3 Template Engines–>Freemarker
点击Next–>Finish即可构建成功!
3.将application.properties修改为application.yml文件
3.1 选中该文件右键找到Refator
3.2 将后缀该为yml文件即可
- 添加相关pom依赖
4.1 pagehelper分页插件
1
2
3
4
5
6
7 1<!-- SpringBoot整合Mybatis-pagehelper分页插件 -->
2<dependency>
3 <groupId>com.github.pagehelper</groupId>
4 <artifactId>pagehelper-spring-boot-starter</artifactId>
5 <version>1.2.3</version>
6</dependency>
7
4.2 alibaba的druid数据库连接池
1
2
3
4
5
6
7 1 <!-- SpringBoot使用alibaba的druid数据库连接池 -->
2<dependency>
3 <groupId>com.alibaba</groupId>
4 <artifactId>druid-spring-boot-starter</artifactId>
5 <version>1.1.10</version>
6</dependency>
7
4.3 jackson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1<!--jackson -->
2<dependency>
3 <groupId>com.fasterxml.jackson.core</groupId>
4 <artifactId>jackson-core</artifactId>
5</dependency>
6
7<dependency>
8 <groupId>com.fasterxml.jackson.core</groupId>
9 <artifactId>jackson-databind</artifactId>
10</dependency>
11
12<dependency>
13 <groupId>com.fasterxml.jackson.datatype</groupId>
14 <artifactId>jackson-datatype-joda</artifactId>
15</dependency>
16<dependency>
17 <groupId>com.fasterxml.jackson.module</groupId>
18 <artifactId>jackson-module-parameter-names</artifactId>
19</dependency>
20
4.4 完整pom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-freemarker</artifactId>
4 </dependency>
5 <dependency>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-jdbc</artifactId>
8 </dependency>
9 <dependency>
10 <groupId>org.springframework.boot</groupId>
11 <artifactId>spring-boot-starter-web</artifactId>
12 </dependency>
13 <dependency>
14 <groupId>org.mybatis.spring.boot</groupId>
15 <artifactId>mybatis-spring-boot-starter</artifactId>
16 <version>1.3.2</version>
17 </dependency>
18
19 <dependency>
20 <groupId>mysql</groupId>
21 <artifactId>mysql-connector-java</artifactId>
22 <scope>runtime</scope>
23 </dependency>
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-starter-test</artifactId>
27 <scope>test</scope>
28 </dependency>
29
30 <!-- SpringBoot整合Mybatis-pagehelper分页插件 -->
31 <dependency>
32 <groupId>com.github.pagehelper</groupId>
33 <artifactId>pagehelper-spring-boot-starter</artifactId>
34 <version>1.2.3</version>
35 </dependency>
36
37 <!-- SpringBoot使用alibaba的druid数据库连接池 -->
38 <dependency>
39 <groupId>com.alibaba</groupId>
40 <artifactId>druid-spring-boot-starter</artifactId>
41 <version>1.1.10</version>
42 </dependency>
43
44 <!--jackson -->
45 <dependency>
46 <groupId>com.fasterxml.jackson.core</groupId>
47 <artifactId>jackson-core</artifactId>
48 </dependency>
49
50 <dependency>
51 <groupId>com.fasterxml.jackson.core</groupId>
52 <artifactId>jackson-databind</artifactId>
53 </dependency>
54
55 <dependency>
56 <groupId>com.fasterxml.jackson.datatype</groupId>
57 <artifactId>jackson-datatype-joda</artifactId>
58 </dependency>
59 <dependency>
60 <groupId>com.fasterxml.jackson.module</groupId>
61 <artifactId>jackson-module-parameter-names</artifactId>
62 </dependency>
63
4.5 在pom文件bulid标签中添加相关配置
4.5.1 添加resources配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 1 <resources>
2 <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
3 <resource>
4 <directory>src/main/java</directory>
5 <includes>
6 <include>**/*.xml</include>
7 </includes>
8 </resource>
9 <!--解决resouces文件放入target文件夹的问题-->
10 <resource>
11 <directory>src/main/resources</directory>
12 <includes>
13 <include>*.properties</include>
14 <include>*.xml</include>
15 <include>*.yml</include>
16 <!--添加模板支持-->
17 <include>templates/*.ftl</include>
18 <!--<include>*.ftl</include>-->
19 </includes>
20 </resource>
21 </resources>
22
4.6.1 mybatis-generator 自动生成代码插件 添加到bulid–>plugins中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 1 <!-- mybatis generator 自动生成代码插件 -->
2 <plugin>
3 <groupId>org.mybatis.generator</groupId>
4 <artifactId>mybatis-generator-maven-plugin</artifactId>
5 <version>1.3.2</version>
6 <dependencies>
7 <!--使用Mybatis-generator插件不能使用太高版本的mysql驱动 -->
8 <dependency>
9 <groupId>mysql</groupId>
10 <artifactId>mysql-connector-java</artifactId>
11 <version>5.1.44</version>
12 </dependency>
13 </dependencies>
14 <configuration>
15 <overwrite>true</overwrite>
16 </configuration>
17 </plugin>
18
5.添加相关配置到application.yml
5.1 修改启动端口号
1
2
3
4 1#配置启动端口号 只要端口不冲突 可以随便取
2server:
3 port: 2001
4
5.2 指定项目名
1
2
3
4 1 # 设置启动项目名 与项目名一致即可 也可以随便取
2 servlet:
3 context-path: /gowinxin
4
5.3 配置数据源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 1#配置数据源
2spring:
3 datasource:
4 driver-class-name: com.mysql.cj.jdbc.Driver
5 #请修改为自己的数据库并且修改username&password
6 url: jdbc:mysql://192.168.43.82:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
7 username: root
8 password: 123
9 # 使用druid数据源
10 type: com.alibaba.druid.pool.DruidDataSource
11 filters: stat
12 maxActive: 20
13 initialSize: 1
14 maxWait: 60000
15 minIdle: 1
16 timeBetweenEvictionRunsMillis: 60000
17 minEvictableIdleTimeMillis: 300000
18 validationQuery: select 'x'
19 testWhileIdle: true
20 testOnBorrow: false
21 testOnReturn: false
22 poolPreparedStatements: true
23 maxOpenPreparedStatements: 20
24
5.4 开放静态资源
1
2
3
4
5 1#请放在spring节点下
2# 开放静态资源
3 resources:
4 static-locations: /static/**
5
5.5 配置freemarker相关配置
1
2
3
4
5
6
7
8
9
10
11
12 1# 配置freemarkerx相关配置
2 freemarker:
3 request-context-attribute: req #req访问request
4 suffix: .ftl #后缀名
5 content-type: text/html
6 enabled: true
7 cache: false #缓存配置
8 template-loader-path: classpath:/templates/ #模板加载路径 按需配置
9 charset: UTF-8 #编码格式
10 settings:
11 number_format: '0.##' #数字格式化,无小数点
12
5.6 配置Mybatis相关信息
1
2
3
4
5
6
7
8
9 1mybatis:
2 mapper-locations: classpath*:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
3 type-aliases-package: com.go.gowinxin.model
4 configuration:
5# 开启驼峰命名
6 map-underscore-to-camel-case: true
7# 设置打印日志为sql、参数、查询结果的
8 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
9
5.7 添加pagehelper分页插件相关信息
1
2
3
4
5
6
7 1#pagehelper分页插件
2pagehelper:
3 helperDialect: mysql
4 reasonable: true
5 supportMethodsArguments: true
6 params: count=countSql
7
5.8 完整配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 1#配置启动端口号 只要端口不冲突 可以随便取
2server:
3 port: 2001
4 # 设置启动项目名 与项目名一致即可 也可以随便取
5 servlet:
6 context-path: /dwinxin
7
8#配置数据源
9spring:
10 datasource:
11 driver-class-name: com.mysql.cj.jdbc.Driver
12 #请修改为自己的数据库并且修改username&password
13 url: jdbc:mysql://192.168.43.82:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
14 username: root
15 password: 123
16 # 使用druid数据源
17 type: com.alibaba.druid.pool.DruidDataSource
18 filters: stat
19 maxActive: 20
20 initialSize: 1
21 maxWait: 60000
22 minIdle: 1
23 timeBetweenEvictionRunsMillis: 60000
24 minEvictableIdleTimeMillis: 300000
25 validationQuery: select 'x'
26 testWhileIdle: true
27 testOnBorrow: false
28 testOnReturn: false
29 poolPreparedStatements: true
30 maxOpenPreparedStatements: 20
31 #请放在spring节点下
32 # 开放静态资源
33 resources:
34 static-locations: /static/**
35 # 配置freemarkerx相关配置
36 freemarker:
37 request-context-attribute: req #req访问request
38 suffix: .ftl #后缀名
39 content-type: text/html
40 enabled: true
41 cache: false #缓存配置
42 template-loader-path: classpath:/templates/ #模板加载路径 按需配置
43 charset: UTF-8 #编码格式
44 settings:
45 number_format: '0.##' #数字格式化,无小数点
46
47mybatis:
48 mapper-locations: classpath*:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
49 type-aliases-package: com.go.gowinxin.model
50 configuration:
51# 开启驼峰命名
52 map-underscore-to-camel-case: true
53# 设置打印日志为sql、参数、查询结果的
54 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
55
56
57#pagehelper分页插件
58pagehelper:
59 helperDialect: mysql
60 reasonable: true
61 supportMethodsArguments: true
62 params: count=countSql
63
6.创建相关的包
- utils
- model
- mapper
- service
- controller
注:该包都要创建在后缀Application.java文件下
7.在
后缀Application.java文件下加上的@MapperScan注解 指定对应的包名
这时候就可以启动测试啦,如果没报错表示配置成功啦!
8.开发
8.1 通过Mybatis逆向工程生成mapper&model
8.1.1 创建generatorConfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE generatorConfiguration PUBLIC
3 "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
5<generatorConfiguration>
6
7 <!-- 导入配置属性 -->
8 <properties resource="jdbc.properties"></properties>
9 <!-- !!!! Driver Class Path !!!! -->
10 <classPathEntry location="${jdbc.driverLocation}"/>
11
12 <context id="context" targetRuntime="MyBatis3">
13 <commentGenerator>
14 <!--去除多余注释-->
15 <property name="suppressAllComments" value="true"/>
16 <property name="suppressDate" value="true"/>
17 </commentGenerator>
18
19 <!-- !!!! Database Configurations !!!! -->
20 <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
21 password="${jdbc.}"/>
22
23 <javaTypeResolver>
24 <property name="forceBigDecimals" value="false"/>
25 </javaTypeResolver>
26
27 <!-- !!!! Model Configurations !!!! -->
28 <javaModelGenerator targetPackage="com.go.gowinxin.model" targetProject="src/main/java">
29 <property name="enableSubPackages" value="false"/>
30 <property name="trimStrings" value="true"/>
31 </javaModelGenerator>
32
33 <!-- !!!! Mapper XML Configurations !!!! -->
34 <sqlMapGenerator targetPackage="com.go.gowinxin.mapper" targetProject="src/main/java">
35 <property name="enableSubPackages" value="false"/>
36 </sqlMapGenerator>
37
38 <!-- !!!! Mapper Interface Configurations !!!! -->
39 <javaClientGenerator targetPackage="com.go.gowinxin.mapper" targetProject="src/main/java" type="XMLMAPPER">
40 <property name="enableSubPackages" value="false"/>
41 </javaClientGenerator>
42
43 <!-- !!!! Table Configurations !!!! -->
44 <!--<table tableName="t_user" domainObjectName="User"-->
45 <!--enableCountByExample="false"-->
46 <!--enableDeleteByExample="false"-->
47 <!--enableSelectByExample="false"-->
48 <!--enableUpdateByExample="false"/>-->
49
50 <!--<table tableName="t_config_file_first_kind" domainObjectName="ConfigFileFirstKind"-->
51 <!--enableCountByExample="false"-->
52 <!--enableDeleteByExample="false"-->
53 <!--enableSelectByExample="false"-->
54 <!--enableUpdateByExample="false"/>-->
55
56 <!--<table tableName="t_config_file_second_kind" domainObjectName="ConfigFileSecondKind"-->
57 <!--enableCountByExample="false"-->
58 <!--enableDeleteByExample="false"-->
59 <!--enableSelectByExample="false"-->
60 <!--enableUpdateByExample="false"/>-->
61
62 <!--<table tableName="t_config_file_third_kind" domainObjectName="ConfigFileThirdKind"-->
63 <!--enableCountByExample="false"-->
64 <!--enableDeleteByExample="false"-->
65 <!--enableSelectByExample="false"-->
66 <!--enableUpdateByExample="false"/>-->
67
68 <!--<table tableName="t_config_question_first_kind" domainObjectName="ConfigQuestionFirstKind"-->
69 <!--enableCountByExample="false"-->
70 <!--enableDeleteByExample="false"-->
71 <!--enableSelectByExample="false"-->
72 <!--enableUpdateByExample="false"/>-->
73
74 <!--<table tableName="t_config_question_second_kind" domainObjectName="ConfigQuestionSecondKind"-->
75 <!--enableCountByExample="false"-->
76 <!--enableDeleteByExample="false"-->
77 <!--enableSelectByExample="false"-->
78 <!--enableUpdateByExample="false"/>-->
79
80 <!--<table tableName="t_config_public_char" domainObjectName="ConfigPublicChar"-->
81 <!--enableCountByExample="false"-->
82 <!--enableDeleteByExample="false"-->
83 <!--enableSelectByExample="false"-->
84 <!--enableUpdateByExample="false"/>-->
85
86 <!--<table tableName="t_config_primary_key" domainObjectName="ConfigPrimaryKey"-->
87 <!--enableCountByExample="false"-->
88 <!--enableDeleteByExample="false"-->
89 <!--enableSelectByExample="false"-->
90 <!--enableUpdateByExample="false"/>-->
91
92 <!--<table tableName="t_config_major_kind" domainObjectName="ConfigMajorKind"-->
93 <!--enableCountByExample="false"-->
94 <!--enableDeleteByExample="false"-->
95 <!--enableSelectByExample="false"-->
96 <!--enableUpdateByExample="false"/>-->
97
98
99 <!--<table tableName="t_config_major" domainObjectName="ConfigMajor"-->
100 <!--enableCountByExample="false"-->
101 <!--enableDeleteByExample="false"-->
102 <!--enableSelectByExample="false"-->
103 <!--enableUpdateByExample="false"/>-->
104
105
106 <!--<table tableName="t_human_file_dig" domainObjectName="HumanFileDig"-->
107 <!--enableCountByExample="false"-->
108 <!--enableDeleteByExample="false"-->
109 <!--enableSelectByExample="false"-->
110 <!--enableUpdateByExample="false"/>-->
111
112
113 <!--<table tableName="t_human_file" domainObjectName="HumanFile"-->
114 <!--enableCountByExample="false"-->
115 <!--enableDeleteByExample="false"-->
116 <!--enableSelectByExample="false"-->
117 <!--enableUpdateByExample="false"/>-->
118
119
120 <!--<table tableName="t_salary_standard_details" domainObjectName="SalaryStandardDetails"-->
121 <!--enableCountByExample="false"-->
122 <!--enableDeleteByExample="false"-->
123 <!--enableSelectByExample="false"-->
124 <!--enableUpdateByExample="false"/>-->
125
126 <!--<table tableName="t_salary_standard" domainObjectName="SalaryStandard"-->
127 <!--enableCountByExample="false"-->
128 <!--enableDeleteByExample="false"-->
129 <!--enableSelectByExample="false"-->
130 <!--enableUpdateByExample="false"/>-->
131
132
133 <!--<table tableName="t_bonus" domainObjectName="Bonus"-->
134 <!--enableCountByExample="false"-->
135 <!--enableDeleteByExample="false"-->
136 <!--enableSelectByExample="false"-->
137 <!--enableUpdateByExample="false"/>-->
138
139
140 <!--<table tableName="t_training" domainObjectName="Training"-->
141 <!--enableCountByExample="false"-->
142 <!--enableDeleteByExample="false"-->
143 <!--enableSelectByExample="false"-->
144 <!--enableUpdateByExample="false"/>-->
145
146 <!--<table tableName="t_major_change" domainObjectName="MajorChange"-->
147 <!--enableCountByExample="false"-->
148 <!--enableDeleteByExample="false"-->
149 <!--enableSelectByExample="false"-->
150 <!--enableUpdateByExample="false"/>-->
151
152 <!--<table tableName="t_salary_grant" domainObjectName="SalaryGrant"-->
153 <!--enableCountByExample="false"-->
154 <!--enableDeleteByExample="false"-->
155 <!--enableSelectByExample="false"-->
156 <!--enableUpdateByExample="false"/>-->
157
158 <!--<table tableName="t_salary_grant_details" domainObjectName="SalaryGrantDetails"-->
159 <!--enableCountByExample="false"-->
160 <!--enableDeleteByExample="false"-->
161 <!--enableSelectByExample="false"-->
162 <!--enableUpdateByExample="false"/>-->
163
164
165 <!--<table tableName="t_engage_major_release" domainObjectName="EngageMajorRelease"-->
166 <!--enableCountByExample="false"-->
167 <!--enableDeleteByExample="false"-->
168 <!--enableSelectByExample="false"-->
169 <!--enableUpdateByExample="false"/>-->
170
171
172 <!--<table tableName="t_engage_exam_details" domainObjectName="EngageExamDetails"-->
173 <!--enableCountByExample="false"-->
174 <!--enableDeleteByExample="false"-->
175 <!--enableSelectByExample="false"-->
176 <!--enableUpdateByExample="false"/>-->
177
178 <!--<table tableName="t_engage_exam" domainObjectName="EngageExam"-->
179 <!--enableCountByExample="false"-->
180 <!--enableDeleteByExample="false"-->
181 <!--enableSelectByExample="false"-->
182 <!--enableUpdateByExample="false"/>-->
183
184
185 <!--<table tableName="t_engage_answer" domainObjectName="EngageAnswer"-->
186 <!--enableCountByExample="false"-->
187 <!--enableDeleteByExample="false"-->
188 <!--enableSelectByExample="false"-->
189 <!--enableUpdateByExample="false"/>-->
190
191
192 <!--<table tableName="t_engage_answer_details" domainObjectName="EngageAnswerDetails"-->
193 <!--enableCountByExample="false"-->
194 <!--enableDeleteByExample="false"-->
195 <!--enableSelectByExample="false"-->
196 <!--enableUpdateByExample="false"/>-->
197
198 <!--<table tableName="t_engage_subjects" domainObjectName="EngageSubjects"-->
199 <!--enableCountByExample="false"-->
200 <!--enableDeleteByExample="false"-->
201 <!--enableSelectByExample="false"-->
202 <!--enableUpdateByExample="false"/>-->
203
204
205 <!--<table tableName="t_engage_resume" domainObjectName="EngageResume"-->
206 <!--enableCountByExample="false"-->
207 <!--enableDeleteByExample="false"-->
208 <!--enableSelectByExample="false"-->
209 <!--enableUpdateByExample="false"/>-->
210
211
212 <!--<table tableName="t_engage_interview" domainObjectName="EngageInterview"-->
213 <!--enableCountByExample="false"-->
214 <!--enableDeleteByExample="false"-->
215 <!--enableSelectByExample="false"-->
216 <!--enableUpdateByExample="false"/>-->
217
218
219 <!--<table tableName="t_tree_node" domainObjectName="TreeNode"-->
220 <!--enableCountByExample="false"-->
221 <!--enableDeleteByExample="false"-->
222 <!--enableSelectByExample="false"-->
223 <!--enableUpdateByExample="false"/>-->
224
225 <!--<table tableName="t_sys_user" domainObjectName="SysUser"-->
226 <!--enableCountByExample="false"-->
227 <!--enableDeleteByExample="false"-->
228 <!--enableSelectByExample="false"-->
229 <!--enableUpdateByExample="false"/>-->
230
231 <!--<table tableName="t_sys_role" domainObjectName="SysRole"-->
232 <!--enableCountByExample="false"-->
233 <!--enableDeleteByExample="false"-->
234 <!--enableSelectByExample="false"-->
235 <!--enableUpdateByExample="false"/>-->
236
237 <!--<table tableName="t_sys_permission" domainObjectName="SysPermission"-->
238 <!--enableCountByExample="false"-->
239 <!--enableDeleteByExample="false"-->
240 <!--enableSelectByExample="false"-->
241 <!--enableUpdateByExample="false"/>-->
242
243 <!--<table tableName="t_sys_user_role" domainObjectName="SysUserRole"-->
244 <!--enableCountByExample="false"-->
245 <!--enableDeleteByExample="false"-->
246 <!--enableSelectByExample="false"-->
247 <!--enableUpdateByExample="false"/>-->
248
249 <table tableName="t_book" domainObjectName="Book"
250 enableCountByExample="false"
251 enableDeleteByExample="false"
252 enableSelectByExample="false"
253 enableUpdateByExample="false"/>
254
255
256 </context>
257</generatorConfiguration>
258
8.1.2 创建maven执行项 命令:mybatis-generator:generate -e
执行逆向生成即可!
8.2 书写service层
-
IBookService
-
BookServiceImpl
8.3 测试service层
8.3.1 书写BaseTestCase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1package com.go.gowinxin.base.test;
2
3import org.junit.Before;
4import org.junit.runner.RunWith;
5import org.springframework.boot.test.context.SpringBootTest;
6import org.springframework.test.context.junit4.SpringRunner;
7
8/**
9 * Create on 2018/9/22 on Dong
10 */
11@RunWith(SpringRunner.class)
12@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
13public class BaseTestCase {
14
15
16 @Before
17 public void before() {
18
19 }
20
21}
22
23
8.3.2 测试service类继承与BaseTestCase类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 1package com.go.dwinxin.service;
2
3import com.go.dwinxin.BaseTestCase;
4import com.go.dwinxin.model.Book;
5import org.junit.Test;
6import org.springframework.beans.factory.annotation.Autowired;
7
8import java.util.List;
9
10/**
11 * Create on 2018/11/6 on Dong
12 */
13public class IBookServiceTest extends BaseTestCase {
14
15 @Autowired
16 private IBookService bookService;
17
18 private Book book;
19
20 @Override
21 public void before() {
22 super.before();
23 book = new Book();
24 }
25
26 @Test
27 public void testListBook() {
28 final List<Book> bookList = bookService.listBook(book);
29 for (Book b : bookList) {
30 System.out.println(b);
31 }
32 }
33
34}
35
9.controller返回视图freemarker
9.1 书写Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 1package com.go.dwinxin.controller;
2
3import com.go.dwinxin.model.Book;
4import com.go.dwinxin.service.IBookService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.stereotype.Controller;
7import org.springframework.ui.Model;
8import org.springframework.web.bind.annotation.RequestMapping;
9
10import java.util.List;
11
12/**
13 * Create on 2018/11/6 on Dong
14 */
15@Controller
16public class BookController {
17
18 @Autowired
19 private IBookService bookService;
20
21 @RequestMapping(value = {"/"})
22 public String toIndex(Model model, Book book) {
23 final List<Book> bookList = bookService.listBook(book);
24 model.addAttribute("bookList", bookList);
25 return "index";
26 }
27}
28
29
9.2 返回freemarker视图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <title>SpringBoot集成Mybatis</title>
6</head>
7<body>
8
9<#--使用freemarker表达式 显示数据-->
10<#list bookList as book>
11 ${book}<br>
12</#list>
13
14</body>
15</html>
16