前言
服务器开发分为三层:表现层 + 业务层 + 持久层
整合的桥梁或者说是方式,我们使用:XML(配置文件)+ 注解的方式
整合的整体思路:
- 搭建整合的环境(引入依赖,创建包,创建实体类)
- 先搭建好 Spring
- Spring 整合 SpringMVC
- Spring 整合 MyBatis
包结构:
搭建整合环境
数据库部分
1 | create database ssm; |
依赖
1 | <properties> |
实体类
1 | public class Account implements Serializable{ |
Dao 层
1 | /** |
Serivice 层
接口
1 | public interface AccountService { |
实现类
1 | public class AccountServiceImpl implements AccountService{ |
Spring 搭建
- 在 Spring 层,我们要进行业务的处理
创建好配置文件
完成配置文件
开启注解扫描
1
2
3
4
5<!--开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理-->
<context:component-scan base-package="cn.itcast" >
<!--配置哪些注解不扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
在 Serivice 层的类上加注解
1
编写测试
1
2
3
4
5
6
7
8
9
10
11
12public class TestSpring {
public void run1(){
// 加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 获取对象
AccountService as = (AccountService) ac.getBean("accountService");
// 调用方法
as.findAll();
}
}
Spring 整合 SpringMVC
搭建测试 SpringMVC 的开发环境
web.xml 中配置
DispatcherServlet
前端控制器1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器,创建该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>配置解决中文乱码的过滤器
1
2
3
4
5
6
7
8
9
10
11
12
13<!--解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>新建 springmvc.xml
添加约束
1
2
3
4
5
6
7
8
9
10
11
12
13
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>开启注解扫描
1
2
3
4<!--开启注解扫描,只扫描Controller注解 与Spring的注解反过来-->
<context:component-scan base-package="cn.itcast">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>配置视图解析器
1
2
3
4
5<!--配置的视图解析器对象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>过滤静态资源
1
2
3
4<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />开启注解
1
2<!--开启SpringMVC注解的支持-->
<mvc:annotation-driven/>
- 测试:
控制层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/**
* 帐户web
*/
public class AccountController {
public String findAll(Model model){
System.out.println("表现层:查询所有账户...");
// 调用service的方法
return "list";
}
}index.jsp
1
2
3
4
5
6
7
8
9<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="account/findAll">测试查询</a>
</body>
</html>list.jsp
1
2
3
4
5
6
7
8
9
10
11<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>查询所有的帐户</h3>
</body>
</html>结果
实现整合
问题:
我们知道,SpringMVC 现在的配置是只扫描 Controller 注解,而别的注解不扫,这就导致一个问题,我们的 Spring 的配置文件:applicationContext.xml
是没有被加载的,因此里面的扫描也不会生效,那我们的 service 层就是个摆设,根本没有被加载到 IOC 容器中,就没办法注入了。
解决:
启动容器的时候就得把Spring的配置文件给加载了才行;
下面我们就要在 web.xml 中做以下两件事:
配置 Spring 的监听器
1
2
3
4<!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>设置配置文件的路径
1
2
3
4
5<!--设置配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
此时我们的 Controller 层就可以做到 Service 的注入了
1 |
|
Spring 整合 MyBatis
搭建测试 MyBatis 环境
ps:配置文件找不到的处理方法
1 | <resources> |
dao 接口上加注解,进行映射
1
2
3
4
5
6
7
8
9
10
11
12/**
* 帐户dao接口
*/
public interface AccountDao {
// 查询所有账户
public List<Account> findAll();
}编写 sql 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!--configuration核心配置文件-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="cn.itcast.dao"/>
</mappers>
</configuration>进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 测试查询
* @throws Exception
*/
public void run1() throws Exception {
// 加载配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 创建SqlSession对象
SqlSession session = factory.openSession();
// 获取到代理对象
AccountDao dao = session.getMapper(AccountDao.class);
// 查询所有数据
List<Account> list = dao.findAll();
for(Account account : list){
System.out.println(account);
}
// 关闭资源
session.close();
in.close();
}
Spring 整合 Mybatis 实现
- 思路:通过让 Spring 框架接管 Mybatis 中的SqlSessionFactory 工厂的创建,再通过读取mapper配置内容去创建dao 的代理对象实现类,将它们(SqlSessionFactory 工厂和代理对象)存入IOC 容器
Mybatis 交由 Spring 托管
分为三步:配置连接池、配置 SqlSessionFactory 工厂、配置接口所在的包
配置连接池
1
2
3
4
5
6
7<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>配置 SqlSessionFactory 工厂
1
2
3
4<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>配置接口所在包
1
2
3
4<!--配置AccountDao接口所在包-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.dao"/>
</bean>
以上三步的作用:以后我们的 SqlSessionFactory 就将会由 IOC 容器帮助我们创建,接着就可以创建 SqlSession,从而得到我们的 mapper 代理对象,并且这些对象全都在 IOC 容器中,意味着什么呢?就可以实现我们的依赖注入了
接下来就来测试一下:
在 AccountDao 接口上添加@Repository注解
1
2
3
4
public interface AccountDao {
}Serivice 的实现类中,依赖注入我们的 Dao 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public List<Account> findAll() {
System.out.println("业务层:查询所有账户...");
return accountDao.findAll();
}
public void saveAccount(Account account) {
System.out.println("业务层:保存帐户...");
accountDao.saveAccount(account);
}
}编写 Controller 层 增加一个视图封装数据部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class AccountController {
private AccountService accountService;
public String findAll(Model model){
System.out.println("表现层:查询所有账户...");
// 调用service的方法
List<Account> list = accountService.findAll();
model.addAttribute("list",list);
return "list";
}
}前端页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>查询所有的帐户</h3>
<c:forEach items="${list}" var="account">
${account.name}
</c:forEach>
</body>
</html>测试(这是我已经把 sq 配置文件删除后的,也就代表着 mybatis 的 xml 配置部分已经完全交由 Spring 托管了)
引入事务
然而框架尚未完全整合完:我们的整合还差最后一步——那就是引入事务的控制
配置 Spring 框架声明式事务管理分为三步:配置事务管理器、配置事务通知、配置 AOP 增强
配置事务管理器
1
2
3
4<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>配置事务通知
1
2
3
4
5
6
7<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>配置 AOP 增强(切入点选择的是 Service 层)
1
2
3
4<!--配置AOP增强-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.service.impl.*ServiceImpl.*(..))"/>
</aop:config>
测试一下:
前端页面加入保存方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="account/findAll">测试查询</a>
<h3>测试包</h3>
<form action="account/save" method="post">
姓名:<input type="text" name="name" /><br />
金额:<input type="text" name="money" /><br />
<input type="submit" value="保存" /><br />
</form>
</body>
</html>Controller 层加入 save 方法
1
2
3
4
5
6
7
8
9
10/**
* 保存
* @return
*/
public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/account/findAll");
return;
}测试:我们加入一个 姓名 Test 金额 100 的用户
至此,我们成功整合了 SSM 框架啦!