在spring-mvc配置文件中加如下配置:
<!--事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 使用注解事务,需要添加Transactional注解属性 --> <tx:annotation-driven transaction-manager="transactionManager"/>
然后在对应的service类下加如下注解:
@Transactional(rollbackFor = Exception.class)
示例代码:
@Service public class MapServiceImpl implements MapService { @Autowired private MapDao dao; @Override @Transactional(rollbackFor = Exception.class) public void insertMapPoint(List<MapPoint> mapPointList) { for(int i=0;i<mapPointList.size();i++){ mapPointList.get(i).setSaveTime(new Date()); dao.insertMapPoint(mapPointList.get(i)); } } }