(025)Spring Boot之JdbcTemplate與Transactional事務處理


(一)springboot提供了JdbcTemplate類來快捷的實現操作數據庫,記錄如下:

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edu.spring</groupId>
    <artifactId>springboot_web</artifactId>
    <version>1.0.0</version>

    <name>springboot_web</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>2.0.4.RELEASE</version> 
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

</project>
View Code

  application.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3309/springboot
spring.datasource.username=root
spring.datasource.password=123456
View Code

  ProductDao.java

package com.edu.spring.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public void add(String name){
        String sql="insert into t_product(pname) values('"+name+"')";
        jdbcTemplate.execute(sql);
    }
    
}
View Code

  App.java

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App    
{ 
    public static void main(String[] args) throws Exception{
        ConfigurableApplicationContext context=SpringApplication.run(App.class, args); 
        context.getBean(ProductDao.class).add("測試1");
        context.close();
    }
} 
View Code

  運行結果如下:

 (二)springboot中使用Transactional注解處理事務(其實是spring中的注解)

  (1)默認運行時異常才會回滾,否需要rollbackFor指定:如@Transactional(rollbackFor=Exception.class)表示所有異常回滾。

  App.java

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App    
{ 
    public static void main(String[] args) throws Exception{
        ConfigurableApplicationContext context=SpringApplication.run(App.class, args); 
        context.getBean(ProductDao.class).addBitch("測試1","測試2","測試3","測試4");
        context.close();
    }
} 
View Code

  1、測試不加注解的情況:

package com.edu.spring.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public void addBitch(String... names) throws Exception{
        for(String name:names){
            String sql="insert into t_product(pname) values('"+name+"')";
            jdbcTemplate.execute(sql);
            if("".equals("")){
                throw new NullPointerException();
            }
        }
    }
    
}
View Code

  運行結果,沒有回滾,如下:

  2、測試添加默認注解,拋出運行時異常的情況:

package com.edu.spring.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Transactional
    public void addBitch(String... names) throws Exception{
        for(String name:names){
            String sql="insert into t_product(pname) values('"+name+"')";
            jdbcTemplate.execute(sql);
            if("".equals("")){
                throw new NullPointerException();
            }
        }
    }
    
}
View Code

  運行結果,回滾,如下:

   3、測試添加默認注解,拋出檢查異常的情況:

package com.edu.spring.springboot;

import java.io.FileNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Transactional
    public void addBitch(String... names) throws Exception{
        for(String name:names){
            String sql="insert into t_product(pname) values('"+name+"')";
            jdbcTemplate.execute(sql);
            if("".equals("")){
                throw new FileNotFoundException();
            }
        }
    }
    
}
View Code

  運行結果,沒有回滾,如下:

   4、測試添加rollbackFor的注解,拋出指定異常的情況:

package com.edu.spring.springboot;

import java.io.FileNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Transactional(rollbackFor=Exception.class)
    public void addBitch(String... names) throws Exception{
        for(String name:names){
            String sql="insert into t_product(pname) values('"+name+"')";
            jdbcTemplate.execute(sql);
            if("".equals("")){
                throw new FileNotFoundException();
            }
        }
    }
    
}
View Code

  運行結果,回滾,如下:

  (2)Transactional注解加在直接調用的方法上面才可以,否則不會回滾

  1、下面的代碼會回滾

package com.edu.spring.springboot;

import java.io.FileNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Transactional(rollbackFor=Exception.class)
    public void addBitch(String... names) throws Exception{
        actiontest(names);
    }
    
    private void actiontest(String...names) throws Exception{
        for(String name:names){
            String sql="insert into t_product(pname) values('"+name+"')";
            jdbcTemplate.execute(sql);
            if("".equals("")){
                throw new FileNotFoundException();
            }
        }
    }
    
}

  下面的代碼不會回滾

package com.edu.spring.springboot;

import java.io.FileNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public void addBitch(String... names) throws Exception{
        actiontest(names);
    }
    
    @Transactional(rollbackFor=Exception.class)
    private void actiontest(String...names) throws Exception{
        for(String name:names){
            String sql="insert into t_product(pname) values('"+name+"')";
            jdbcTemplate.execute(sql);
            if("".equals("")){
                throw new FileNotFoundException();
            }
        }
    }
    
}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM