druid的springboot

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.4</version>
</dependency>

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

加入上面的两个依赖

第二步,书写application.yml,这个是错误的配置,下面另有一份正确的配置

参考下面还有另一个正确配置
spring: datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid: 
driver-class-name: com.mysql.cj.jdbc.Driver
#url: jdbc.mysql:///zhouyi2
url: jdbc:mysql://127.0.0.1:3306/zhouyi2?useUnicode=true
&characterEncoding=utf8&userSSL=true&serverTimezone=Asia/Shanghai #输错了冒号,为分号
#url: jdbc.mysql:///zhouyi2
username: root
password: xxxxx
initial-size: 10
min-idle: 10
max-active: 50
max-wait: 15000
#监控配置
stat-view-servlet:
allow: 112.38.46.177
deny:
url-pattern: /druid/*
enabled: true
login-username: system
login-password: 123456
server: port: 80

不知为何,设置初始化连接数时总是报错。只有设置为0,才成功。目前的mysql的版本为5.0,明天升级到mysql5.7,试试。

已经解决,url的冒号写成了分号

另外,在数据源的autowire时,必须和yml文件的一致,否者报url不存在的错误

以下是正确的yml文件

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/zhouyi2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
      username: root
      password: xxxxxxx
      initial-size: 1
     
      min-idle: 2
      max-active: 50
      max-wait: 15000
      enable: true
      test-on-borrow: true
      test-on-return: true
      test-while-idle: true
      #validation-query: select 1
     # db-type: mysql
      #async-init: true
      #db-type: mysql
      #type:   com.alibaba.druid.pool.DruidDataSource  
      #监控配置
      filters: stat 
      #上面的filter是查看sql监控的。
      stat-view-servlet:
        allow: 112.38.46.177
        deny: 
        url-pattern: /druid/*
        enabled: true
        login-username: system
        login-password: 123456
      
#      init-variants: true
#      async-init: true
server:
  port: 80
        

 测试方法

@Configuration
public class DataSourceConfig {

    @Bean(destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource dataSource() {
        return new DruidDataSource();
    }
    
    
    
}
@RestController
public class TestDataSource {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/hello")
    public List<Map<String, Object>> getUser() {
        String sql = "select * from tbl_admin";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

}
<?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 https://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.3.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.taotao</groupId>
    <artifactId>druid</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>druid</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> 
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.4</version>
</dependency>

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

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

<!--servlet依赖的jar包-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--jsp依赖的jar包-->
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        
        
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency> -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

以上代码测试通过

原文地址:https://www.cnblogs.com/sdgtxuyong/p/14405693.html