ssm框架搭建

结合前面文章,把spring ,springmvc,mybatis集成在一起。

新建maven项目ssm.

步骤:其中,先整合spring和mybatis,再整合springmvc

1,myecplise:new -web project,勾选 add maven support

2,pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ssmTest</groupId>
    <artifactId>ssmTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name />
    <description />
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.annotation</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.ejb</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-osgi-bundle</artifactId>
            <version>1.0.1-SP3</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.servlet</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid 版本太高会出错 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
        <!-- <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> 
            <version>1.2.2</version> </dependency> -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.4.Release</version>
        </dependency>
        <dependency>
            <groupId> org.aspectj</groupId>
            <artifactId> aspectjweaver</artifactId>
            <version> 1.8.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- spirng mvc jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.4.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2,src下加入这些文件

applicationContext.xml:

<!-- test mybatis-spring -->
<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"
    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
    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
    http://www.springframework.org/schema/mvc    
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.aluo">
    </context:component-scan>
    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 配置c3p0连接池 -->
    <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
        <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" 
        value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> 
        <property name="password" value="${jdbc.password}" /> </bean> -->
    <!-- 配置数据源druid -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClass}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置事务的增强 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />

            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />

            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut expression="execution(* com.aluo.service.*.*(..))"
            id="pointcut1" />
        <!-- 配置切面 -->
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointcut1" />
    </aop:config>
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-- myBatis文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 配置数据库表对应的java实体类 -->
        <property name="typeAliasesPackage" value="com.aluo.domain.User" />
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath:com/aluo/mybatis/*.xml" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.aluo.mybatis" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

</beans>

jdbc.properties:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/luo
jdbc.username=root
jdbc.password=123456

log4j.properties:

log4j.rootLogger=INFO,Console,File  
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  
  
#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6  
log4j.appender.File = org.apache.log4j.RollingFileAppender  
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55  
log4j.appender.File.File = logs/ssm.log  
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F  
log4j.appender.File.MaxFileSize = 10MB  
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7  
log4j.appender.File.Threshold = ALL  
log4j.appender.File.layout = org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

ssm-servlet.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 http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 自动扫描controller包下的所有类,使其认为是spring mvc的控制器 路径即为类路径 -->
    <context:component-scan base-package="com.aluo.control">
    </context:component-scan>
    <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 根据控制器返回的字符串拼接成jsp路径:/WEB-INF/jsp/xx.jsp -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

3,加入以下类:

User:

package com.aluo.domain;

public class User {

    public User(int id, String phone, String password) {
        super();
        this.id = id;
        this.phone = phone;
        this.password = password;
    }

    private int id;
    private String phone;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

接口UserMapper:

package com.aluo.mybatis;

import java.util.List;

import com.aluo.domain.User;

public interface UserMapper {
    User selectUser(int id);

    List<User> selectUserList();

    void insertUser(User user);

    void deleteUserById(int id);
}

UserMapper.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.aluo.mybatis.UserMapper">
    <select id="selectUser" resultType="com.aluo.domain.User">
        select * from user where id =
        #{id}
    </select>
    <select id="selectUserList" resultType="com.aluo.domain.User">
        select * from user
    </select>
    <insert id="insertUser">
        insert into user (phone,password)
        values
        (#{phone},#{password})
    </insert>
    <delete id="deleteUserById">
        delete from user where id = #{id}
    </delete>
</mapper>

接口UserDao:

package com.aluo.dao;

import java.util.List;

import com.aluo.domain.User;

public interface UserDao {
    User selectUser(int id);

    List<User> selectUserList();

    void inserttUser(User user);

    void deleteUserById(int id);
}

实现类UserDaoImpl:

package com.aluo.dao.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aluo.dao.UserDao;
import com.aluo.domain.User;
import com.aluo.mybatis.UserMapper;

@Repository 
public class UserDaoImpl implements UserDao {
    @Autowired
    UserMapper userMapper;

    @Override
    public User selectUser(int id) {
        return userMapper.selectUser(id);
    }

    @Override
    public List<User> selectUserList() {
        return userMapper.selectUserList();
    }

    @Override
    public void inserttUser(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public void deleteUserById(int id) {
        userMapper.deleteUserById(id);
    }

}

接口UserService:

package com.aluo.service;

import java.util.List;

import com.aluo.domain.User;

public interface UserService {
    User selectUser(int id);

    List<User> selectUserList();

    void insertUser(User user);

    void deleteUserById(int id);
}

实现类:UserServiceImpl:

package com.aluo.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aluo.dao.UserDao;
import com.aluo.domain.User;
import com.aluo.service.UserService;
import com.aluo.test.ssmTest;
@Service
//@Transactional (propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public User selectUser(int id) {
        return userDao.selectUser(id);
    }

    @Override
    public List<User> selectUserList() {
        return userDao.selectUserList();
    }

    @Override
    public void insertUser(User user) {
        userDao.inserttUser(user);
       // int  a = 12/0;
    }

    @Override
    public void deleteUserById(int id) {
        userDao.deleteUserById(id);
    }

}

Controller类UserController:

package com.aluo.control;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aluo.domain.User;
import com.aluo.service.UserService;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/getUsser")
    public String getUser(ModelMap model) {
        model.addAttribute("user", userService.selectUser(3));
        return "user";
    }
    
    @RequestMapping("/selectUserList")
    public String selectUserList(ModelMap model) {
        model.addAttribute("userList", userService.selectUserList());
        return "userList";
    }
    
    @RequestMapping("/insertUser")
    public String insertUser(ModelMap model) {
        User user = new User(7, "7777", "777");
        userService.insertUser(user);
        model.addAttribute("message","insert success" );
        return "message";
    }
    
    @RequestMapping("/deleteUserById")
    public String deleteUserById(ModelMap model) {
        userService.deleteUserById(3);
        model.addAttribute("message", "delete success");
        return "message";
    }
}

4,新建测试类SsmTest:

package com.aluo.test;

import org.apache.log4j.Logger;
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;

import com.aluo.dao.UserDao;
import com.aluo.domain.User;
import com.aluo.mybatis.UserMapper;
import com.aluo.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SsmTest {
    private static Logger logger = Logger.getLogger(SsmTest.class);
    @Autowired
    private UserService userService;

    @Test
    public void test() {
        System.out.println("begin");
        System.out.println(userService.selectUser(3));
//        User user = new User(2, "999", "999");
//        userService.insertUser(user);
        // logger.info("value:" + userMapper.selectUser(3));
    }
}

output: com.aluo.domain.User@5aacc14

至此,spring和mybatis整合成功。接下来,再整合springmvc

5,web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <!-- 配置SpringMVC下的配置文件位置及名称 -->
   <display-name>Spring MVC Application</display-name>
   <!-- Spring和mybatis的配置文件 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext.xml</param-value>  
    </context-param>  
   
   <!-- Springmvc的配置文件 -->  
   <servlet>
      <servlet-name>ssm</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ssm-servlet.xml</param-value>
    </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>ssm</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
 <!-- Spring监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- 防止Spring内存溢出监听器 -->  
    <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>  

</web-app>

6,web-inf下面新建文件夹jsp,再新建user.jsp,message.jsp,userList.jsp,贴出关键部分:

<body>
    This is my JSP page. <br>
    ${user }
  </body>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
    This is my JSP page.
    <br>
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>phone</td>
            <td>password</td>
        </tr>
        <c:forEach items="${userList}" var="c" varStatus="st">
            <tr>
                <td>${c.id}</td>
                <td>${c.phone}</td>
                <td>${c.password}</td>
            </tr>
        </c:forEach>
    </table>
</body>

 <body>
    This is my JSP page. <br>
    ${message }
  </body>

7,项目运行在tomcat7服务器。

访问http://localhost:8080/ssm/getUsser,如图:

访问:http://localhost:8080/ssm/selectUserList,如图:

访问:http://localhost:8080/ssm/insertUser,如图:

至此,基本搭建完成。

上事务:

有两种,一种是注解,二是xml配置aop.

一,注解

1,applicationContext.xml加上以下代码

<!-- 开启注解事务 -->
 <tx:annotation-driven transaction-manager="transactionManager" />

2,类UserServiceImpl的方法insertUser加上出错代码

     int  a = 12/0;

3,UserServiceImpl加上

     @Transactional (propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)

如图;

4,再运行http://localhost:8080/ssm/insertUser,提示出错,事务回滚。

二,xml配置aop

1,,applicationContext.xml加上以下代码

<!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置事务的增强 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />

            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />

            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut expression="execution(* com.aluo.service.*.*(..))"
            id="pointcut1" />
        <!-- 配置切面 -->
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointcut1" />
    </aop:config>
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

 

2,类UserServiceImpl的方法insertUser加上出错代码

     int  a = 12/0;

3,再运行http://localhost:8080/ssm/insertUser,提示出错,事务回滚。

 

posted @ 2018-10-01 21:48  阿罗luo  阅读(140)  评论(0编辑  收藏  举报