Dao类:
package com.wh;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* @Author:wh007
*
* @Date:2012-8-1
*
* @TODO:事务测试
*/
@Repository
public class TransactionDao {
private static Logger logger = LoggerFactory
.getLogger(TransactionDao.class);
@Autowired
private JdbcTemplate jdbcTemplate;
private static final String SQL_CREATE_ROLE_MODULE = "INSERT INTO ROLE_MODULE (ROLE_ID , MODULE_ID) VALUES(? , ?) ";
private static final String SQL_DELETE_ROLE_MODULE = "DELETE FROM ROLE_MODULE WHERE ID = ? ";
public boolean createRoleModule(String roleId, String moduleId) throws Exception {
try {
logger.info("start execute createRoleModule method!");
jdbcTemplate.update(SQL_CREATE_ROLE_MODULE, new Object[] { roleId,
moduleId });
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("end execute createRoleModule method wrong !");
throw e;
}
}
public boolean deleteRoleModule(String id) throws Exception {
try {
logger.info("start execute deleteRoleModule method!");
if (id != null && !id.equals("")) {
jdbcTemplate.update(SQL_DELETE_ROLE_MODULE,
new Object[] { id });
return true;
}
} catch (Exception e) {
e.printStackTrace();
logger.info("end execute deleteRoleModule method wrong !");
throw e;
}
logger.info("end execute deleteRoleModule method!");
return false;
}
}
service类:
package com.wh;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
/**
* @Author:wh007
*
* @Date:2012-8-1
*
* @TODO:
*/
@Service
public class TransactionService {
@Resource
TransactionDao dao;
public void create(String[][] array) throws Exception{
for(int i = 0 ; i < array.length ; i++){
dao.createRoleModule(array[i][0],array[i][1]);
}
}
public void delete(String[] ids) throws Exception{
for(String id : ids){
System.out.print("id : " + Integer.parseInt(id));
dao.deleteRoleModule(id);
}
}
public void doComplexThings(String id , String roleId , String moduleId) throws Exception {
// if(dao.deleteRoleModule(id)){
// dao.createRoleModule(roleId,moduleId);
// }
if(dao.createRoleModule(roleId,moduleId)){
dao.deleteRoleModule(id);
}
}
}
JUnit:
package com.wh;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.rockstar.TransactionDao;
import com.rockstar.TransactionService;
/**
* @Author:wh007
*
* @Date:2012-8-1
*
* @TODO:
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml",
"classpath:applicationContext-service.xml",
"classpath:applicationContext-dao.xml" })
public class TestTransactionService extends AbstractJUnit4SpringContextTests {
private static Logger logger = LoggerFactory
.getLogger(TestTransactionService.class);
@Resource
private TransactionService service;
@Resource
private TransactionDao dao;
@Before
public void setUp() throws Exception {
if (service != null && dao != null) {
logger.info("init success!");
}
}
@Test
@Ignore
public void testDaoCreate() {
String[][] array = {
{"3","4"},
{"5","6"},
{"A","S"}
};
for(int i = 0 ; i < array.length ; i++){
try {
Assert.assertTrue(dao.createRoleModule(array[i][0] , array[i][1]));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Test
@Ignore
public void testServiceCreate() {
String[][] array = {
{"3","4"},
{"5","6"},
{"A","S"}
};
try {
service.create(array);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
@Ignore
public void testServiceDelete() {
String[] array = {"6",","};
try {
service.delete(array);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testComplexThings() throws Exception{
String id = "15";
String roleId = "23";
String moduleId = "3";
service.doComplexThings(id, roleId, moduleId);
}
}
applicationContext.xml片段:
<!-- Transaction manager for a single JDBC DataSource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 以AspectJ方式 定义 AOP --> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.wh..*.*(..))" advice-ref="txAdvice"/> </aop:config> <!-- 基本事务定义,使用transactionManager作事务管理,默认get* find*方法的事务为readonly,其余方法按默认设置. 默认的设置请参考Spring文档事务一章. --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="*" /> </tx:attributes> </tx:advice>