创建环境
系统:macOS
Java:1.8
软件:eclipse,maven,mysql
创建步骤
本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列表(页面发起请求-访问web项目-查询数据库)
创建数据库
1、创建数据库和表,如下:
创建sql如下:
1 CREATE DATABASEtest_mybatis;2 USEtest_mybatis;3
4 ------------------------------
5 --Table structure for employee
6 ------------------------------
7 DROP TABLE IF EXISTS`employee`;8 CREATE TABLE`employee` (9 `id` int(11) NOT NULLAUTO_INCREMENT,10 `last_name` varchar(255) DEFAULT NULL,11 `gender` char(1) DEFAULT NULL,12 `email` varchar(255) DEFAULT NULL,13 `dept_id` int(11) DEFAULT NULL COMMENT '部门ID',14 PRIMARY KEY(`id`)15 ) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8;16
17 ------------------------------
18 --Records of employee
19 ------------------------------
20 BEGIN;21 INSERT INTO `employee` VALUES (1, '大白', '1', 'dabai@163.com', 1);22 INSERT INTO `employee` VALUES (2, '小明', '1', 'xiaoming@163.com', 1);23 INSERT INTO `employee` VALUES (3, '小红', '1', 'xiaohong@163.com', 1);24 COMMIT;
sql文件
创建web项目
2、编辑pom文件,添加SpringMVC+Spring+Mybatis的相关依赖
1
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4 4.0.0
5 com.hd
6 test-spring-mybatis
7 war
8 1.0.0-SNAPSHOT
9 http://maven.apache.org
10
11
12
13
14 5.1.4.RELEASE
15
16
17 3.5.0
18
19 2.0.0
20
21
22 8.0.13
23
24
25 0.9.5.4
26
27
28 1.7.5
29 0.9.30
30
31
32 3.0.1
33 2.2
34
35
36 1.2
37 1.1.2
38
39
40 3.8.1
41
42
43 1.8
44 2.3.2
45
46
47
48
49
50
51
52 org.springframework
53 spring-core
54 ${spring.version}
55
56
57
58 org.springframework
59 spring-beans
60 ${spring.version}
61
62
63
64 org.springframework
65 spring-context
66 ${spring.version}
67
68
69
70 org.springframework
71 spring-expression
72 ${spring.version}
73
74
75
76
77 org.springframework
78 spring-aop
79 ${spring.version}
80
81
82
83
84 org.springframework
85 spring-web
86 ${spring.version}
87
88
89
90 org.springframework
91 spring-webmvc
92 ${spring.version}
93
94
95
96
97 org.springframework
98 spring-tx
99 ${spring.version}
100
101
102
103
104 org.springframework
105 spring-orm
106 ${spring.version}
107
108
109
110
111 org.springframework
112 spring-jdbc
113 ${spring.version}
114
115
116
117
118 org.mybatis
119 mybatis
120 ${mybatis.version}
121
122
123
124
125 org.mybatis
126 mybatis-spring
127 ${mybatis-spring.version}
128
129
130
131
132 mysql
133 mysql-connector-java
134 ${mysql.version}
135
136
137
138
139
140 com.mchange
141 c3p0
142 ${c3p0.version}
143
144
145
146
147
148 org.slf4j
149 slf4j-api
150 ${slf4j-api.version}
151 jar
152 compile
153
154
155
156 ch.qos.logback
157 logback-core
158 ${logback.version}
159 jar
160
161
162
163 ch.qos.logback
164 logback-classic
165 ${logback.version}
166 jar
167
168
169
170 ch.qos.logback
171 logback-access
172 ${logback.version}
173
174
175
176
177
178 javax.servlet
179 javax.servlet-api
180 ${servlet.version}
181 provided
182
183
184 javax.servlet.jsp
185 jsp-api
186 ${jsp-api.version}
187 provided
188
189
190
191
192 javax.servlet
193 jstl
194 ${jstl.version}
195
196
197
198 taglibs
199 standard
200 ${standard.version}
201
202
203
204
205 junit
206 junit
207 ${junit.version}
208 test
209
210
211
212
213
214
215
216
217 org.apache.maven.plugins
218 maven-compiler-plugin
219 ${maven.compiler.plugin.version}
220
221 ${jdk.version}
222 ${jdk.version}
223
224
225
226 test_spring_mybatis
227
228
pom.xml
3、编辑web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
5
6 test_spring_mybatis
7
8
9
10 contextConfigLocation
11 classpath:spring-context.xml
12
13
14
15
16
17 org.springframework.web.context.ContextLoaderListener
18
19
20
21
23
24 spring
25 org.springframework.web.servlet.DispatcherServlet
26
27
28 contextConfigLocation
29 classpath:spring-mvc.xml
30
31 1
32
33
34
35
36 spring
37 /
38
39
40
41
42 CharacterEncodingFilter
43 org.springframework.web.filter.CharacterEncodingFilter
44
45 encoding
46 UTF-8
47
48
49 forceEncoding
50 true
51
52
53
54 CharacterEncodingFilter
55 /*
56
57
58
4、在src/main/resources,新建一个spring-mvc.xml的配置文件,进行Spring-MVC相关配置
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
5、在src/main/resources,新建一个spring-context.xml的配置文件,进行Spring相关配置
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd8 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
11
12
13
14
15 expression="org.springframework.stereotype.Controller" />
16
17
18
19
20
6、在src/main/resources,新建一个spring-mybatis.xml的配置文件,进行Spring与Mybatis整合相关配置
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans8 http://www.springframework.org/schema/beans/spring-beans.xsd9 http://mybatis.org/schema/mybatis-spring10 http://mybatis.org/schema/mybatis-spring.xsd11 http://www.springframework.org/schema/tx12 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd13 http://www.springframework.org/schema/context14 http://www.springframework.org/schema/context/spring-context-4.0.xsd">
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
58
59
60
7、在src/main/resources,新建一个mybatis-config.xml的配置文件,相当于mybatis的全局配置文件,进行Mybatis相关配置
1 <?xml version="1.0" encoding="UTF-8"?>
2 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5
6
12
13
14
15
16
18
19
20
21
22
23
8、在src/main/resources,新建一个dbconfig.properties的配置文件,配置数据库连接的相关信息
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/test_mybatis?allowPublicKeyRetrieval=true
jdbc.username = admin
jdbc.password = 123456
代码编写
1、在src/main/java的com.test.mybatis.controller,新建一个类TestController.java
1 packagecom.test.mybatis.controller;2
3 importjava.util.List;4 importjava.util.Map;5
6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Controller;8 importorg.springframework.web.bind.annotation.RequestMapping;9
10 importcom.test.mybatis.pojo.Employee;11 importcom.test.mybatis.service.EmployeeService;12
13 @Controller14 public classTestController {15
16
17 @Autowired18 EmployeeService employeeService;19
20 @RequestMapping("/getEmps")21 public String emps(Mapmap){22 List emps =employeeService.getEmps();23 System.out.println(emps.size());24 map.put("allEmps", emps);25 return "list";26 }27
28 }
2、在src/main/java的com.test.mybatis.service,新建一个类EmployeeService.java
1 packagecom.test.mybatis.service;2
3 importjava.util.List;4
5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.stereotype.Service;7 importorg.springframework.transaction.annotation.Transactional;8
9 importcom.test.mybatis.dao.EmployeeMapper;10 importcom.test.mybatis.pojo.Employee;11
12 @Service13 @Transactional14 public classEmployeeService {15
16 @Autowired17 privateEmployeeMapper employeeMapper;18
19 public ListgetEmps() {20 returnemployeeMapper.getEmployee();21 }22
23 }
3、在src/main/java的com.test.mybatis.dao,新建一个类EmployeeMapper.java
packagecom.test.mybatis.dao;importjava.util.List;importjava.util.Map;importorg.apache.ibatis.annotations.Param;importorg.apache.ibatis.annotations.Select;importorg.springframework.stereotype.Repository;importcom.test.mybatis.pojo.Employee;
@Repositorypublic interfaceEmployeeMapper {public ListgetEmployee();
}
4、在src/main/java的com.test.mybatis.pojo,新建一个类Employee.java
1 packagecom.test.mybatis.pojo;2
3 importjava.io.Serializable;4
5
6 public class Employee implementsSerializable{7
8 privateInteger id;9 privateString lastName;10 privateString gender;11 privateString email;12
13
14 publicEmployee() {15 //TODO Auto-generated constructor stub
16 }17
18
19 publicEmployee(String lastName, String gender, String email) {20 super();21 this.lastName =lastName;22 this.gender =gender;23 this.email =email;24 }25
26
27
28 publicEmployee(Integer id, String lastName, String gender, String email) {29 super();30 this.id =id;31 this.lastName =lastName;32 this.gender =gender;33 this.email =email;34 }35
36
37 publicInteger getId() {38 returnid;39 }40 public voidsetId(Integer id) {41 this.id =id;42 }43 publicString getLastName() {44 returnlastName;45 }46 public voidsetLastName(String lastName) {47 this.lastName =lastName;48 }49 publicString getGender() {50 returngender;51 }52 public voidsetGender(String gender) {53 this.gender =gender;54 }55 publicString getEmail() {56 returnemail;57 }58 public voidsetEmail(String email) {59 this.email =email;60 }61 @Override62 publicString toString() {63 return "Employee [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]";64 }65
66
67 }
Employee.java
5、在src/main/resources/mybatis/mapper,新增EmployeeMapper.xml文件
1 <?xml version="1.0" encoding="UTF-8"?>
2
3
4
5
8
9 resultType="com.test.mybatis.pojo.Employee">
10 select id, last_name lastName, gender, email from employee11
12
13
6、编辑首页index.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
Hello World!
7 获取员工列表
8
9
7、在webapp/WEB-INF/pages,中新增用户列表页list.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
Insert title here9
10
11 员工列表12
13
14
id15
lastName16
gender17
email18
19
20
21
${emp.id }22
${emp.lastName }23
${emp.gender }24
${emp.email}25
26
27
28
29
测试
项目最终的目录结构
1、启动web项目
2、在浏览器中访问,地址:localhost:8080/test_spring_mybatis,点击员工列表