自下而上整合SSM框架(业务层)

此篇为自下而上搭建SSM框架子篇,如果第一次看该系列,请先看总篇:自上而下的搭建SSM框架

分析

业务层其实比较简单,业务层主要只有一个需要配置就是,事务的配置,事务的配置目前比较主流的有声明式事务,以及使用面向切面编程(AOP)去控制事务,这里只介绍声明式事务

配置事务

引入spring事务管理jar

<!-- 3)持久层 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>4.3.7.RELEASE</version>
</dependency>

在resources/spring下配置spring-service.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: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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 扫描service包下所有使用注解的类型 -->
    <context:component-scan base-package="com.csdn.shop.service" />

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置基于注解的声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

实现service层

在service包下编写接口

package com.csdn.shop.service;

import com.csdn.shop.entity.Area;

public interface AreaService {
    void insertArea(Area area);
}

在service/impl下编写实现类

package com.csdn.shop.service.impl;

import com.csdn.shop.dao.AreaDao;
import com.csdn.shop.entity.Area;
import com.csdn.shop.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service  //业务层注解
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaDao areaDao;

    //事务注解
    @Transactional
    public void insertArea(Area area) {
        areaDao.insertArea(area);

        //抛出一个异常看是否回滚
        throw new RuntimeException("自定义异常");
    }
}

测试

编写service测试类

package com.csdn.shop.service;

import com.csdn.shop.entity.Area;
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 java.util.Date;

@RunWith(SpringJUnit4ClassRunner.class)
//告诉Junit spring配置文件的位置
@ContextConfiguration({"classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml"})
public class AreaServiceTest {
    @Autowired
    private AreaService areaService;

    @Test
    public void testAreaService() {
        Area area = new Area();
        area.setAreaName("上海");
        area.setCreateTime(new Date());

        areaService.insertArea(area);
    }
}

执行,发现控制台抛出自定义异常,并且查看数据库没有新增上海数据,说明进行了回滚,事务控制成功

猜你喜欢

转载自blog.csdn.net/weixin_42604515/article/details/81294701
今日推荐