Spring framework (6) - JDBCTemplate Spring transaction control and use of

Spring Framework has packaged JDBCTemplate can be used to manually create too much trouble and there is no reference value, just use the code shown below JDBCTemplate cases developed by the annotation:

Steps for usage:

1. Import coordinates associated

Using a druid link pool:

 1     <dependencies>
 2         <dependency>
 3             <groupId>mysql</groupId>
 4             <artifactId>mysql-connector-java</artifactId>
 5             <version>5.1.47</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>com.alibaba</groupId>
 9             <artifactId>druid</artifactId>
10             <version>1.1.10</version>
11         </dependency>
12         <dependency>
13             <groupId>org.springframework</groupId>
14             <artifactId>spring-context</artifactId>
15             <version>5.0.5.RELEASE</version>
16         </dependency>
17         <dependency>
18             <groupId>org.springframework</groupId>
19             <artifactId>spring-jdbc</artifactId>
20             <version>5.0.5.RELEASE</version>
21         </dependency>
22       
27         <dependency>
28             <groupId>org.aspectj</groupId>
29             <artifactId>aspectjweaver</artifactId>
30             <version>1.8.13</version>
31         </dependency>
32     </dependencies>

2. Create a database table

Create your own.

3. Create the implementation class

Create your own.

4. Configure Spring core profile

 <!--扫描包-->
    <context:component-scan base-package="com.itheima"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssm_spring"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</bean>

 

5. Create an object and use JDBCTemplate

Dao layer in the implementation class by obtaining injection annotation objects:

 @Autowired
    private JdbcTemplate template;

JDBCTemplate call the query or update method on it.

6. Test

Self-test.

Repeatability with Spring (4) - Notes development is relatively high, so do not write a detailed step.

 

Spring's transaction control:

Spring transaction control into programmatic and declarative transaction control transaction control.

Programmatic transaction control means controlling to open or close the transaction by the transaction control coding. Too troublesome to learn on the line.

Declarative transaction control is to open or close the transaction by the configuration. Declarative transaction control is divided into xml-based declarative transaction control and annotation-based declarative transaction control.

 

Programmatic transaction control:

Programmatic transaction control three objects:

PlatformTransactionManager

TransactionDefinition

TransactionStatus

 

PlatformTransactionManager interface is the spring of transaction manager, which it provides to our common method of operation of its services.

note:

PlatformTransactionManager is the interface type, the different layers of Dao technology has a different implementation class, for example: Dao layer technology is jdbc or mybatis: org.springframework.jdbc.datasource.DataSourceTransactionManager

Dao layer technology is hibernate when: org.springframework.orm.hibernate5.HibernateTransactionManager

 

TransactionDefinition transaction information object is defined, which has the following method:

Transaction isolation level

Setting the isolation level, you can solve the problem of generating transaction concurrency, such as dirty reads, non-repeatable reads and phantom reads.

ISOLATION_DEFAULT

ISOLATION_READ_UNCOMMITTED

ISOLATION_READ_COMMITTED

ISOLATION_REPEATABLE_READ

ISOLATION_SERIALIZABLE

事务传播行为

REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)

SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)

MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常

REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。

NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起

NEVER:以非事务方式运行,如果当前存在事务,抛出异常

NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作

超时时间:默认值是-1,没有超时限制。如果有,以秒为单位进行设置

是否只读:建议查询时设置为只读

 

TransactionStatus 接口提供的是事务具体的运行状态,方法介绍如下。

 以上了解就可以了。

 

声明式事务控制:

使用步骤:

1.导入相应的jar包

 1 <!-- spring的事务管理 -->
 2 <dependency>
 3     <groupId>org.springframework</groupId>
 4     <artifactId>spring-tx</artifactId>
 5     <version>5.0.5.RELEASE</version>
 6 </dependency>
 7 <!-- Aop的切入点表达式解析 -->
 8 <dependency>
 9     <groupId>org.aspectj</groupId>
10     <artifactId>aspectjweaver</artifactId>
11     <version>1.8.7</version>
12 </dependency>

2.配置事务管理器

1 <!-- 事务管理器(在Spring的核心配置文件中) -->
2 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
3     <property name="dataSource" ref="dataSource"/>
4 </bean>

3.配置事务的切面和织入关系

xml配置:

 1 <!--事务切面-->
 2 <aop:config>
 3     <aop:advisor advice-ref="txAdvice" 
 4                  pointcut="execution(* com.itheima.service.impl.*Impl.*(..))">
 5     </aop:advisor>
 6 </aop:config>
 7 <!--事务的详细配置-->
 8 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 9     <tx:attributes>
10         <tx:method name="save*" propagation="REQUIRED" read-only="false"/>
11         <tx:method name="update*" propagation="REQUIRED" read-only="false"/>
12         <tx:method name="delete*" propagation="REQUIRED" read-only="false"/>
13         <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
14         <tx:method name="*"/>
15     </tx:attributes>
16 </tx:advice>

注解配置:

在Spring核心配置文件中:

<!--开启Spring声明式事务的注解支持-->
<tx:annotation-driven />

在对应的类上加入事务注解:

 1 @Transactional  //表示当前类的所有方法都加入注解支持 propagation:REQUIRED read-only:false
 2 public class AccountServiceImpl implements AccountService {
 3 
 4     @Autowired
 5     private AccountDao accountDao;
 6 
 7     public void transfer(String outMan, String inMan, double money) {
 8         accountDao.out(outMan, money);
 9         int i = 10 / 0;
10         accountDao.in(inMan, money);
11     }
12 }

4.测试

Guess you like

Origin www.cnblogs.com/j9527/p/12036167.html