SpringMVC学习之路 第三篇(完结篇)
在前两篇的基础上,今天我们继续学习SpringMVC框架。 本文涉及以下内容: 1. SSM框架的整合 。
1.1 先看看项目的整体结构图:
1.2 在数据库中创建account表:
create database ssm;
create table account (
id int primary key auto_increment,
name varchar ( 100 ) ,
money double ( 7 , 2 ) ,
) ;
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> 5.1.6</ mysql.version>
< mybatis.version> 3.4.5</ mybatis.version>
</ properties>
< dependencies>
< 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-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>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> ${mysql.version}</ version>
</ dependency>
< 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>
< 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>
< 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> c3p0</ groupId>
< artifactId> c3p0</ artifactId>
< version> 0.9.1.2</ version>
< type> jar</ type>
< scope> compile</ scope>
</ dependency>
</ dependencies>
1.4 编写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>
< 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>
< 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>
< 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>
1.5 加入log4j.properties,内容如下:
log4j. rootCategory= info, CONSOLE, LOGFILE
log4j. logger. org. apache. axis. enterprise= FATAL, CONSOLE
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
log4j. appender. LOGFILE= org. apache. log4j. FileAppender
log4j. appender. LOGFILE. File= d: \axis. 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
1.6 编写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" >
< context: component-scan base-package = " fang" >
< context: exclude-filter type = " annotation" expression = " org.springframework.stereotype.Controller" />
</ context: component-scan>
< bean id = " dataSource" class = " com.mchange.v2.c3p0.ComboPooledDataSource" >
< property name = " driverClass" value = " com.mysql.jdbc.Driver" />
< property name = " jdbcUrl" value = " jdbc:mysql:///ssm" />
< property name = " user" value = " root" />
< property name = " password" value = " root" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< bean id = " mapperScanner" class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " fang.dao" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< 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: config>
< aop: advisor advice-ref = " txAdvice" pointcut = " execution(* fang.service.impl.*ServiceImpl.*(..))" />
</ aop: config>
</ beans>
1.7 编写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" >
< context: component-scan base-package = " fang" >
< context: include-filter type = " annotation" expression = " org.springframework.stereotype.Controller" />
</ 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 = " /images/" mapping = " /images/**" />
< mvc: resources location = " /js/" mapping = " /js/**" />
< mvc: annotation-driven/>
</ beans>
1.8 编写Account类,忽略get等方法:
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
}
1.9 编写持久层接口IAccountDao:
@Repository ( "accountDao" )
public interface IAccountDao {
@Select ( "select * from account" )
public List< Account> findAll ( ) ;
@Insert ( "insert into account(name,money) values(#{name},#{money})" )
public void saveAccount ( Account account) ;
}
1.10 编写业务层接口IAccountService:
public interface IAccountService {
public List< Account> findAll ( ) ;
public void saveAccount ( Account account) ;
}
1.11 编写业务层实现类AccountServiceImpl:
@Service ( "accountService" )
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List< Account> findAll ( ) {
System. out. println ( "业务层:查询所有账户..." ) ;
return accountDao. findAll ( ) ;
}
@Override
public void saveAccount ( Account account) {
System. out. println ( "业务层:保存帐户..." ) ;
accountDao. saveAccount ( account) ;
}
}
1.12 编写表现层AccountController:
@Controller
@RequestMapping ( "/account" )
public class AccountController {
@Autowired
private IAccountService accountService;
@RequestMapping ( "/findAll" )
public String findAll ( Model model) {
System. out. println ( "表现层:查询所有账户。。。" ) ;
List< Account> list = accountService. findAll ( ) ;
model. addAttribute ( "list" , list) ;
return "list" ;
}
@RequestMapping ( "/save" )
public void save ( Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
accountService. saveAccount ( account) ;
response. sendRedirect ( request. getContextPath ( ) + "/account/findAll" ) ;
return ;
}
}
1.13 编写index.jsp:
<%@ 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>
1.14 编写list.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>
< h3> 查询所有的帐户</ h3>
< c: forEach items = " ${list}" var = " account" >
${account.name}
</ c: forEach>
</ body>
</ html>