一、环境搭建
1、基本环境搭建
1.1、创建数据库和表结构
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">create</span> database ssm;
use ssm;
<span style="color:#770088">create</span> <span style="color:#770088">table</span> account(
id <span style="color:#3300aa">int</span> primary key auto_increment,
name <span style="color:#3300aa">varchar</span>(<span style="color:#116644">20</span>),
money <span style="color:#3300aa">double</span>
);</span></span>
1.2、新建一个Maven的web项目
1.3、导入jar包坐标(pom.xml)
<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.2.12.RELEASE</spring.version>
<slf4j.version>1.7.32</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>8.0.26</mysql.version>
<mybatis.version>3.5.7</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<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-web</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-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</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>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
1.4、建包
1.5、创建tomcat服务器
二、搭建三层架构和测试
2.1、搭建Spring框架和测试
搭建Spring容器,将常⽤对象的创建和销毁交给Spring容器来管理
2.1.1、项目中新建一个Spring配置文件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"
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">
<!--开启注解的扫描,后期跟springmvc整合时,把扫描放在springmvc.xml⽂件中-->
<context:component-scan base-package="com.soft" >
</context:component-scan>
</beans>
2.1.2、写测试类进行测试:
2.1.2.1、创建账户数据表对应的类
package com.soft.domain;
public class Account {
private long id;
private String name;
private double money;
public long getId() {
return id;
}
public void setId(long 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;
}
}
2.1.2.2、编写service类
接口:
package com.soft.service;
import com.soft.domain.Account;
import java.util.List;
public interface AccountService {
List<Account> findAll();
}
接口实现类:
package com.soft.service.impl;
import com.soft.domain.Account;
import com.soft.service.AccountService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Override
public List<Account> findAll() {
System.out.println("业务层---->查询所有的账户");
return null;
}
}
2.1.2.3、测试建包
2.1.2.4、Spring集成测试框架
AccountServiceTest01:
package com.soft.service;
import com.soft.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:ApplicationContext.xml")
public class AccountServiceTest01 {
@Autowired
private AccountServiceImpl accountService;
@Test
public void findAllTest(){
accountService.findAll();
}
}
2.1.2.5、添加日志文件
注:上面由于缺少日志文件有警告,我们需要在resources目录下添加log4j.properties文件
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=D:/Java_MyBatis/MyBatis_log4j/ssm.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
三、搭建与整合SpringMVC
3.1搭建SpringMVC
3.1.1、在web.xml中配置DispatcherServlet前端控制器
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置前端的控制器,服务器启动必须加载,需要加载springmvc.xml配置⽂件-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.1.2、在springmvc.xml配置中文的过滤器,配置开启注解扫描,配置过滤静态资源,开启springmvc.xml注解支持
注: 如果springmvc.xml开启了注解扫描,那么spring中(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: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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解扫描,spring中的扫描关闭-->
<context:component-scan base-package="com.soft">
</context:component-scan>
<!--配置的视图解析器对象-->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/image/" mapping="/image/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!--开启SpringMVC注解的⽀持-->
<mvc:annotation-driven/>
</beans>
3.1.3、index.jsp页面与success页面编写
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Hello World!</h2>
<a href="account/findAll">查找所有的帐户</a>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>成功</h1>
</body>
</html>
3.1.4、创建控制器
package com.soft.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/account")
public class AccountController {
//查找所有的⽤户
@RequestMapping("/findAll")
public String findAll(){
System.out.println("表现层--->查询所有的⽤户");
return "success";
}
}
四、Spring整合MyBatis框架
4.1、Spring整合MyBatis框架
修改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"
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">
<!--开启注解的扫描,后期跟springmvc整合时,把扫描放在springmvc.xml⽂件中-->
<!-- <context:component-scan base-package="com.soft" >
</context:component-scan>-->
<!-- 配置C3P0的连接池对象 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///ssm"/>
<property name="username" value="root"/>
<property name="password" value="12345678"/>
</bean>
<!-- 配置SqlSession的⼯⼚ -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置扫描dao的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.soft.dao"/>
</bean>
</beans>
4.2、配置监听器,加载applicationContext.xml配置
在web.xml中配置监听,开启监听,在监听中读取applicationContext.xml配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--启动读取ApplicationContext.xml配置-->
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端的控制器,服务器启动必须加载,需要加载springmvc.xml配置⽂件-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
五、ssm整个流程测试
5.1、index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Hello World!</h2>
<a href="account/findAll">查找所有的帐户</a>
</body>
</html>
5.2、controller包的AccountController类
package com.soft.controller;
import com.soft.domain.Account;
import com.soft.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
//查找所有的⽤户
@RequestMapping("/findAll")
public ModelAndView findAll(){
System.out.println("表现层--->查询所有的⽤户");
List<Account> accounts = accountService.findAll();
//创建ModelAndView
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accounts",accounts);
modelAndView.setViewName("success");
return modelAndView;
}
}
5.3、service包下的AccountServiceImpl类
5.3.1、接口类
package com.soft.service;
import com.soft.domain.Account;
import java.util.List;
public interface AccountService {
List<Account> findAll();
}
5.3.2、接口实现类
package com.soft.service.impl;
import com.soft.dao.IAccountDao;
import com.soft.domain.Account;
import com.soft.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAll() {
System.out.println("业务层--查询所有的帐户");
List<Account> accounts = accountDao.findAll();
return accounts;
}
}
5.4、dao包下的IAccountDao
package com.soft.dao;
import com.soft.domain.Account;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface IAccountDao {
@Select("select * from Account")
public List<Account> findAll();
}
5.5、success.jsp
<%@ 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>
<h1>成功</h1>
${accounts}
</body>
</html>
5.6、展示结果
六、添加事务管理
6.1、转账场景
A帐户钱减少 B帐户钱增加 在这⾥我们只做最简单的业务实现,不考虑具体的钱的变少,变多
6.2、index.jsp界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Hello World!</h2>
<a href="account/findAll">查找所有的帐户</a>
<a href="account/transfer?id1=1&id2=2&money1=100&money2=200">转帐</a>
</body>
</html>
6.3、创建请求参数封装的类
package com.soft.domain;
public class TransferInfo {
private int id1;
private double money1;
private int id2;
private double money2;
public int getId1() {
return id1;
}
public void setId1(int id1) {
this.id1 = id1;
}
public double getMoney1() {
return money1;
}
public void setMoney1(double money1) {
this.money1 = money1;
}
public int getId2() {
return id2;
}
public void setId2(int id2) {
this.id2 = id2;
}
public double getMoney2() {
return money2;
}
public void setMoney2(double money2) {
this.money2 = money2;
}
@Override
public String toString() {
return "TransferInfo{" +
"id1=" + id1 +
", money1=" + money1 +
", id2=" + id2 +
", money2=" + money2 +
'}';
}
}
6.4、控制器类中添加对应的transfer方法
package com.soft.controller;
import com.soft.domain.Account;
import com.soft.domain.TransferInfo;
import com.soft.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
//查找所有的⽤户
@RequestMapping("/findAll")
public ModelAndView findAll(){
System.out.println("表现层--->查询所有的用户");
List<Account> accounts = accountService.findAll();
//创建ModelAndView
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accounts",accounts);
modelAndView.setViewName("success");
return modelAndView;
}
//转帐的操作
@RequestMapping("/transfer")
public String transfer(TransferInfo transferInfo) {
System.out.println(transferInfo);
//表现层调⽤业务层的⽅法实现功能
accountService.transfer(transferInfo);
return "success";
}
}
6.5、业务层service类中添加对应实现的方法
6.5.1、业务层接口
public interface AccountService {
//查找⽤户
List<Account> findAll();
//转帐
void transfer(TransferInfo transferInfo);
}
6.5.2、业务层具体实现
package com.soft.service.impl;
import com.soft.dao.IAccountDao;
import com.soft.domain.Account;
import com.soft.domain.TransferInfo;
import com.soft.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAll() {
System.out.println("业务层--查询所有的帐户");
List<Account> accounts = accountDao.findAll();
return accounts;
}
@Override
public void transfer(TransferInfo transferInfo) {
//分别更新两个帐户的钱
//业务层调⽤dao层数据模型层的更新帐户的⽅法
accountDao.updateMoney(transferInfo.getId1(),transferInfo.getMoney1());
//int a = 3/0;//出现异常
accountDao.updateMoney(transferInfo.getId2(),transferInfo.getMoney2());
}
}
6.5.3、dao层具体的实现
package com.soft.dao;
import com.soft.domain.Account;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface IAccountDao {
@Select("select * from account")
public List<Account> findAll();
@Update("update Account set money=${money} where id=${id}")
void updateMoney(@Param("id") int id, @Param("money") double money);
}
6.5.4、转账出现的问题
如果在业务层transfer()⽅法中出现异常,那么有可能两个帐户只更新⼀个帐户的信息,这不符合现实的操作,我们需要使用事务
6.5.5、使用xml配置完成事务
在springvc.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描,spring中的扫描关闭-->
<context:component-scan base-package="com.soft">
</context:component-scan>
<!--配置的视图解析器对象-->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/image/" mapping="/image/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!--开启SpringMVC注解的⽀持-->
<mvc:annotation-driven/>
<!-- 1.配置事务的管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 指定要对哪个数据库进⾏事务操作 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注解完成事务-->
<!--<tx:annotation-driven></tx:annotation-driven>-->
<!--配置事务的通知-->
<tx:advice id="advice">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置切⾯-->
<aop:config>
<!-- 6.5.6、使⽤注解配置完成事务-->
<!-- 6.5.6.1、springmvc.xml中添加事务配置-->
<!-- 6.5.6.2、在业务层添加事务的注解@Transactional-->
<aop:pointcut id="pointcut" expression="execution(* com.soft.service.impl.*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
6.5.6、使用注解配置完成事务
a、springmv.xml中添加事务配置
<!-- 1.配置事务的管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 指定要对哪个数据库进⾏事务操作 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注解完成事务-->
<tx:annotation-driven></tx:annotation-driven>
b、在业务层添加事务的注解@Transactional
package com.soft.service.impl;
import com.soft.dao.IAccountDao;
import com.soft.domain.Account;
import com.soft.domain.TransferInfo;
import com.soft.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAll() {
System.out.println("业务层--查询所有的帐户");
List<Account> accounts = accountDao.findAll();
return accounts;
}
@Override
public void transfer(TransferInfo transferInfo) {
//分别更新两个帐户的钱
//业务层调⽤dao层数据模型层的更新帐户的⽅法
accountDao.updateMoney(transferInfo.getId1(),transferInfo.getMoney1());
// int a = 3/0;//出现异常
accountDao.updateMoney(transferInfo.getId2(),transferInfo.getMoney2());
}
}
七、拓展
7.1添加中文过滤器
在web.xml中添加中文的过滤
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--启动读取ApplicationContext.xml配置-->
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<!--配置中⽂过滤器-->
<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>
<!--启动监听器加载ApplicationContext.xml配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端的控制器,服务器启动必须加载,需要加载springmvc.xml配置⽂件-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>