Spring Boot整合JPA druid

一、数据库test中数据表stu的内容
Spring Boot整合JPA druid_第1张图片
二、去maven官网查找依赖 ,maven地址:Maven官网

  1. 搜索druid,由于是 springboot 和 druid整合,所以选择第二个Druid Spring Boot Starter ,点击进入查看各种版本。
    Spring Boot整合JPA druid_第2张图片
  2. 点击版本号1.2.6 ,可以看到不同构建工具如何添加druid依赖,选择Maven,复制依赖代码,添加到pom.xml中。


    com.alibaba
    druid-spring-boot-starter
    1.2.16

Spring Boot整合JPA druid_第3张图片

三、在pom.xml文件中添加依赖。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.9
         
    
    dzxx
    crud
    0.0.1-SNAPSHOT
    crud
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
                
        
            org.springframework.boot
            spring-boot-starter-web
        
         
        
            com.alibaba
            druid-spring-boot-starter
            1.2.16
        
      
        
	        com.mysql
	        mysql-connector-j
	        runtime
	            
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


三、在application.properties文件中配置数据库连接,同时在pom.xml文件中添加log4j2的依赖。


    log4j
    log4j
    1.2.17


#服务器配置
server.port=8080
server.servlet.context-path=/
server.tomcat.uri-encoding=utf-8
server.tomcat.max-connections=10000
server.tomcat.accept-count=1000
server.tomcat.threads.max=1000
server.tomcat.threads.min-spare=500
#数据库连接属性配置
#第一种配置方式
#spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
#spring.datasource.url= jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
#spring.datasource.username= root
#spring.datasource.password= 1234
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#第二种配置方式(推荐)
spring.datasource.druid.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.druid.url= jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.druid.username= root
spring.datasource.druid.password= 1234
#druid 数据源专有配置
spring.datasource.druid.initialSize= 5
spring.datasource.druid.min-Idle= 5
spring.datasource.druid.max-active= 20
spring.datasource.druid.max-Wait= 60000
spring.datasource.druid.time-between-eviction-runs-millis= 60000
spring.datasource.druid.min-evictable-idle-time-millis= 300000
spring.datasource.druid.validation-query= SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle= true
spring.datasource.druid.test-on-borrow= false
spring.datasource.druid.test-on-return= false
spring.datasource.druid.pool-prepared-statements = true
spring.datasource.druid.filters= stat,wall,log4j2
spring.datasource.druid.max-pool-prepared-statement-per-connection-size= 20
spring.datasource.druid.use-global-data-source-stat= true
spring.datasource.druid.connection-properties= druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#druid连接池监控
##druid的版本在1.1.10版本以上需要配置开启 stat-view-servlet ,不配置情况下为默认不开监控页面,
# 其他一些配置 如sql监控也是默认不开启的,若是要开启某个功能要手动开启
spring.datasource.druid.stat-view-servlet.enabled = true
spring.datasource.druid.stat-view-servlet.url-pattern = /druid/*
spring.datasource.druid.stat-view-servlet.login-username = admin
spring.datasource.druid.stat-view-servlet.login-password = 123
spring.datasource.druid.web-stat-filter.enabled = true
spring.datasource.druid.web-stat-filter.url-pattern = /*
spring.datasource.druid.web-stat-filter.exclusions = *.js,*.jpg

#JPA的配置
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

四、创建实体类

package dzxx.crud.pojo;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Entity(name = "stu")
public class Stu {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String pass;
    private int age;
}

五、创建自定义JpaRepository接口

package dzxx.crud.repository;
import dzxx.crud.pojo.Stu;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StuRepository extends JpaRepository{}

六、编写测试方法

package dzxx.crud;
import dzxx.crud.pojo.Stu;
import dzxx.crud.repository.StuRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;

@SpringBootTest
class CrudApplicationTests {
    @Autowired
    private StuRepository stuRepository;
    @Test
    void contextLoads() {
        List stuList=stuRepository.findAll();
        for (Stu stu:stuList){
            System.out.println(stu.toString());
        }
    }
}

七、运行测试方法contextLoads()
Spring Boot整合JPA druid_第4张图片

你可能感兴趣的:(Springboot,spring,boot,java,junit)