(025) Spring Boot of JdbcTemplate and Transactional Transactions

(A) springboot the JdbcTemplate provided to achieve efficient operation of the database, recorded as follows:

  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

  Results are as follows:

 (B) springboot use Transactional annotation processing services (actually spring in the comments)

  Abnormalities will rollback (1) default operation, no need to specify rollbackFor: as @Transactional (rollbackFor = Exception.class) represents all exceptions rollback.

  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 (. the ProductDao class ) .addBitch ( "Test 1", "Test 2", "Test 3" "Test 4" );
        context.close();
    }
} 
View Code

  1, the test case without comment:

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

  Run a result, no rollback, as follows:

  2, testing the default add annotations, throwing an exception case runtime:

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

  Operating results, rollback, as follows:

   3, testing the default add annotations, throw checked exceptions situation:

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

  Run a result, no rollback, as follows:

   4, add annotations rollbackFor the test, throws an exception specified circumstances:

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

  Operating results, rollback, as follows:

  (2) Transactional annotation added to the above method can only be called directly, they will not be rolled back

  1, the following code will rollback

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();
            }
        }
    }
    
}

  The following code will not be rolled back

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();
            }
        }
    }
    
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/javasl/p/11966664.html