1. 创建数据库表 1 2 3 4 5 6 CREATE TABLE tbl_employee ( id int (11 ) PRIMARY KEY AUTO_INCREMENT, last_name varchar (255 ) , gender char (1 ) , email varchar (255 ) );
2. 导入Jar包 主要包括如下几部分的 Jar 包:
Spring 核心 Jar 包 SpringMVC 所需要的额外 Jar 包 Spring 切面编程的 Jar 包 Spring JDBC 开发相关的 Jar 包,包括第三方C3P0数据源相关 Jar 包 MyBatis 支持 Jar 包和 MyBatis 与 Spring 整合的 Jar 包 日志包以及 MySQL 数据库驱动 JSTL 所需 Jar 包3. 编写 POJO 类 Employee 按照数据库字段,编写一个 POJO 类 Employee,具体代码如下所示:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.shoto.ssm.bean;public class Employee { private Integer id; private String lastName; private String gender; private String email; public Employee () { super (); } public Employee (Integer id, String lastName, String gender, String email) { super (); this .id = id; this .lastName = lastName; this .gender = gender; this .email = email; } }
4. 配置web.xml文件 在web.xml文件中,我们需要配置Spring相关的ContextLoaderListener以及与SpringMVC相关的DispatcherServlet等信息。具体配置如下:
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 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id ="WebApp_ID" version ="3.0" > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:applicationContext.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <servlet > <servlet-name > springDispatcherServlet</servlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class > <init-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:applicationContext-mvc.xml</param-value > </init-param > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > springDispatcherServlet</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > </web-app >
5. 创建和配置Spring和SpringMVC配置文件 创建src的同级目录config,用于存放一些配置文件,如Spring的配置文件applicationContext.xml和SpringMVC配置文件applicationContext-mvc.xml。如下所示:
5.1 applicationContext-mvc.xml配置 applicationContext-mvc.xml的内容如下所示,主要配置注解扫描 、视图解析器 、静态资源处理 以及 <mvc:annotation-driven> 。
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 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:context ="http://www.springframework.org/schema/context" xmlns:mvc ="http://www.springframework.org/schema/mvc" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd" > <context:component-scan base-package ="com.shoto.ssm" use-default-filters ="false" > <context:include-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> <context:include-filter type ="annotation" expression ="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan > <bean id ="resourceViewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="/WEB-INF/pages/" /> <property name ="suffix" value =".jsp" /> </bean > <mvc:default-servlet-handler /> <mvc:annotation-driven > </mvc:annotation-driven > </beans >
5.2 applicationContext.xml配置 applicationContext.xml的配置主要包括如下几个步骤:
配置注解扫描
1 2 3 4 5 6 7 <context:component-scan base-package ="com.shoto.ssm" > <context:exclude-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> <context:exclude-filter type ="annotation" expression ="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan >
config目录下创建数据库配置文件dbconfig.properties
1 2 3 4 5 6 7 8 9 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true jdbc.username=root jdbc.password=abc123! orcl.driver=oracle.jdbc.OracleDriver orcl.url=jdbc:oracle:thin:@localhost:1521:orcl orcl.username=scott orcl.password=tiger
配置C3P0连接池和事务管理器 我们之前在MyBatis的配置文件mybatis-config.xml中配置的默认连接池 和事务管理器 全部交由Spring来管理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <context:property-placeholder location ="classpath:dbconfig.properties" /> <bean id ="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name ="driverClass" value ="${jdbc.driver}" /> <property name ="jdbcUrl" value ="${jdbc.url}" /> <property name ="user" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> </bean > <bean id ="dataSourceTransactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" /> </bean >
配置声明式事务管理
1 2 <tx:annotation-driven transaction-manager ="dataSourceTransactionManager" />
Spring与MyBatis的整合配置 之前我们使用MyBatis时都是使用SqlSessionFactoryBuilder类读取MyBatis的配置文件mybatis-config.xml来创建SqlSessionFactory 类,然后使用其开启和获取会话session以此获取映射接口进行数据库操作。
现在我们可以让Spring来帮我们做这些事情,我们只需要进行如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <bean id ="sqlSessionFactoryBean" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" /> <property name ="configLocation" value ="classpath:mybatis-config.xml" > </property > <property name ="mapperLocations" value ="classpath:mybatis/mapper/*.xml" > </property > </bean > <mybatis-spring:scan base-package ="com.shoto.ssm.dao" />
6. 创建和配置MyBatis配置文件 我们在config目录下创建MyBatis的配置文件mybatis-config.xml。具体内容如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration > <settings > <setting name ="mapUnderscoreToCamelCase" value ="true" /> </settings > </configuration >
7. 编写映射接口(Dao层) 1 2 3 4 5 6 7 8 9 package com.shoto.ssm.dao;public interface EmployeeMapper { public List<Employee> getEmps () ; }
8. 编写映射文件EmployeeMapper.xml 在config目录下创建包mybatis.mapper ,在该包下创建映射文件EmployeeMapper.xml。
1 2 3 4 5 6 7 8 9 10 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace ="com.shoto.ssm.dao.EmployeeMapper" > <select id ="getEmps" resultType ="com.shoto.ssm.bean.Employee" > select * from tbl_employee </select > </mapper >
9. 编写EmployeeService(Service层) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.shoto.ssm.services;@Service public class EmployeeService { @Autowired private EmployeeMapper employeeMapper; public List<Employee> getAllEmps () { return employeeMapper.getEmps(); } }
10. 编写控制器(Web层) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.shoto.ssm.controller;@Controller public class EmployeeController { @Autowired EmployeeService employeeService; @RequestMapping ("/getAllEmps" ) public String getAllEmps (Map<String,Object> map) { List<Employee> empList = employeeService.getAllEmps(); map.put("empList" , empList); return "list" ; } }
11. 编写员工列表页面 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 <%@ page language ="java" contentType ="text/html; charset=UTF-8" pageEncoding ="UTF-8" %> <%@ taglib prefix ="c" uri ="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html > <head > <meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > <title > 员工列表</title > </head > <body > <table border ="1" cellpadding ="10" cellspacing ="0" > <tr > <th > ID</th > <th > LastName</th > <th > Gender</th > <th > Email</th > </tr > <c:forEach items ="${empList }" var ="emp" > <tr > <td > ${emp.id }</td > <td > ${emp.lastName }</td > <td > ${emp.gender }</td > <td > ${emp.email }</td > </tr > </c:forEach > </table > </body > </html >
12. 请求响应测试 最后进行请求响应测试,查询数据库后员工列表页面显示如下: