SpringBoot注解与xml方式进行事务配置(八)

接上一节(第七节)Mybatis配置完后(https://blog.csdn.net/qq_37431224/article/details/103894091

项目结构:

这里提供2种方法:法一:注解配置

                               法二: 基于XML的事务管理配置

一、注解配置:

1.在主配置类中贴上@EnableTransactionManagement表示开启事务注解驱动支持.等价XML配置的: <tx:annotaion-driven/>  

@SpringBootApplication
/**
 * 扫描Mybatis的Mapper类,相当于
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="cn.wolfcode.crm.mapper"/>
 </bean>
 @MapperScan中包含@ComponentScan,所以必须精确到mapper包下,否则如果在上一层扫描到IService接口的话又会再一次装配,出现错误
 */
@MapperScan("com.demo._day04MyBatis.mapper")
//配置事务扫描,相当于以前的<tx:annotation-driven/>
//这句话可以不写,因为springboot已经自动加了,但在Service中必须加Transactional

@EnableTransactionManagement
public class AppConfig {
    public static void main(String[] args) {
        try {
            SpringApplication.run(AppConfig.class,args);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 2.在需要事务的Service上贴上@Transactional注解,表示该类下面的所有方法都需要事务.

@Service
//配置事务,注解方式,如果是applicationcon.xml方式则把这句话去掉
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    //可读事务
    @Transactional(readOnly = true)
    public List<Permission> selectAll(){
        return permissionMapper.selectAll();
    }

    //配置事务后出错会回滚
    public void save(Permission permission)
    {   int i=1/0;
        permissionMapper.insert(permission);
    }
}

3.配置后在Controller设置执行方法:,访问地址如localhost/save报错,但save方法并没有写入数据库,即配置成功

@Controller
public class PermissionController {
    @Autowired
    IPermissionService permissionService;
    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        permissionService.save(new Permission());
        return "save";
    }
}

二、基于XML的事务管理步骤:

1.添加依赖(主要是要添加支持AspectJ表达式的支持)

<!--applicationc.xml中有AspectJ语法,所以需要导包,这里干脆直接导入springboot的aop包,
 即常用事务功能都支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <optional>true</optional>
        </dependency>

2.在资源目录中添加applicationContext-tx.xml配置文件.

	<!--txManager以前在DataSourceTransactionManager中引入,现转回Config类中自己构建其Bean
	 <bean id="transactionManager" 
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	-->
	<tx:advice id="advice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="list*" read-only="true"/>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut expression="execution(* com.demo._day04MyBatis.*service.*(..))" id="pointCut"/>
		<aop:advisor advice-ref="advice" pointcut-ref="pointCut"/>
	</aop:config>

3.在主配置类(AppConfig.java)中添加事务管理器PlatformTransactionManager,以及引入applicationContext-tx.xml文件.

@SpringBootApplication
/**
 * 扫描Mybatis的Mapper类,相当于
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="cn.wolfcode.crm.mapper"/>
 </bean>
 @MapperScan中包含@ComponentScan,所以必须精确到mapper包下,否则如果在上一层扫描到IService接口的话又会再一次装配,出现错误
 */
@MapperScan("com.demo._day04MyBatis.mapper")

//引入配置applicationContext.xml,与注解一起混用的方式
@ImportResource("classpath:applicationContext-tx.xml")

public class AppConfig {
    public static void main(String[] args) {
        try {
            SpringApplication.run(AppConfig.class,args);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * txManager以前在DataSourceTransactionManager中引入,现转回Config类中自己构建其Bean
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
     </bean>
     */
    @Bean
    public PlatformTransactionManager platformTransactionManager(DataSource dataSource){
        DataSourceTransactionManager txManager=new DataSourceTransactionManager();
        txManager.setDataSource(dataSource);
        return txManager;
    }
}

配置完成, 测试方法与上面一致。

这里推荐方法一,因为SpringBoot就是为了去除xml配置文件而存在的!!!

 

猜你喜欢

转载自blog.csdn.net/qq_37431224/article/details/103894477
今日推荐