1. 搭建整合环境
1. 整合说明:SSM整合可以使用多种方式,我选择XML + 注解的方式
2. 整合的思路
* 先把spring、springmvc、mybatis分别独立搭建好,没有问题了再用spring整合springmvc和mybatis
1. 先搭建整合的环境
2. 先把Spring的配置搭建完成
3. 再使用Spring整合SpringMVC框架
4. 最后使用Spring整合MyBatis框架
1.1 创建数据库和表结构
create database ssm;
use ssm;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
1.2 创建maven的工程
1.3 pom.xml文件中导入依赖的jar包和插件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>8.0.15</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
<spring.security.version>5.0.1.RELEASE</spring.security.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<build>
<!--配置插件-->
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 项目访问路径 -->
<path>/mybatis</path>
<!-- 访问项目的端口号 -->
<port>80</port>
</configuration>
</plugin>
</plugins>
</build>
1.4 编写实体类,编写domain目录下的实体类
package cn.wanghao.ssm.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
1.5 编写dao接口
package cn.wanghao.ssm.dao;
import cn.wanghao.ssm.domain.Account;
import java.util.List;
public interface AccountDao {
public void saveAccount(Account account);
public List<Account> findAll();
}
1.6 编写service接口和实现类
package cn.wanghao.ssm.service;
import cn.wanghao.ssm.domain.Account;
import java.util.List;
public interface AccountService {
public void saveAccount(Account account);
public List<Account> findAll();
}
package cn.wanghao.ssm.service.impl;
import cn.wanghao.ssm.domain.Account;
import cn.wanghao.ssm.service.AccountService;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Override
public void saveAccount(Account account) {
System.out.println("业务层:保存账户信息.");
}
@Override
public List<Account> findAll() {
System.out.println("业务层:查询所有账户信息.");
return null;
}
}
此时目录:
2. Spring框架代码的编写
2.1 在resources目录下创建applicationContext.xml的配置文件并配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架 去管理 -->
<context:component-scan base-package="cn.wanghao.ssm">
<!-- 配置要忽略的注解 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
注意:也可以使用下面具体的代码来代替上面的扫描:
<context:component-scan base-package=“cn.wanghao.ssm.dao”></context:component-scan>
<context:component-scan base-package=“cn.wanghao.ssm.service”></context:component-scan>
2.2 编写测试方法,进行测试
package cn.wanghao.ssm.test;
import cn.wanghao.ssm.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
@Test
public void run1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = ac.getBean("accountService", AccountService.class);
accountService.findAll();
}
}
运行结果如下:
上面报错是缺少log4j的相应文件产生的,对我们的程序影响不大。如果你看着不顺眼,想消除,可以加入下面的配置文件到resources
目录下:
链接:https://pan.baidu.com/s/1czwWFMu0sqa1tgpr8MvzOg | 提取码:bt49 |
---|
现在项目的目录如下:
3. Spring整合SpringMVC框架
3.1 搭建和测试SpringMVC的开发环境
3.1.1 在web.xml中配置DispatcherServlet、中文乱码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app.xsd"
version="3.1">
<!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码过滤器 -->
<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>
</web-app>
3.1.2 创建springmvc.xml的配置文件并编写
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="cn.wanghao.ssm">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"></context:include-filter>
</context:component-scan>
<!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JSP文件所在的目录 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 设置静态资源不过滤 -->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
</beans>
3.1.3 测试SpringMVC的框架搭建是否成功
- 删除项目中的index.jsp,然后新建一个index.jsp并编写
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="account/testSuccess.do">点击跳转到成功页面</a>
</body>
</html>
- 在
WEB-INF
目录下建立pages
目录,然后在pages
目录下建立success.jsp
页面并编写
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div>成功!</div>
</body>
</html>
- 在项目目录下建立一个
controller
目录,然后在controller
目录下建立AccountController
类并编写:
package cn.wanghao.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping("/testSuccess.do")
public String testSuccess(){
System.out.println("springMVC:跳转到成功页面...");
return "success";
}
}
启动服务器,进行测试:
此时的项目结构如下:
3.2 Spring整合SpringMVC的框架
3.2.1 编写web.xml文件
目的:在controller中能成功的调用service对象中的方法。
在项目启动的时候,就去加载applicationContext.xml
的配置文件,在web.xml
中配置 ContextLoaderListener
监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文 件)。
添加的代码如下:
<!-- 配置Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
3.2.2 测试整合是否成功
然后在控制器类中引用业务层对象:
package cn.wanghao.ssm.controller;
import cn.wanghao.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping("/testSuccess.do")
public String testSuccess(){
accountService.findAll();
System.out.println("springMVC:跳转到成功页面...");
return "success";
}
}
启动项目运行:
整合成功!
4. Spring整合MyBatis框架
按照正常的思维,是先独立测试Mybatis框架是否能独立运行,再整合到spring框架中。由于最终不需要SqlMapConfig.xml
配置文件,所以我习惯直接整合到spring中。
4.1 配置applicationContext.xml配置文件
目的:将Mybatis框架中创建的对象放入Spring 的IOC容器中。
在applicationContext.xml
中配置如下代码:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描,要扫描的是service层注解,要忽略web层注解,因为web层让SpringMVC框架 去管理 -->
<context:component-scan base-package="cn.wanghao.ssm.service"></context:component-scan>
<!--spring整合mybatis框架-->
<!--1. 引入属性文件,在配置中占位使用 -->
<context:property-placeholder location="classpath:druid.properties"/>
<!--2. 配置连接池对象: 使用Druid连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--配置连接数据库的4个基本信息-->
<!-- driverClassName属性可以不写,会根据url自动判断 -->
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}" />
<property name="username" value="root" />
<property name="password" value="xxxxxx" />
</bean>
<!-- 3. 创建会话工厂:SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 别名 -->
<property name="typeAliasesPackage" value="cn.wanghao.ssm.domain"></property>
<!-- sql映射文件路径。扫描mappers目录以及子目录下的所有xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml"/>
</bean>
<!-- 4. 配置扫描dao的包,由spring创建对象-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.wanghao.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事务 -->
<!-- 1. 配置一个事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入 DataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2. 事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--在 tx:advice 标签内部 配置事务的属性 -->
<tx:attributes>
<!-- 对所有的方法设置可读写 -->
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<!-- 优先级高。find方法中对数据库的操作只能读数据 -->
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置 aop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* cn.wanghao.ssm.service.impl.*.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
<!--支持注解驱动的事务管理,指定事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
druid.properties
:
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username=root
password=xxxxxx
# 初始化连接数
initialSize=5
# 连接池的最大数据库连接数。设为0表示无限制
maxActive=10
# 从连接池获取连接等待超时的毫秒数
maxWait=3000
4.2 编写mybatis的映射文件xxx.xml
这里我建立如下AccountDao.xml
文件:
<?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="cn.wanghao.ssm.dao.AccountDao"> <!-- namespace的值是需要配置的dao层下的接口类 -->
<!-- id是要简化的方法名; resultType返回值的类型类路径 -->
<select id="findAll" resultType="Account">
select * from account;
</select>
</mapper>
注意:不需要在dao层接口上加@Repository 注解
4.3 在service的继承类中使用AccountDao对象
package cn.wanghao.ssm.service.impl;
import cn.wanghao.ssm.dao.AccountDao;
import cn.wanghao.ssm.domain.Account;
import cn.wanghao.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
public void saveAccount(Account account) {
System.out.println("业务层:保存账户信息.");
}
@Override
public List<Account> findAll() {
System.out.println("业务层:查询所有账户信息.");
List<Account> accounts = accountDao.findAll();
for (Account account: accounts) {
System.out.println(account);
}
return null;
}
}
4.4 编写测试类:
package cn.wanghao.ssm.test;
import cn.wanghao.ssm.service.AccountService;
import cn.wanghao.ssm.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
@Test
public void run1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = ac.getBean("accountServiceImpl", AccountService.class);
accountService.findAll();
}
}
注意:ac.getBean("accountServiceImpl", AccountService.class)
这个很关键。如果有问题可以参考博客:https://blog.csdn.net/qq_43546676/article/details/105269474
4.5 结果
这样spring整合springmvc和mybatis就成功了!!!!
5. 最后附上完整的配置代码
5.1 web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app.xsd"
version="3.1">
<!-- 配置加载类路径的配置文件 -->
<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>
<!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码过滤器 -->
<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>
</web-app>
5.2 springmvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="cn.wanghao.ssm">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"></context:include-filter>
</context:component-scan>
<!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JSP文件所在的目录 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 设置静态资源不过滤 -->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
</beans>
5.3 applicationContext.xml文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描,要扫描的是service层注解,要忽略web层注解,因为web层让SpringMVC框架 去管理 -->
<context:component-scan base-package="cn.wanghao.ssm.service"></context:component-scan>
<!--spring整合mybatis框架-->
<!--1. 引入属性文件,在配置中占位使用 -->
<context:property-placeholder location="classpath:druid.properties"/>
<!--2. 配置连接池对象: 使用Druid连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--配置连接数据库的4个基本信息-->
<!-- driverClassName属性可以不写,会根据url自动判断 -->
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}" />
<property name="username" value="root" />
<property name="password" value="xxxxxx" />
</bean>
<!-- 3. 创建会话工厂:SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 别名 -->
<property name="typeAliasesPackage" value="cn.wanghao.ssm.domain"></property>
<!-- sql映射文件路径。扫描mappers目录以及子目录下的所有xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml"/>
</bean>
<!-- 4. 配置扫描dao的包,由spring创建对象-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.wanghao.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事务 -->
<!-- 1. 配置一个事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入 DataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2. 事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--在 tx:advice 标签内部 配置事务的属性 -->
<tx:attributes>
<!-- 对所有的方法设置可读写 -->
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<!-- 优先级高。find方法中对数据库的操作只能读数据 -->
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置 aop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* cn.wanghao.ssm.service.impl.*.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
<!--支持注解驱动的事务管理,指定事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>