天天看點

SSM之Service層基于注解的聲明式事物

原文位址:

前面我們已經做了SpringMVC和MyBatis整合的DAO設計,如果還沒有整合,那麼請參考:

http://blog.csdn.net/uq_jin/article/details/51527404。

現在我們來做Service層的設計

整合完成圖

SSM之Service層基于注解的聲明式事物

UserService.java

這裡是一個Service接口:站在”使用者”的角度去設計

public interface UserService {
    public List<User> findAll();

    public void save(User user);
}
           

UserServiceImpl.java

接口的具體實作,這裡采用注解來實作依賴注入,以及基于注解的聲明式事物

@Service
public class UserServiceImpl implements UserService {

    @Autowired //注入DAO
    private UserDao mUserDao;

    public List<User> findAll() {
        return mUserDao.findAll();
    }

    @Transactional
    public void save(User user) {
        mUserDao.save(user);
    }
}
           

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-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    <!-- 掃描所有的Service -->
    <context:component-scan base-package="me.jinkun.ssm.service"/>

    <!-- 事務管理 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--
        基于注解的聲明時事物 :
        1、開發團隊達成一緻約定,明确标注事物的方法
        2、保證事物方法的執行時間盡可能短
        3、不是所有的方法都需要事物,如隻有一條修改記錄操作
    -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
           

logback.xml

logback的日志配置,這裡是預設配置,具體配置見:

https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast

<!-- 參考:https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

    <logger name="me.jinkun.ssm" level="TRACE"/>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
           

UserServiceTest.java

Junit 測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/spring/*.xml"})
public class UserServiceTest {

    Logger mLogger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserService mUserService;

    @Test
    public void save() {
        User user = new User();
        user.setUsername("jack");
        user.setUserpass("1234");
        mUserService.save(user);
    }

    @Test
    public void findAll() {
        List<User> userList = mUserService.findAll();
        for (User user : userList) {
            System.out.println(user);
        }
        mLogger.info(userList.toString());
    }
}
           

檢視測試結果

查詢:

SSM之Service層基于注解的聲明式事物

這裡有5個使用者,我們測試儲存使用者并測試事物是否生效。如下:

我們需要在UserServiceImpl的save方法裡抛出一個異常看是否插入進去資料

SSM之Service層基于注解的聲明式事物

檢視測試結果:

SSM之Service層基于注解的聲明式事物

先插入,然後查詢,發現還是5條記錄,說明事物成功。删掉異常再次測試如下:

SSM之Service層基于注解的聲明式事物

發現多了一個id為7的記錄,那是因為6被復原了,是以新資料的id為7

源碼下載下傳位址: https://github.com/cskun/SSM.git