Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to configure @ Transactional in Spring

Shulou Source: shulou.com Published: 2022-06-01 11:06:51 09月26日 Update

This article will explain in detail how to configure @Transactional in Spring. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

Background:

Older versions of Spring use TransactionProxyFactoryBean to configure Spring transactions (disadvantages own Google, a lot of disadvantages)

Spring 2.x introduces AOP (AOP)

At the beginning, the project also likes to use the spring xml configuration. Later, the project uses the spring3.x version and sees the @Transactional annotation. Personally, I think it is quite convenient and practical. (For whatever reason, I don't know)

code on

classpath:/db.properties classpath:/sqlmap-config.xml classpath*:/sqlmap/*.xml stat 20 1 60000 1 60000 300000 SELECT 'x' true false false true 20 package com.xun.spring3.src.dao;import com.ibatis.sqlmap.client.SqlMapClient;import org.apache.commons.lang.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import javax.annotation.PostConstruct;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;/** * Require the name of each dao to end with Dao * Generic ENTITY -which entity * Generic PRIMARYKEY -type of specific primary key * @Date : 2014/9/7 0007 20:41 * @From : spring3-test * @Author : hebad90@163.com */public class BaseDao extends SqlMapClientDaoSupport { @Autowired private SqlMapClient sqlMapClient; @PostConstruct private void initSuper() { /** * Initialize parent class */ super.setSqlMapClient( sqlMapClient ); } private Class entityClass ; private String ibatisNamespace; protected BaseDao() { Type genType = getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType)genType).getActualTypeArguments(); entityClass = (Class)params[0]; /** * Get namespace XxxDao of current ibatis, i.e. namespace xxx */ this.ibatisNamespace = getIbatisNamespace( getClass() ); System.out.println( "Initialize current environment successfully, ibatisNamespace=["+this.ibatisNamespace+"],entityClass=[" + entityClass + "]" ); } public PRIMARYKEY insert( ENTITY entity ) { return(PRIMARYKEY)super.getSqlMapClientTemplate().insert( this.ibatisNamespace + ".insert", entity ); } public int update( ENTITY entity ) { return super.getSqlMapClientTemplate().update( this.ibatisNamespace + ".update", entity ); } public ENTITY queryById( PRIMARYKEY primarykey ) { return ( ENTITY ) super.getSqlMapClientTemplate().queryForObject( this.ibatisNamespace + ".queryById", primarykey); } public int delete( ENTITY entity ) { return super.getSqlMapClientTemplate().delete( this.ibatisNamespace + ".delete", entity); } ///############################## private String getIbatisNamespace( Class clazz ) { String simpleName = clazz.getSimpleName(); int index = StringUtils.indexOf( simpleName, "Dao" ); return StringUtils.lowerCase( StringUtils.substring( simpleName, 0, index ) ); }}

Where is @Transactional used? Spring recommends that we use this annotation on our business implementation classes because java implementations cannot inherit annotations. Therefore, it is best to annotate the business implementation class so that the business class enables transactions regardless of whether proxy-target-class is true or false.

How to use @Transactional parameters?

About "Spring @Transactional how to configure" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Transactions aspects annotations configuration business attributes articles agents utility transaction management names subclasses more versions spaces shortcomings projects management good success Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Shulou Information Huawei Redmi