91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot怎么進行整合Druid數據源

發布時間:2022-09-15 17:31:01 來源:億速云 閱讀:165 作者:iii 欄目:開發技術

今天小編給大家分享一下SpringBoot怎么進行整合Druid數據源的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

1.自定義方式

使用自定義方式整合Druid

SpringBoot怎么進行整合Druid數據源

1.添加依賴

在pom.xml添加相關依賴

  <!--數據庫相關-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>
    </dependencies>

2.編寫配置

application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

配置Druid的監控頁功能:

Druid內置提供了一個StatViewServlet用于展示Druid的統計信息。官方文檔 - 配置StatViewServlet

這個StatViewServlet的用途包括:

  • 提供監控信息展示的html頁面

  • 提供監控信息的JSON API

Druid內置提供一個StatFilter,用于統計監控信息。官方文檔 - 配置_StatFilter

WebStatFilter用于采集web-jdbc關聯監控的數據,如SQL監控、URI監控。官方文檔 - 配置WebStatFilter

Druid提供了WallFilter,它是基于SQL語義分析來實現防御SQL注入攻擊的。官方文檔 - 配置 wallfilter

DataSourceConfig

package com.limi.springboottest2.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties("spring.datasource")//復用配置文件的數據源配置, 這樣就不用寫代碼一個一個set了
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        //加入監控和防火墻功能功能
        druidDataSource.setFilters("stat,wall");
        return druidDataSource;
    }
    /**
     * 配置 druid的監控頁功能
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> registrationBean =
                new ServletRegistrationBean<>(statViewServlet, "/druid/*");
        //監控頁賬號密碼:
        registrationBean.addInitParameter("loginUsername","admin");
        registrationBean.addInitParameter("loginPassword","123456");
        return registrationBean;
    }
    /**
     * WebStatFilter 用于采集web-jdbc關聯監控的數據。
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

3.測試

HelloController

package com.limi.springboottest2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @ResponseBody
    @GetMapping("/sql")
    public String test1(){
        String sql = "select count(*) from t_user";
        Long i = jdbcTemplate.queryForObject(sql, Long.class);
        return i.toString();
    }
}

多次點擊刷新進行訪問

SpringBoot怎么進行整合Druid數據源

登錄druid后臺查看監控信息

賬號admin, 密碼123456

SpringBoot怎么進行整合Druid數據源

SpringBoot怎么進行整合Druid數據源

SpringBoot怎么進行整合Druid數據源

2.starter方式(推薦)

使用場景啟動器整合druid, 效果和自定義方式一樣, 但更加方便

1.添加依賴

在pom.xml添加相關依賴

        <!--數據庫相關-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>

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 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.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.limi</groupId>
    <artifactId>springboot-test2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-test2</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.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>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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--數據庫相關-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </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>
                    </excludes>
                </configuration>
            </plugin>
            <!-- 下面插件作用是工程打包時,不將spring-boot-configuration-processor打進包內,讓其只在編碼的時候有用 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.編寫配置

application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#監控SpringBean
spring.datasource.druid.aop-patterns=com.limi.springboottest2.*  
# 底層開啟功能,stat(sql監控),wall(防火墻)
spring.datasource.druid.filters=stat,wall

# 配置監控頁功能
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.reset-enable=false

# 監控web
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

# 對上面filters里面的stat的詳細配置
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.config.drop-table-allow=false

3.測試

HelloController

package com.limi.springboottest2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @ResponseBody
    @GetMapping("/sql")
    public String test1(){
        String sql = "select count(*) from t_user";
        Long i = jdbcTemplate.queryForObject(sql, Long.class);
        return i.toString();
    }
}

多次點擊刷新進行訪問

SpringBoot怎么進行整合Druid數據源

登錄druid后臺查看監控信息

賬號admin, 密碼123456

SpringBoot怎么進行整合Druid數據源

SpringBoot怎么進行整合Druid數據源

SpringBoot怎么進行整合Druid數據源

以上就是“SpringBoot怎么進行整合Druid數據源”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新乡市| 武宣县| 东方市| 赤峰市| 铜鼓县| 田东县| 余姚市| 博客| 贵州省| 义马市| 杭锦后旗| 武定县| 武穴市| 夏河县| 花垣县| 剑川县| 齐齐哈尔市| 杂多县| 石楼县| 项城市| 长宁县| 阿克苏市| 柘城县| 镇雄县| 定结县| 凤庆县| 甘肃省| 太原市| 育儿| 无极县| 色达县| 茂名市| 洪泽县| 邳州市| 宁河县| 台中县| 封丘县| 新民市| 湾仔区| 莒南县| 门源|