日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

2、SpringBoot整合JDBC

發布時間:2025/3/19 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2、SpringBoot整合JDBC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 1、配置pom.xml
      • 2、配置application.yml
      • 3、建表sql
      • 4、HelloController
      • 5、高級配置,使用druid數據源
        • 1)引入druid對應的pom.xml
        • 2)配置application.yml
        • 3)配置DruidConfig
        • 4)訪問druid后臺

1、配置pom.xml

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>6.0.6</version> </dependency>

2、配置application.yml

spring:datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTCdriver-class-name: com.mysql.jdbc.Driver

3、建表sql

SET FOREIGN_KEY_CHECKS=0;-- ---------------------------- -- Table structure for department -- ---------------------------- DROP TABLE IF EXISTS `department`; CREATE TABLE `department` (`id` int(11) NOT NULL AUTO_INCREMENT,`departmentName` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

4、HelloController

package com.mi.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; import org.springframework.web.bind.annotation.RestController;import java.util.List; import java.util.Map;/*** Created by chengwen on 2019/7/3.*/ @RestController public class HelloController {@AutowiredJdbcTemplate jdbcTemplate;@GetMappingpublic Map<String,Object> map(){List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from department");return list.get(0);} }

5、高級配置,使用druid數據源

1)引入druid對應的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.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.mi</groupId><artifactId>spring-boot-jdbc</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-boot-jdbc</name><description>spring-boot-jdbc 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-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>6.0.6</version></dependency><!--引入自定義數據源--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.18</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2)配置application.yml

spring:datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTCdriver-class-name: com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceinitialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true # 配置監控統計攔截的filters,去掉后監控界面sql無法統計,'wall'用于防火墻filters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500# schema:# - classpath:department.sql

3)配置DruidConfig

package com.mi.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.util.Arrays; import java.util.HashMap; import java.util.Map;/*** Created by chengwen on 2019/7/3.*/ @Configuration public class DruidConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource druid(){return new DruidDataSource();}//配置Druid的監控//1、配置一個管理后臺的servlet@Beanpublic ServletRegistrationBean statViewServlet(){ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");Map<String,String> initParms = new HashMap<>();initParms.put("loginUsername","admin");initParms.put("loginPassword","123456");initParms.put("allow",""); //默認是允許所有訪問initParms.put("deny","192.168.15.21"); //不允許訪問bean.setInitParameters(initParms);return bean;}//2、配置一個監控的filter@Beanpublic FilterRegistrationBean webStatFilter(){FilterRegistrationBean bean = new FilterRegistrationBean();bean.setFilter(new WebStatFilter());Map<String,String> initParms = new HashMap<>();initParms.put("exclusions","*.js,*.css,/druid/*");bean.setInitParameters(initParms);bean.setUrlPatterns(Arrays.asList("/*"));return bean;} }

4)訪問druid后臺

http://localhost:8080/druid/

總結

以上是生活随笔為你收集整理的2、SpringBoot整合JDBC的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。