【Mybatis】MyBatis之整合Spring(八)

时间:2021-11-25 20:13:11

 

创建环境

  • 系统:macOS
  • Java:1.8 
  • 软件:eclipse,maven,mysql

创建步骤

  本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列表(页面发起请求-访问web项目-查询数据库)

  创建数据库

  1、创建数据库和表,如下:

    【Mybatis】MyBatis之整合Spring(八)

    创建sql如下:

【Mybatis】MyBatis之整合Spring(八)【Mybatis】MyBatis之整合Spring(八)
 1 CREATE DATABASE test_mybatis;
 2 USE test_mybatis;
 3 
 4 -- ----------------------------
 5 -- Table structure for employee
 6 -- ----------------------------
 7 DROP TABLE IF EXISTS `employee`;
 8 CREATE TABLE `employee` (
 9   `id` int(11) NOT NULL AUTO_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项目

    1、创建一个Maven Web项目,可以参照:【Maven】Eclipse 使用Maven创建Java Web项目

      【Mybatis】MyBatis之整合Spring(八)

    2、编辑pom文件,添加SpringMVC+Spring+Mybatis的相关依赖

【Mybatis】MyBatis之整合Spring(八)【Mybatis】MyBatis之整合Spring(八)
  1 <project xmlns="http://maven.apache.org/POM/4.0.0"
  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     <modelVersion>4.0.0</modelVersion>
  5     <groupId>com.hd</groupId>
  6     <artifactId>test-spring-mybatis</artifactId>
  7     <packaging>war</packaging>
  8     <version>1.0.0-SNAPSHOT</version>
  9     <url>http://maven.apache.org</url>
 10 
 11     <!-- 定义maven变量 -->
 12     <properties>
 13         <!-- spring -->
 14         <spring.version>5.1.4.RELEASE</spring.version>
 15 
 16         <!-- Mybatis -->
 17         <mybatis.version>3.5.0</mybatis.version>
 18         <!-- Mybatis 整合 Spring -->
 19         <mybatis-spring.version>2.0.0</mybatis-spring.version>
 20 
 21         <!-- mysql -->
 22         <mysql.version>8.0.13</mysql.version>
 23         
 24         <!-- c3p0 连接池 -->
 25         <c3p0.version>0.9.5.4</c3p0.version>
 26         
 27         <!-- logback -->
 28         <slf4j-api.version>1.7.5</slf4j-api.version>
 29         <logback.version>0.9.30</logback.version>
 30 
 31         <!-- Servlet -->
 32         <servlet.version>3.0.1</servlet.version>
 33         <jsp-api.version>2.2</jsp-api.version>
 34 
 35         <!-- jstl -->
 36         <jstl.version>1.2</jstl.version>
 37         <standard.version>1.1.2</standard.version>
 38 
 39         <!-- test junit -->
 40         <junit.version>3.8.1</junit.version>
 41 
 42         <!-- jdk -->
 43         <jdk.version>1.8</jdk.version>
 44         <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
 45     </properties>
 46 
 47 
 48     <dependencies>
 49 
 50         <!-- Spring IOC 核心容器 -->
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-core</artifactId>
 54             <version>${spring.version}</version>
 55         </dependency>
 56 
 57         <dependency>
 58             <groupId>org.springframework</groupId>
 59             <artifactId>spring-beans</artifactId>
 60             <version>${spring.version}</version>
 61         </dependency>
 62 
 63         <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-context</artifactId>
 66             <version>${spring.version}</version>
 67         </dependency>
 68 
 69         <dependency>
 70             <groupId>org.springframework</groupId>
 71             <artifactId>spring-expression</artifactId>
 72             <version>${spring.version}</version>
 73         </dependency>
 74 
 75         <!-- Spring AOP 切面 模块 -->
 76         <dependency>
 77             <groupId>org.springframework</groupId>
 78             <artifactId>spring-aop</artifactId>
 79             <version>${spring.version}</version>
 80         </dependency>
 81 
 82         <!-- Spring WEB MVC 模块 -->
 83         <dependency>
 84             <groupId>org.springframework</groupId>
 85             <artifactId>spring-web</artifactId>
 86             <version>${spring.version}</version>
 87         </dependency>
 88 
 89         <dependency>
 90             <groupId>org.springframework</groupId>
 91             <artifactId>spring-webmvc</artifactId>
 92             <version>${spring.version}</version>
 93         </dependency>
 94 
 95         <!-- Spring 事物 模块 -->
 96         <dependency>
 97             <groupId>org.springframework</groupId>
 98             <artifactId>spring-tx</artifactId>
 99             <version>${spring.version}</version>
100         </dependency>
101 
102         <!-- Spring ORM 对象关系映射 模块 -->
103         <dependency>
104             <groupId>org.springframework</groupId>
105             <artifactId>spring-orm</artifactId>
106             <version>${spring.version}</version>
107         </dependency>
108 
109         <!-- Spring JDBC 模块 -->
110         <dependency>
111             <groupId>org.springframework</groupId>
112             <artifactId>spring-jdbc</artifactId>
113             <version>${spring.version}</version>
114         </dependency>
115 
116         <!-- Mybatis -->
117         <dependency>
118             <groupId>org.mybatis</groupId>
119             <artifactId>mybatis</artifactId>
120             <version>${mybatis.version}</version>
121         </dependency>
122 
123         <!-- Mybatis 整合 Spring -->
124         <dependency>
125             <groupId>org.mybatis</groupId>
126             <artifactId>mybatis-spring</artifactId>
127             <version>${mybatis-spring.version}</version>
128         </dependency>
129 
130         <!-- mysql -->
131         <dependency>
132             <groupId>mysql</groupId>
133             <artifactId>mysql-connector-java</artifactId>
134             <version>${mysql.version}</version>
135         </dependency>
136 
137         <!-- c3p0 连接池 -->
138         <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
139         <dependency>
140             <groupId>com.mchange</groupId>
141             <artifactId>c3p0</artifactId>
142             <version>${c3p0.version}</version>
143         </dependency>
144         
145         
146         <!-- logback -->
147         <dependency>
148             <groupId>org.slf4j</groupId>
149             <artifactId>slf4j-api</artifactId>
150             <version>${slf4j-api.version}</version>
151             <type>jar</type>
152             <scope>compile</scope>
153         </dependency>
154 
155         <dependency>
156             <groupId>ch.qos.logback</groupId>
157             <artifactId>logback-core</artifactId>
158             <version>${logback.version}</version>
159             <type>jar</type>
160         </dependency>
161 
162         <dependency>
163             <groupId>ch.qos.logback</groupId>
164             <artifactId>logback-classic</artifactId>
165             <version>${logback.version}</version>
166             <type>jar</type>
167         </dependency>
168 
169         <dependency>
170             <groupId>ch.qos.logback</groupId>
171             <artifactId>logback-access</artifactId>
172             <version>${logback.version}</version>
173         </dependency>
174 
175 
176         <!-- Servlet -->
177         <dependency>
178             <groupId>javax.servlet</groupId>
179             <artifactId>javax.servlet-api</artifactId>
180             <version>${servlet.version}</version>
181             <scope>provided</scope>
182         </dependency>
183         <dependency>
184             <groupId>javax.servlet.jsp</groupId>
185             <artifactId>jsp-api</artifactId>
186             <version>${jsp-api.version}</version>
187             <scope>provided</scope>
188         </dependency>
189 
190         <!-- jstl -->
191         <dependency>
192             <groupId>javax.servlet</groupId>
193             <artifactId>jstl</artifactId>
194             <version>${jstl.version}</version>
195         </dependency>
196 
197         <dependency>
198             <groupId>taglibs</groupId>
199             <artifactId>standard</artifactId>
200             <version>${standard.version}</version>
201         </dependency>
202 
203         <!-- test -->
204         <dependency>
205             <groupId>junit</groupId>
206             <artifactId>junit</artifactId>
207             <version>${junit.version}</version>
208             <scope>test</scope>
209         </dependency>
210 
211     </dependencies>
212 
213     <build>
214         <plugins>
215             <!-- define the project compile level -->
216             <plugin>
217                 <groupId>org.apache.maven.plugins</groupId>
218                 <artifactId>maven-compiler-plugin</artifactId>
219                 <version>${maven.compiler.plugin.version}</version>
220                 <configuration>
221                     <source>${jdk.version}</source>
222                     <target>${jdk.version}</target>
223                 </configuration>
224             </plugin>
225         </plugins>
226         <finalName>test_spring_mybatis</finalName>
227     </build>
228 </project>
pom.xml

 

    3、编辑web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 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     <display-name>test_spring_mybatis</display-name>
 7 
 8     <!--Spring配置: needed for ContextLoaderListener -->
 9     <context-param>
10         <param-name>contextConfigLocation</param-name>
11         <param-value>classpath:spring-context.xml</param-value>
12     </context-param>
13 
14     <!-- 指定Spring 的配置文件所在目录。默认配置在WEB-INF目录下 -->
15     <!-- Bootstraps the root web application context before servlet initialization -->
16     <listener>
17         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
18     </listener>
19 
20     <!-- Spring MVC配置 -->
21     <!-- The front controller of this Spring Web application, responsible for 
22         handling all application requests -->
23     <servlet>
24         <servlet-name>spring</servlet-name>
25         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
26         <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
27         <init-param>
28             <param-name>contextConfigLocation</param-name>
29             <param-value>classpath:spring-mvc.xml</param-value>
30         </init-param>
31         <load-on-startup>1</load-on-startup>
32     </servlet>
33 
34     <!-- Map all requests to the DispatcherServlet for handling -->
35     <servlet-mapping>
36         <servlet-name>spring</servlet-name>
37         <url-pattern>/</url-pattern>
38     </servlet-mapping>
39     
40     <!-- 中文过滤器 -->
41     <filter>
42         <filter-name>CharacterEncodingFilter</filter-name>
43         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
44         <init-param>
45             <param-name>encoding</param-name>
46             <param-value>UTF-8</param-value>
47         </init-param>
48         <init-param>
49             <param-name>forceEncoding</param-name>
50             <param-value>true</param-value>
51         </init-param>
52     </filter>
53     <filter-mapping>
54         <filter-name>CharacterEncodingFilter</filter-name>
55         <url-pattern>/*</url-pattern>
56     </filter-mapping>
57     
58 </web-app>

    4、在src/main/resources,新建一个spring-mvc.xml的配置文件,进行Spring-MVC相关配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 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.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9 
10     <!--SpringMVC只是控制网站跳转逻辑  -->
11     <!-- 只扫描控制器 -->
12     <context:component-scan base-package="com.test.mybatis" use-default-filters="false">
13         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
14     </context:component-scan>
15     
16     <!-- 视图解析器 -->
17     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18         <property name="prefix" value="/WEB-INF/pages/"></property>
19         <property name="suffix" value=".jsp"></property>
20     </bean>
21     
22     <!-- 默认的注解映射的支持 -->
23     <mvc:annotation-driven></mvc:annotation-driven>
24     
25     <!-- 对静态资源文件的访问 -->
26     <mvc:default-servlet-handler/>
27     
28 </beans>

    5、在src/main/resources,新建一个spring-content.xml的配置文件,进行Spring相关配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 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.xsd
 8         http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
 9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
11 
12     <!-- Sprin望管理所有的业务逻辑组件等 -->
13     <context:component-scan base-package="com.test.mybatis">
14         <context:exclude-filter type="annotation"
15             expression="org.springframework.stereotype.Controller" />
16     </context:component-scan>
17 
18     <!-- 导入spring整合mybatis配置文件 -->
19     <import resource="classpath:spring-mybatis.xml" />    
20 </beans>

    6、在src/main/resources,新建一个spring-mybatis.xml的配置文件,进行Spring与Mybatis整合相关配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 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 
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9           http://mybatis.org/schema/mybatis-spring 
10           http://mybatis.org/schema/mybatis-spring.xsd
11         http://www.springframework.org/schema/tx 
12         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
13         http://www.springframework.org/schema/context 
14         http://www.springframework.org/schema/context/spring-context-4.0.xsd">
15 
16     <!-- 引入数据库的配置文件 -->
17     <context:property-placeholder location="classpath:dbconfig.properties" />
18     
19     <!-- 数据源:Spring用来控制业务逻辑。数据源、事务控制、aop -->
20     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
21         <property name="jdbcUrl" value="${jdbc.url}"></property>
22         <property name="driverClass" value="${jdbc.driver}"></property>
23         <property name="user" value="${jdbc.username}"></property>
24         <property name="password" value="${jdbc.password}"></property>
25     </bean>
26     <!-- spring事务管理 -->
27     <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
28         <property name="dataSource" ref="dataSource"></property>
29     </bean>
30 
31     <!-- 开启基于注解的事务 -->
32     <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
33     
34     <!-- 
35     整合mybatis 
36         目的:1、spring管理所有组件。mapper的实现类。
37                 service==>Dao   @Autowired:自动注入mapper;
38             2、spring用来管理事务,spring声明式事务
39     -->
40     <!--创建出SqlSessionFactory对象  -->
41     <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
42         <property name="dataSource" ref="dataSource"></property>
43         <!-- configLocation指定全局配置文件的位置 -->
44         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
45         <!--mapperLocations: 指定mapper文件的位置-->
46         <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
47     </bean>
48     
49     <!--配置一个可以进行批量执行的sqlSession  -->
50     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
51         <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
52         <constructor-arg name="executorType" value="BATCH"></constructor-arg>
53     </bean>
54     
55     <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;
56     base-package:指定mapper接口的包名
57      -->
58     <mybatis-spring:scan base-package="com.test.mybatis.dao"/>
59     
60 </beans>

    7、在src/main/resources,新建一个mybatis-config.xml的配置文件,相当于mybatis的全局配置文件,进行Mybatis相关配置

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4  "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- 
 7         2、settings包含含多重要的设置项
 8             setting:用来设置每一个设置项
 9                 name:设置项名字
10                 values:设置项取值
11      -->
12     <settings>
13         <setting name="mapUnderscoreToCamelCase" value="true"/>
14         
15         <!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->
16         <!-- <setting name="lazyloadingenabled" value="true"/>
17         <setting name="aggressivelazyloading" value="false"/> -->
18         
19         <!-- 设置二级缓存,默认是true -->
20         <setting name="cacheEnabled" value="true"/>
21     </settings>
22     
23 </configuration>

    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 package com.test.mybatis.controller;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 
10 import com.test.mybatis.pojo.Employee;
11 import com.test.mybatis.service.EmployeeService;
12 
13 @Controller
14 public class TestController {
15     
16 
17     @Autowired
18     EmployeeService employeeService;
19     
20     @RequestMapping("/getEmps")
21     public String emps(Map<String,Object> map){
22         List<Employee> 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 package com.test.mybatis.service;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 import org.springframework.transaction.annotation.Transactional;
 8 
 9 import com.test.mybatis.dao.EmployeeMapper;
10 import com.test.mybatis.pojo.Employee;
11 
12 @Service
13 @Transactional
14 public class EmployeeService {
15     
16     @Autowired
17     private EmployeeMapper employeeMapper;
18 
19     public List<Employee> getEmps() {
20         return employeeMapper.getEmployee();
21     }
22 
23 }

 

    3、在src/main/java的com.test.mybatis.dao,新建一个类EmployeeMapper.java

package com.test.mybatis.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.test.mybatis.pojo.Employee;

@Repository
public interface EmployeeMapper {
    
    public List<Employee> getEmployee();
    
}

    4、在src/main/java的com.test.mybatis.pojo,新建一个类Employee.java

【Mybatis】MyBatis之整合Spring(八)【Mybatis】MyBatis之整合Spring(八)
 1 package com.test.mybatis.pojo;
 2 
 3 import java.io.Serializable;
 4 
 5 
 6 public class Employee implements Serializable{
 7 
 8     private Integer id;
 9     private String lastName;
10     private String gender;
11     private String email;
12     
13     
14     public Employee() { 
15         // TODO Auto-generated constructor stub
16     }
17     
18     
19     public Employee(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     public Employee(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     public Integer getId() {
38         return id;
39     }
40     public void setId(Integer id) {
41         this.id = id;
42     }
43     public String getLastName() {
44         return lastName;
45     }
46     public void setLastName(String lastName) {
47         this.lastName = lastName;
48     }
49     public String getGender() {
50         return gender;
51     }
52     public void setGender(String gender) {
53         this.gender = gender;
54     }
55     public String getEmail() {
56         return email;
57     }
58     public void setEmail(String email) {
59         this.email = email;
60     }
61     @Override
62     public String 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 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <!-- namespace:名称空间 id:唯一标识 resultType:返回值类型 #{id}:从传过来的参数中取出id值 -->
 4 <mapper namespace="com.test.mybatis.dao.EmployeeMapper">
 5 <!--
 6 <cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024"></cache>
 7 -->
 8     <select id="getEmployee"
 9         resultType="com.test.mybatis.pojo.Employee">
10         select id, last_name lastName, gender, email from employee
11     </select>
12 
13 </mapper> 

    6、编辑首页index.jsp

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2     pageEncoding="UTF-8"%>
3 <html>
4 
5 <body>
6     <h2>Hello World!</h2>
7     <a href="getEmps">获取员工列表</a>
8 </body>
9 </html>

    7、在webapp/WEB-INF/pages,中新增用户列表页list.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     员工列表
12     <table>
13         <tr>
14             <th>id</th>
15             <th>lastName</th>
16             <th>gender</th>
17             <th>email</th>
18         </tr>
19         <c:forEach items="${allEmps }" var="emp">
20             <tr>
21                 <td>${emp.id }</td>
22                 <td>${emp.lastName }</td>
23                 <td>${emp.gender }</td>
24                 <td>${emp.email}</td>
25             </tr>
26         </c:forEach>
27     </table>
28 </body>
29 </html>

 

  测试

    项目最终的目录结构

      【Mybatis】MyBatis之整合Spring(八)

    1、启动web项目

    2、在浏览器中访问,地址:localhost:8080/test_spring_mybatis,点击员工列表

      【Mybatis】MyBatis之整合Spring(八)