这里我写了一些基本的代码,用于我的角色不起作用的事务(仅供参考:我正在使用Postgre数据库)。我是新来的春天。如果有任何错误请纠正我。以下是代码:
我的dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="Included beans, context, aop, tx">
<context:component-scan base-package="com.x.y"> </context:component-scan>
<bean id="MyDAOI" class="com.x.y.DAO.MyDAOImpl">
<property name="dataSource" ref="datasource"></property>
</bean>
<bean id="MyServiceI" class="com.x.y.Service.serviceImpl">
<property name="myDAOI" ref="MyDAOI"></property>
</bean>
<bean id="datasource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver_class_name}"></property>
<property name="url" value="${db_url}"></property>
<property name="username" value="${db_uname}"></property>
<property name="password" value="${db_password}"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="Included beans, context, aop, tx">
<context:component-scan base-package="com.x.y"> </context:component-scan>
<bean id="MyDAOI" class="com.x.y.DAO.MyDAOImpl">
<property name="dataSource" ref="datasource"></property>
</bean>
<bean id="MyServiceI" class="com.x.y.Service.serviceImpl">
<property name="myDAOI" ref="MyDAOI"></property>
</bean>
<bean id="datasource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver_class_name}"></property>
<property name="url" value="${db_url}"></property>
<property name="username" value="${db_uname}"></property>
<property name="password" value="${db_password}"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
我的控制器:
我的ServiceInterface:(MyServiceI)
@Controller
public class MyController{
public String formupload(){
//Autowired serviceImpl object name = serviceImpl
success = serviceImpl.saveMyData(data);
}
}
我的ServiceImpl:(MyServiceImpl)
public interface MyServiceI {
public int getDuplicateRow(String selectQuery, Object[] object, int[] types);
public boolean saveMyData(Sheet data) throws Exception;
}
我的DAOI :( MyDAOI)
public class MyServiceImpl implements MyServiceI {
private static Map<String, Integer> mapSQLTypeToInt = SQLTypeToJavaTypeMap.getJdbcTypeInteger();
@Autowired
private MyDAOI myDAOI;
public MyDAOI getMyDAOI() {
return myDAOI;
}
public void setMyDAOI(MyDAOI myDAOI) {
this.myDAOI = myDAOI;
}
@Override
public int getDuplicateRow(query, object, types){
//Any exception from here should also rollback total transaction.
return myDAOI.getDuplicateRow();
}
@Transactional(
rollbackFor = Exception.class)
@Override
public boolean saveMyData(Sheet data) throws Exception{
/*Loop around the Sheet and check if the duplicate exist - rollback every thing inserted else commit all data which is inserted */
try{
foreach(Row rodData: data) {
Object[] ColumnObject = geteachColumnData(Map columnKeys)
if(getDuplicateRow(query, object, types) != 0){
//Any exception from here should also rollback total transaction.
throw new Exception("Error :Duplicate");
}
else{
//Any exception from here should also rollback total transaction.
myDAOI.insertRow(query, object, types);//DAOImpl.insertRow
}
}
return true;
}catch(Exception e){
throw new Exception(e.getMessage());
}finally{
return false;
}
}
public Object[] geteachColumnData(Map columnKeys){
//Any exception from here should also rollback total transaction.
return object;
}
}
我的DAOI Impl:(MyDAOImpl)
public interface MyDAOI {
public int getDuplicateRow(String selectQuery, Object[] object, int[] types);
public int insertRow(String insertQuery, Object[] object,
int[] types) throws Exception;
}