SSM 整合
1. 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.0.2.RELEASE</ spring.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-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>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.11</ version>
< scope> compile</ scope>
</ 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> 1.2.17</ version>
</ dependency>
< dependency>
< groupId> org.slf4j</ groupId>
< artifactId> slf4j-log4j12</ artifactId>
< version> 1.7.12</ 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.1</ version>
< type> jar</ type>
< scope> compile</ scope>
</ dependency>
</ dependencies>
2. 编写实体类
package com. demo. entity;
import java. io. Serializable;
public class Account implements Serializable {
private Integer id;
private String name;
private Float 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 Float getMoney ( ) {
return money;
}
public void setMoney ( Float money) {
this . money = money;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}' ;
}
}
3. 编写 dao 接口
package com. demo. dao;
import com. demo. entity. Account;
import org. apache. ibatis. annotations. Delete;
import org. apache. ibatis. annotations. Insert;
import org. apache. ibatis. annotations. Select;
import org. springframework. stereotype. Repository;
import java. util. List;
@Repository
public interface IAccountDao {
@Select ( "select * from account" )
List< Account> findAll ( ) ;
@Select ( "select * from account where id = #{id}" )
Account findById ( Integer id) ;
@Insert ( "insert into account (name,money) values (#{name},#{money})" )
void save ( Account account) ;
@Delete ( "delete from account where id = #{id}" )
void delete ( Integer id) ;
}
4. 编写 service 接口和实现类
package com. demo. service;
import com. demo. entity. Account;
import java. util. List;
public interface IAccountService {
List< Account> findAll ( ) ;
Account findById ( Integer id) ;
void save ( Account account) ;
void delete ( Integer id) ;
}
package com. demo. service. impl;
import com. demo. dao. IAccountDao;
import com. demo. entity. Account;
import com. demo. service. IAccountService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import java. util. List;
@Service ( "accountService" )
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List< Account> findAll ( ) {
return accountDao. findAll ( ) ;
}
@Override
public Account findById ( Integer id) {
return accountDao. findById ( id) ;
}
@Override
public void save ( Account account) {
accountDao. save ( account) ;
}
@Override
public void delete ( Integer id) {
accountDao. delete ( id) ;
}
}
5. 编写 controller 类
package com. demo. controller;
import com. demo. entity. Account;
import com. demo. service. IAccountService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Controller;
import org. springframework. ui. Model;
import org. springframework. web. bind. annotation. PathVariable;
import org. springframework. web. bind. annotation. RequestMapping;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import java. io. IOException;
import java. util. List;
@Controller ( "accountController" )
@RequestMapping ( "/account" )
public class AccountController {
@Autowired
private IAccountService accountService;
@RequestMapping ( "/find" )
public String findAll ( Model model) {
List< Account> accounts = accountService. findAll ( ) ;
model. addAttribute ( "accounts" , accounts) ;
return "success" ;
}
@RequestMapping ( "/findById/{id}" )
public String findById ( @PathVariable ( name = "id" ) Integer id, Model model) {
Account account = accountService. findById ( id) ;
model. addAttribute ( "account" , account) ;
return "findById" ;
}
@RequestMapping ( "/save" )
public void save ( Account account, HttpServletResponse response, HttpServletRequest request) throws IOException {
accountService. save ( account) ;
response. sendRedirect ( request. getContextPath ( ) + "/account/find" ) ;
}
@RequestMapping ( "/delete/{id}" )
public String delete ( @PathVariable ( name = "id" ) Integer id) {
Account account = accountService. findById ( id) ;
if ( account != null) {
accountService. delete ( id) ;
return "success" ;
}
return "error" ;
}
}
6. 编写 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>
< 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>
< 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>
< 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>
</ web-app>
7. 创建 springmvc 配置文件(springmvc.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: mvc= " http://www.springframework.org/schema/mvc"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< context: component-scan base-package = " com.demo" >
< context: include-filter type = " annotation" expression = " org.springframework.stereotype.Controller" />
</ context: component-scan>
< bean id = " viewResolver" class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/pages/" > </ property>
< property name = " suffix" value = " .jsp" > </ property>
</ bean>
< mvc: annotation-driven/>
</ beans>
8. 创建 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: aop= " http://www.springframework.org/schema/aop"
xmlns: tx= " http://www.springframework.org/schema/tx"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd" >
< context: component-scan base-package = " com.demo" >
< 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://localhost:3306/south" />
< 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 = " com.demo.dao" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< tx: advice id = " txAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " *" propagation = " REQUIRED" read-only = " false" />
< tx: method name = " find*" propagation = " SUPPORTS" read-only = " true" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " pt1" expression = " execution(* com.demo.service.impl.*.*(..))" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " pt1" />
</ aop: config>
</ beans>
9. 编写 index.jsp 页面和 pages 文件夹下的展示页面进行测试
<%--index.jsp页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> Title</ title>
</ head>
< body>
< h2> 测试查询所有</ h2>
< a href = " account/find" > 查询所有</ a>
< h2> 测试查询一个</ h2>
< a href = " account/findById/1" > 查询一个</ a>
< h2> 测试保存</ h2>
< form action = " /account/save" method = " post" >
姓名:< input type = " text" name = " name" /> < br/>
金额:< input type = " text" name = " money" /> < br/>
< input type = " submit" value = " 保存" />
</ form>
< h2> 测试删除</ h2>
< a href = " account/delete/2" > 删除</ a>
</ body>
</ html>
<%--error.jsp页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> Title</ title>
</ head>
< body>
< h2> 失败了...</ h2>
</ body>
</ html>
<%--success.jsp页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
< html>
< head>
< title> Title</ title>
</ head>
< body>
< h2> 成功</ h2>
${accounts}
</ body>
</ html>
<%--findById.jsp页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
< html>
< head>
< title> Title</ title>
</ head>
< body>
< h2> 成功</ h2>
${account}
</ body>
</ html>