从功能角度没有存在问题,但是在设计角度存在缺陷,就是MVC层与其他层次存在耦合,
如果日后,替换了MVC的实现,整体的代码都将受到影响,如果日后把SpringMVC换成Strus2,Webflat的话,换了MVC不仅仅影响MVC的内容,也影响到其他层次
通过两个工厂,解决MVC层与非MVC层之间的耦合
父容器:
子工厂:
diapatcher.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:mvc="http://www.springframework.org/schema/mvc" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--设置注解扫描的路径 -->
<context:component-scan base-package="com.baizhiedu"/>
<!--引入springMVC的核心功能-->
<mvc:annotation-driven />
<!--视图解析器配置: 控制器方法返回的结果的前缀和后缀-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
在web.xml中添加父容器ContextLoadListener创建工厂 :
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 通过父容器创建工厂解决SpringMVC耦合问题-->
<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>
<!-- 解决中文乱码问题:Spring给我们提供的过滤器CharacterEncodingFilter-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 初始化servlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数:指定SpringMVC配置文件的路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher.xml</param-value>
</init-param>
<!-- 本servlet会在tomcat启动的时候,就会被创建-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--暴露servlet的url的访问路径-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
父工厂:
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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 包扫描路径-->
<context:component-scan base-package="com.baizhiedu"/>
<!--创建连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&charaterEcoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 创建SqlSessionFactory SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--指定类别名-->
<property name="typeAliasesPackage" value="com.baizhiedu.entiry"/>
<!--指定mapper文件-->
<property name="mapperLocations">
<!--mapperLocations对应的是数组类型的赋值 spring内置关键字代表src-->
<list>
<value>classpath:com.baizhiedu.mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<!--创建DAO对象 MapperScannerConfigure -->
<bean id="confiure" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.baizhiedu.dao"></property>
</bean>
<!-- 指定额外功能 事务-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 整合切面-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
User:
package com.baizhiedu.entity;
public class User {
private Integer id;
private String name;
private String password;
public User() {
}
public User(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDAO:
package com.baizhiedu.dao;
import com.baizhiedu.entity.User;
public interface UserDAO {
public void save(User user);
}
UserDAOMapper.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="com.baizhiedu.dao.UserDAO">
<!--使用insert,update,delete,select标签来写sql语句-->
<!--useGeneratedKeys="true"主键自增-->
<insert id="save" useGeneratedKeys="true">
insert into tb_users(name,password) values (#{name},#{password})
</insert>
</mapper>
UserService:
package com.baizhiedu.service;
import com.baizhiedu.entity.User;
public interface UserService {
public void register(User user);
}
UserServiceImpl:
package com.baizhiedu.service;
import com.baizhiedu.dao.UserDAO;
import com.baizhiedu.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service //使用@Service注解创建这个类的对象
@Transactional //使用注解,添加事务属性
public class UserServiceImpl implements UserService {
@Autowired //注入userdao
private UserDAO userDAO;
@Override
public void register(User user) {
userDAO.save(user);
}
}
控制器:
UserController:
package com.baizhiedu.controller;
import com.baizhiedu.entity.User;
import com.baizhiedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/register")
public String register(User user){
userService.register(user);
return "regOk";
}
}
reg.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/4
Time: 18:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user/register">
UserName:<input type="text" name="name"><br>
Password:<input type="text" name="password"><br>
<input type="submit" value="reg">
</form>
</body>
</html>
regOk.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/4
Time: 18:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h1>RegOK</h1>
</body>
</html>
现在的ssm整合开发父子容器处理过程当中是没有事务的,下面打印没有输出create new Transction开启事务
解决:在包扫描上子容器不扫描com.baizhiedu这个包,只让他扫描com.baizhiedu.Controller,这样他就不会创建Service对象,他就会往父容器里面找Service
让zi工厂只创建Controller:
diapatcher.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:mvc="http://www.springframework.org/schema/mvc" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--设置注解扫描的路径 -->
<!-- 设置只扫描conroller包即可,这样就不会创建事务-->
<context:component-scan base-package="com.baizhiedu.controller"/>
<!--引入springMVC的核心功能-->
<mvc:annotation-driven />
<!--视图解析器配置: 控制器方法返回的结果的前缀和后缀-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
让父容器排除创建Controller:
<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 包扫描路径-->
<context:component-scan base-package="com.baizhiedu">
<!-- 排除创建Controller-->
<context:exclude-filter type="aspectj" expression="com.baizhiedu.controller.*"/>
</context:component-scan>
<!--创建连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&charaterEcoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 创建SqlSessionFactory SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--指定类别名-->
<property name="typeAliasesPackage" value="com.baizhiedu.entiry"/>
<!--指定mapper文件-->
<property name="mapperLocations">
<!--mapperLocations对应的是数组类型的赋值 spring内置关键字代表src-->
<list>
<value>classpath:com.baizhiedu.mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<!--创建DAO对象 MapperScannerConfigure -->
<bean id="confiure" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.baizhiedu.dao"></property>
</bean>
<!-- 指定额外功能 事务-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 整合切面-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
从新运行,添加上了事务: