spring boot連線資料庫
阿新 • • 發佈:2018-12-22
springboot連線資料庫:
1.mybatis (註解版:一個sql語句只能查詢一張表) 1.1 1 select * from t_user 2 select * from t_dept 通過java程式碼進行關聯組成一個集合返回頁面 2.1 註解版完成 單表增刪改查 2.mysql 3.datasource:username password url driver 4.druid監控資料庫 4.1 匯入druid pom檔案 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.3</version> </dependency> 4.2 配置資料庫監控日誌記錄,將spring boot中log4j排除,並引入slf4j日誌記錄 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> 4.3 配置連線池屬性 #配置資料來源 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/1806a?useUnicode=true&characterEncoding=utf8&useSSL=true&zeroDateTimeBehavior=convertToNull spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 下面為連線池的補充設定,應用到上面所有資料來源中 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置獲取連線等待超時的時間 spring.datasource.maxWait=60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一個連線在池中最小生存的時間,單位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆 spring.datasource.filters=stat,wall,log4j spring.datasource.logSlowSql=true 4.4 匯入druid監控類DruidConfiguration,放在config包下 4.5 啟動專案訪問http://localhost:8080/druid/login.html 4.6druid常用屬性解釋:https://blog.csdn.net/deng11408205/article/details/79629183 4.7 https://www.cnblogs.com/liuchuanfeng/p/7002046.html https://blog.csdn.net/moshowgame/article/details/80304198 5.頁面thymeleaf頁面模板引擎