Springboot integrates transactions and hikari connection pool

Insert picture description here
1.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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>

    </parent>
    <groupId>cn.itcast</groupId>
    <artifactId>Spingboot_day1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.9</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--java配置配置数据库要的-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!--Spring Boot的属性注入需要的-->
        <dependency>
            <groupId> org.springframework.boot </groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <!--不传递依赖-->
            <optional>true</optional>
        </dependency>

        <!--lombok需要的-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--事务和连接池需要的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
    </dependencies>
</project>

2. As for transactions, SpringBoot is controlled by annotations. Just set it on the corresponding class or method when using the well-known @Transactional
.

Create a service to demonstrate the transaction:

package com.itheima.service;

import com.itheima.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/2/21 22:01
 */
@Service
public class UserService {
    
    

    public User queryById(Long id){
    
    
        //根据id查询
        return new User();
    }

    //Transactional如果你其中一个error会自动回滚的
    @Transactional
    public void saveUser(User user){
    
    
        //现在暂时不写具体代码,假假演示
        System.out.println("新增用户");
    }
}

Connection pool usage:
Insert picture description here

Insert picture description here
Insert picture description hereTherefore, we only need to specify the connection pool parameters; open application.yml to add and modify the configuration as follows:


spring:
  profiles:
    active: abc,def
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot_test
    username: root
    data-password: root

#tomcatΪ端口
server:
  port: 81

#日志记录级别
logging:
  level:
    com.itheima: debug
    org.springframework: info


Test Insert picture description here
Start the project, visit http://localhost:81/hello1; view the background output, you can also get the datasource in HelloController. (Break point)Insert picture description here

Guess you like

Origin blog.csdn.net/GLOAL_COOK/article/details/113925072