spring boot nutz 事务 怎么整合
9 回复
package com.donfo;
import javax.sql.DataSource;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
//import org.nutz.integration.spring.SpringDaoRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
public class OecTableinitApplication {
public static void main(String[] args) {
SpringApplication.run(OecTableinitApplication.class, args);
}
@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public Object testBean(PlatformTransactionManager platformTransactionManager) {
System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
return new Object();
}
/*@Bean
public SpringDaoRunner springDaoRunner(){
SpringDaoRunner sb = new SpringDaoRunner();
return sb;
}
@Bean
public Dao nutDao(DataSource dataSource,SpringDaoRunner springDaoRunner){
NutDao dao = new NutDao(dataSource);
dao.setRunner(springDaoRunner);
return dao;
}*/
}
pom:
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz-plugins-spring-boot-starter</artifactId>
<version>${nutz.version}</version>
</dependency>
<!-- <dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz-integration-spring</artifactId>
<version>${nutz.version}</version>
</dependency> -->
package com.donfo.service;
import javax.transaction.Transactional;
import org.nutz.dao.Dao;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Transactional
@Service
public class TestService {
@Autowired
private Dao dao;
public void test(){
Person p = new Person();
p.setName("ABC");
p.setAge(20);
dao.insert(p);
System.out.println(p.getId());
}
@Transactional
public void test2(){
Person p = new Person();
p.setName("ABC125");
p.setAge(20);
dao.insert(p);
System.out.println(p.getId());
Person p2 = new Person();
p2.setName("ABC125");
p2.setAge(20);
dao.insert(p2);
System.out.println(p2.getId());
}
public void testTra(){
Person p = new Person();
p.setName("ABC123");
p.setAge(20);
System.out.println(p.getId());
Person p2 = new Person();
p2.setName("ABC123");
p2.setAge(20);
System.out.println(p2.getId());
Trans.exec(new Atom(){
public void run() {
dao.update(p);
dao.update(p2);
}
});
}
}
执行 test2 因为那么为唯一的索引 所以后台会报错的 但是还是插入了一条数据 请问哪里有问题
你的意思 我只用在 pom引用:
org.nutz
nutz-plugins-spring-boot-starter
${nutz.version}
然后 在代码里面不用直接加入: @Bean
public SpringDaoRunner springDaoRunner(){
SpringDaoRunner sb = new SpringDaoRunner();
return sb;
}
@Bean
public Dao nutDao(DataSource dataSource,SpringDaoRunner springDaoRunner){
NutDao dao = new NutDao(dataSource);
dao.setRunner(springDaoRunner);
return dao;
}
就可以了吗
事务开启不是这个吗:@EnableTransactionManagement
添加回复
请先登陆