最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合JDBC、Druid數(shù)據(jù)源的示例代碼

 更新時間:2021年05月12日 08:24:10   作者:人無名,則可專心練劍  
這篇文章主要介紹了SpringBoot整合JDBC、Druid數(shù)據(jù)源,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.SpringBoot整合JDBCTemplate

1.1.導(dǎo)入jdbc相關(guān)依賴包

主要的依賴包:

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<!--實(shí)現(xiàn)web頁面接口調(diào)用-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

1.2.yaml配置數(shù)據(jù)源

application.yml用于連接jdbc數(shù)據(jù)庫操作數(shù)據(jù)源配置,這里是最簡化的配置:

spring:
  datasource:
    username: root
    password: admin
    #假如時區(qū)報錯,增加時區(qū)配置serverTimezone=UTC,以及編碼配置
    url: jdbc:mysql://localhost:3306/mybatis02_0322?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

實(shí)際開發(fā)過程中基本上會與Druid、C3P0整合,下面也給出了整合Druid數(shù)據(jù)源相關(guān)的配置,所以這里一并放上完整的application.yml配置:

spring:
  datasource:
    username: root
    password: admin
    #假如時區(qū)報錯,增加時區(qū)配置serverTimezone=UTC
    url: jdbc:mysql://localhost:3306/mybatis02_0322?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #springboot 默認(rèn)是不注入這些屬性值的,需要自己綁定
    #druid 數(shù)據(jù)源專有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置監(jiān)控統(tǒng)計攔截的filters,stat:監(jiān)控統(tǒng)計、log4j:日志記錄、wall:防止sql注入
    #如果允許時報錯:java.lang.ClassNotFoundException:org.apache.log4j.Priority
    #則導(dǎo)入 log4j 依賴即可,maven地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

最后測試一下數(shù)據(jù)庫連接訪問是否成功:

@SpringBootTest
class SpringbootDataApplicationTests {
    @Autowired
    private DataSource dataSource;

    @Test
    void testConnection() throws SQLException {
        System.out.println(dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
}

打印成功,顯示獲取到了數(shù)據(jù)源信息:

1.3.界面訪問接口測試

@RestController
public class JDBCController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/queryList")
    public List<Map<String, Object>> query() {
        String sql = "select * from user";
        List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql);
        return queryForList;
    }

    @RequestMapping("/addUser")
    public String AddUser(){
        String sql = "insert into mybatis02_0322.user(id, username, password) values(4, '李磊', '654321')";
        int update = jdbcTemplate.update(sql);
        return "AddUser Ok";
    }

    @RequestMapping("/update/{id}")
    public String update(@PathVariable("id") Integer id){
        String sql = "update mybatis02_0322.user set username = ?, password = ? where id = " + id;
        Object[] objects = new Object[2];
        objects[0] = "測試哈哈哈111";
        objects[1] = "32321";
        int update = jdbcTemplate.update(sql, objects);
        return "updateUser Ok";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        String sql = "delete from mybatis02_0322.user where id = " + id;
        int update = jdbcTemplate.update(sql);
        return "delete Ok";
    }
}

這里對應(yīng)接口請求頁面進(jìn)行請求測試即可,后臺數(shù)據(jù)庫層面進(jìn)行驗(yàn)證,比較簡單,這里就不一一細(xì)說,對應(yīng)可以去看我的源碼。

2.SpringBoot整合DruidDataSource

2.1.Druid簡介

Druid是阿里巴巴開源平臺上一個數(shù)據(jù)庫連接池實(shí)現(xiàn),它結(jié)合了C3P0、DBCP、PROXOOL等DB池的優(yōu)秀實(shí)踐,同時加入了日志監(jiān)控。

Druid可以很好地監(jiān)控DB池連接和Sql的執(zhí)行情況,是天生針對監(jiān)控的DB連接池。

SpringBoot2.0以上默認(rèn)使用Hikari數(shù)據(jù)源,可以說HiKari和Druid都是當(dāng)前Java Web上開源的優(yōu)秀數(shù)據(jù)源。

2.2.導(dǎo)入Druid相關(guān)依賴

對應(yīng)pom.xml文件:

<!--整合alibaba druid數(shù)據(jù)源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.3</version>
</dependency>
<!--導(dǎo)入log4j日志包-->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

2.3.配置Druid并使用監(jiān)控頁面

①編寫DruidConfig類,啟用后臺監(jiān)控功能Bean以及過濾器Bean:

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后臺監(jiān)控功能
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //后臺需要有人登陸,賬號密碼配置
        HashMap<String, String> initParameters = new HashMap<>();
        initParameters.put("loginUsername", "admin");  //登陸key,是固定的  loginUsername loginPassword
        initParameters.put("loginPassword", "123456");
        //允許誰可以訪問
        initParameters.put("allow", "");
        //禁止誰可以訪問   initParameters.put("fengye", "192.168.1.10");
        bean.setInitParameters(initParameters);     //設(shè)置初始化參數(shù)

        return bean;
    }

    //過濾器
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());

        HashMap<String, String> initParameters = new HashMap<>();
        //這些不進(jìn)行統(tǒng)計
        initParameters.put("exclusions", "*.js,*.css,/druid/*");
        bean.setInitParameters(initParameters);

        return bean;
    }
}

