Spring|Spring AOP事务管理的示例详解

目录

  • 转账案例-环境搭建
    • 步骤1:准备数据库表
    • 步骤2:创建项目导入jar包
    • 步骤3:根据表创建模型类
    • 步骤4:创建Dao接口
    • 步骤5:创建Service接口和实现类
    • 步骤6:添加jdbc.properties文件
    • 步骤7:创建JdbcConfig配置类
    • 步骤8:创建MybatisConfig配置类
    • 步骤9:创建SpringConfig配置类
    • 步骤10:编写测试类
  • 事务管理

    转账案例-环境搭建
    步骤1:准备数据库表
    之前我们在整合Mybatis的时候已经创建了这个表,可以直接使用
    create database spring_db character set utf8; use spring_db; create table tbl_account(id int primary key auto_increment,name varchar(35),money double); insert into tbl_account values(1,'Tom',1000); insert into tbl_account values(2,'Jerry',1000);


    步骤2:创建项目导入jar包
    项目的pom.xml添加相关依赖
    org.springframeworkspring-context5.2.10.RELEASEcom.alibabadruid1.1.16org.mybatismybatis3.5.6mysqlmysql-connector-java5.1.47org.springframeworkspring-jdbc5.2.10.RELEASEorg.mybatismybatis-spring1.3.0junitjunit4.12testorg.springframeworkspring-test5.2.10.RELEASE


    步骤3:根据表创建模型类
    public class Account implements Serializable {private Integer id; private String name; private Double money; //setter...getter...toString...方法略}


    步骤4:创建Dao接口
    public interface AccountDao {@Update("update tbl_account set money = money + #{money} where name = #{name}")void inMoney(@Param("name") String name, @Param("money") Double money); @Update("update tbl_account set money = money - #{money} where name = #{name}")void outMoney(@Param("name") String name, @Param("money") Double money); }


    步骤5:创建Service接口和实现类
    public interface AccountService {/*** 转账操作* @param out 传出方* @param in 转入方* @param money 金额*/public void transfer(String out,String in ,Double money) ; }@Servicepublic class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao; public void transfer(String out,String in ,Double money) {accountDao.outMoney(out,money); accountDao.inMoney(in,money); }}


    步骤6:添加jdbc.properties文件
    jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=falsejdbc.username=rootjdbc.password=root


    步骤7:创建JdbcConfig配置类
    public class JdbcConfig {@Value("${jdbc.driver}")private String driver; @Value("${jdbc.url}")private String url; @Value("${jdbc.username}")private String userName; @Value("${jdbc.password}")private String password; @Beanpublic DataSource dataSource(){DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; }}


    步骤8:创建MybatisConfig配置类
    public class MybatisConfig {@Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.itheima.domain"); ssfb.setDataSource(dataSource); return ssfb; }@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; }}


    步骤9:创建SpringConfig配置类
    @Configuration@ComponentScan("com.itheima")@PropertySource("classpath:jdbc.properties")@Import({JdbcConfig.class,MybatisConfig.class})public class SpringConfig {}


    步骤10:编写测试类
    @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = SpringConfig.class)public class AccountServiceTest {@Autowiredprivate AccountService accountService; @Testpublic void testTransfer() throws IOException {accountService.transfer("Tom","Jerry",100D); }}


    事务管理 上述环境,运行单元测试类,会执行转账操作,Tom的账户会减少100,Jerry的账户会加100。
    这是正常情况下的运行结果,但是如果在转账的过程中出现了异常,如:
    @Servicepublic class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao; public void transfer(String out,String in ,Double money) {accountDao.outMoney(out,money); int i = 1/0; accountDao.inMoney(in,money); }}

    【Spring|Spring AOP事务管理的示例详解】以上就是Spring AOP事务管理的示例详解的详细内容,更多关于Spring AOP事务管理的资料请关注脚本之家其它相关文章!

      推荐阅读