SpringMVC中MongoDB如何使用事务
1.配置mongoDB事务,核心代码如下所示
<bean id="mongoTransactionManager" class="org.springframework.data.mongodb.MongoTransactionManager">
<constructor-arg name="dbFactory" ref="mongoDbFactory"/>
</bean>
<tx:annotation-driven transaction-manager="mongoTransactionManager" proxy-target-class="true"/>
2.业务中使用MongoDB案例代码
//加上事务注解
@Transactional(rollbackFor = Exception.class)
public void insertTest(Student student) {
mongoTemplate.insert(student,"student");
//回滚测试
int i = 1/0;
}