②啟動頁面訪問Druid并測試請求訪問sql:

本博客寫作參考文檔相關(guān):

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter

https://www.yuque.com/atguigu/springboot/aob431#wtNk1

Spring Boot 2 學(xué)習(xí)筆記(上):https://blog.csdn.net/u011863024/article/details/113667634
Spring Boot 2 學(xué)習(xí)筆記(下):
https://blog.csdn.net/u011863024/article/details/113667946

示例代碼已上傳至Github地址:

https://github.com/devyf/SpringBoot_Study

到此這篇關(guān)于SpringBoot整合JDBC、Druid數(shù)據(jù)源的文章就介紹到這了,更多相關(guān)SpringBoot整合JDBC、Druid數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 代碼中預(yù)防空指針異常的處理辦法

    java 代碼中預(yù)防空指針異常的處理辦法

    個人在做項(xiàng)目時,對NullPointerException的幾點(diǎn)總結(jié),請網(wǎng)友拍磚?。?!多多提意見,
    2013-03-03
  • 詳細(xì)解讀AbstractStringBuilder類源碼

    詳細(xì)解讀AbstractStringBuilder類源碼

    這篇文章主要介紹了詳細(xì)解讀AbstractStringBuilder類源碼,具有一定參考價值,需要的朋友可以了解下。
    2017-12-12
  • 一文帶你秒懂Java為什么只有值傳遞

    一文帶你秒懂Java為什么只有值傳遞

    值傳遞是指在調(diào)用函數(shù)時將實(shí)際參數(shù)復(fù)制一份傳遞到函數(shù)中,引用傳遞是指在調(diào)用函數(shù)時將實(shí)際參數(shù)的引用直接傳遞到函數(shù)中,本文將詳細(xì)介紹Java中值傳遞的相關(guān)知識,感興趣的可以了解下
    2024-11-11
  • IDEA版最新MyBatis程序配置教程詳解

    IDEA版最新MyBatis程序配置教程詳解

    這篇文章主要介紹了IDEA版最新MyBatis程序配置教程詳解,需要的朋友可以參考下
    2020-07-07
  • Spring5使用JSR 330標(biāo)準(zhǔn)注解的方法

    Spring5使用JSR 330標(biāo)準(zhǔn)注解的方法

    從Spring3.0之后,除了Spring自帶的注解,我們也可以使用JSR330的標(biāo)準(zhǔn)注解,本文主要介紹了Spring5使用JSR 330標(biāo)準(zhǔn)注解,感興趣的可以了解一下
    2021-09-09
  • MyBatis-Plus忽略多租戶隔離自定義注解

    MyBatis-Plus忽略多租戶隔離自定義注解

    本文主要介紹了MyBatis-Plus忽略多租戶隔離自定義注解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • Java創(chuàng)建、識別條形碼和二維碼方法示例

    Java創(chuàng)建、識別條形碼和二維碼方法示例

    這篇文章主要給大家介紹了關(guān)于Java創(chuàng)建、識別條形碼和二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java Arrays.fill()的具體使用

    Java Arrays.fill()的具體使用

    本文主要介紹了Java Arrays.fill()的具體使用,更好地理解Arrays.fill()方法的用法以及在實(shí)際應(yīng)用中如何使用它,感興趣的可以了解一下
    2023-09-09
  • maven報錯:Failed to execute goal on project問題及解決

    maven報錯:Failed to execute goal on p

    這篇文章主要介紹了maven報錯:Failed to execute goal on project問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • SpringBoot3整合SpringCloud啟動后nacos報錯獲取不到配置、無法注冊服務(wù)的解決方案

    SpringBoot3整合SpringCloud啟動后nacos報錯獲取不到配置、無法注冊服務(wù)的解決方案

    文章介紹了如何使用Spring Boot 3.3.4和Spring Cloud 2023.0.3搭建微服務(wù)項(xiàng)目,并解決與Nacos服務(wù)注冊發(fā)現(xiàn)和配置中心的集成問題,主要解決了依賴版本不兼容、配置文件導(dǎo)入問題及服務(wù)注冊失敗等問題,感興趣的朋友跟隨小編一起看看吧
    2025-02-02

最新評論

凉城县| 米易县| 佛坪县| 定安县| 常熟市| 盐津县| 贡山| 宝山区| 乐陵市| 博爱县| 济源市| 仪陇县| 从江县| 洛川县| 乐山市| 洪湖市| 龙南县| 即墨市| 建宁县| 南和县| 西昌市| 横峰县| 宣化县| 惠州市| 营口市| 浦县| 望奎县| 区。| 元阳县| 牡丹江市| 四平市| 黔西县| 奈曼旗| 全南县| 雅安市| 凤山市| 东辽县| 长葛市| 峨眉山市| 沁水县| 德庆县|