Druid เป็นพูลการเชื่อมต่อฐานข้อมูลโอเพนซอร์สจากอาลีบาบาซึ่งให้ฟังก์ชั่นการตรวจสอบที่ยอดเยี่ยมสำหรับการดำเนินการฐานข้อมูล บทความนี้จะอธิบายวิธีการรวม Druid เข้ากับโครงการ Springboot
บทความนี้ได้รับการพัฒนาภายใต้โครงการที่ใช้ JPA ขั้นแรกให้เพิ่มการพึ่งพา DRUID เพิ่มเติมในไฟล์ POM ไฟล์ POM มีดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <ArtIfactId> springboot_druid </artifactid> <persion> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> springboot_druid </name> <secutional> springboot_druid </คำอธิบาย> <ArtIfactId> Spring-Boot-Starter-Parent </ArtifactId> <Sersion> 1.5.12.release </เวอร์ชัน> <inelypath/> <!-ผู้ปกครองค้นหาจากที่เก็บ-> </parent> <properties> <project.build.sourceencoding> <Project.Reporting.UutputEncoding> UTF-8 </project.Reporting.UutputUntencoding> <Java.Version> 1.8 </Java.Version> </premerties> <การพึ่งพา> <การพึ่งพา> </permentency> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Web </ArtifactId> </dependency> <การพึ่งพา </perctency> <predency> <roupId> mysql </groupId> <ratifactid> mysql-connector-java </artifactid> <pope> รันไทม์ </cope> </perentency> <Scope> ทดสอบ </cope> </perctency> <การพึ่งพา> <roupId> com.alibaba </groupId> <ratifactId> druid </artifactid> <cersion> 1.0.28 </เวอร์ชัน> </การพึ่งพา> </การพึ่งพา <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
ครึ่งแรกของแอปพลิเคชัน properties และ JPA แบบบูรณาการไม่ได้เปลี่ยนแปลงเลย การกำหนดค่า DRUID บางส่วนได้รับการเพิ่มด้านล่าง หากคุณไม่เข้าใจการกำหนดค่าของ Druid คุณสามารถตรวจสอบออนไลน์ได้ (ฉันคิดว่าบทความนี้เขียนได้ดีพอร์ทัล)
#port number server.port = 8888 ## ตรวจสอบความถูกต้องเมื่อมีการโหลด hibernate ตรวจสอบการสร้างโครงสร้างตารางฐานข้อมูล ## สร้างแต่ละครั้งที่ไฮเบอร์เนตถูกโหลดขึ้นใหม่สร้างโครงสร้างตารางฐานข้อมูลซึ่งเป็นสาเหตุของการสูญเสียข้อมูลตารางฐานข้อมูล ## create-drop ที่สร้างขึ้นเมื่อมีการโหลด hibernate ออกไปคือการลบโครงสร้างตาราง ## อัปเดตการโหลด hibernate โดยอัตโนมัติอัปเดตโครงสร้างฐานข้อมูลโดยอัตโนมัติ ## ตรวจสอบการตรวจสอบโครงสร้างของตารางเมื่อเริ่มต้นและจะไม่สร้างตาราง ## ไม่มีอะไรเลย sqlspring.jpa.show-sql = true ## การกำหนดค่าฐานข้อมูล ## ที่อยู่ฐานข้อมูล spring.datasource.url = jdbc: mysql: // localhost: 3306/ทดสอบ? Spring.datasource.password = root ## ไดรเวอร์ฐานข้อมูล Spring.datasource.driver-class-name = com.mysql.jdbc.driver#นี่คือ#ที่แตกต่างกันถ้าคุณใช้ druid คุณต้องกำหนดค่าคุณสมบัติอีกหนึ่ง Spring.datasource.typespring.datasource.type ของพูลการเชื่อมต่อ # เริ่มต้นขนาดต่ำสุด, สปริงสูงสุด dataSource.initialSize = 5 spring.datasource.minidle = 5 spring.datasource.maxactive = 20 # กำหนดเวลาเพื่อให้การเชื่อมต่อหมดเวลา Spring.datasource.timebetweenevictionrunsmillis = 60000 # กำหนดเวลาขั้นต่ำสำหรับการเชื่อมต่อเพื่อความอยู่รอดในสระว่ายน้ำหน่วยเป็นมิลลิวินาทีสปริง. Spring.datasource.testonborrow = False Spring.datasource.testonReturn = false # เปิด pscache และระบุขนาดของ pscache ในการเชื่อมต่อแต่ละ Spring.datasource.poolpreparedStatements = True Spring.Datasource.MaxPoolPropreadStementPerConnectiones หลังจากลบออกแล้วอินเตอร์เฟสการตรวจสอบ SQL ไม่สามารถนับได้ 'Wall' ใช้สำหรับ Firewall Spring.datasource.filters = Stat, Wall, Log4j# ใช้คุณสมบัติ ConnectProperties เพื่อเปิดฟังก์ชั่น MergesQl; บันทึก SQL ช้า
จากนั้นเพิ่ม druidConfig ในโครงการและอธิบายสั้น ๆ ว่าไฟล์กำหนดค่านี้ส่วนใหญ่โหลดการกำหนดค่าของ application.properties และรหัสมีดังนี้:
แพ็คเกจ com.dalaoyang.config; นำเข้า java.sql.sqlexception; นำเข้า javax.sql.datasource; นำเข้า org.apache.log4j.logger นำเข้า org.springframework.beans.factory.annotation.value; org.springframework.context.annotation.configuration; นำเข้า org.springframework.context.annotation.primary; นำเข้า com.alibaba.druid.pool.druiddataSource;/** @author Dalaoyang * @description @Email [email protected] * @date 2018/4/12 */ @configurationPublic คลาส druidconfig {logger ส่วนตัว logger = logger.getLogger (this.getClass ()); @Value ("$ {Spring.datasource.url}") สตริงส่วนตัว dburl; @Value ("$ {Spring.datasource.username}") ชื่อผู้ใช้สตริงส่วนตัว; @Value ("$ {Spring.Datasource.Password}") รหัสผ่านสตริงส่วนตัว; @Value ("$ {Spring.Datasource.Driver-Class-Name}") สตริงส่วนตัว DriverClassName; @Value ("$ {Spring.Datasource.InitialSize}") ค่าเริ่มต้น INT ส่วนตัว; @Value ("$ {Spring.datasource.minidle}") private int minidle; @Value ("$ {Spring.datasource.maxactive}") int maxactive ส่วนตัว; @Value ("$ {Spring.datasource.maxwait}") int maxwait ส่วนตัว; @Value ("$ {Spring.datasource.timeBetweenevictionRunsmillis}") INt TimebetweeneVictionRunsmillis ส่วนตัว; @Value ("$ {Spring.datasource.MinevictableidletItimeMillis}") int private int minevictableidletimeMillis ส่วนตัว; @Value ("$ {Spring.Datasource.ValidationQuery}") Private String ValidationQuery; @Value ("$ {Spring.datasource.testhialidle}") บูลีนส่วนตัวทดสอบในขณะเดียวกัน; @Value ("$ {Spring.datasource.testonborrow}") Boolean Private Boolean Testonborrow; @Value ("$ {Spring.datasource.testonReturn}") บูลีนส่วนตัว testonreturn; @Value ("$ {Spring.datasource.poolpreparedStatements}") บูลีนส่วนตัว poolean poolpreparedStatements; @Value ("$ {Spring.Datasource.MaxPoolPreparedStatementPerConnesionesize}") int maxpoolpreparedStatementPerconnectionize ส่วนตัว @Value ("$ {Spring.Datasource.Filters}") ตัวกรองสตริงส่วนตัว; @Value ("{spring.datasource.connectionProperties}") สตริงการเชื่อมต่อส่วนตัว @Bean @Primary // แหล่งข้อมูลหลัก DataSource Public DataSource () {dataSource DRUIDDATASOURCE = ใหม่ DRUIDDATASOURCE (); dataSource.seturl (this.dburl); DataSource.SetUserName (ชื่อผู้ใช้); DataSource.SetPassword (รหัสผ่าน); DataSource.SetDriverClassName (DriverClassName); // dataSource.SetInitialSize (ค่าเริ่มต้น); DataSource.setMinidle (minidle); DataSource.SetMaxactive (maxactive); DataSource.setMaxWait (MaxWait); DataSource.SettimeBetweenevictionRunsmillis (TimebetweenevictionRunsmillis); DataSource.SetMineVictableIdletImeMillis (minevictableidletimeMillis); DataSource.SetValidationQuery (ValidationQuery); dataSource.settesthavyidle (testharhialidle); DataSource.SettestonBorrow (TestonBorrow); DataSource.SettestonReturn (TestOnReturn); DataSource.SetPoolPreparedStatements (PoolPreparedStatements); DataSource.SetMaxPoolPreparedStatementPerConnectionize (MaxPoolPreparedStatementPerConnectionize); ลอง {dataSource.setFilters (ตัวกรอง); } catch (sqlexception e) {logger.error ("ข้อยกเว้นการกำหนดค่า druid", e); } dataSource.SetConnectionProperties (ConnectionProperties); ส่งคืนแหล่งข้อมูล; -
จากนั้นสร้าง druidfilter รหัสมีดังนี้:
แพ็คเกจ com.dalaoyang.filter; นำเข้า Javax.servlet.annotation.webfilter; นำเข้า Javax.servlet.annotation.webinitparam; นำเข้า com.alibaba.druid.support.http.webstatfilter; com.dalaoyang.filter * @Email [email protected] * @date 2018/4/12 */ @webfilter (filtername = "druidwebstatfilter", urlpatterns = "/ *", initparams = { @webinitparam (name = "การยกเว้น", value = "*. js,*. gif,*. jpg,*. bmp,*. png,*. css,*. ico,/druid/*") // ละเว้นทรัพยากร})
สร้าง druidservlet ใหม่เพิ่มคำอธิบายประกอบลงในคลาส @webservlet ซึ่งกำหนดค่ารหัสผ่านบัญชีสำหรับการเข้าสู่ระบบไปยังหน้าการตรวจสอบ Druid, การกำหนดค่า Whitelist และ Blacklist, รหัสมีดังนี้:
แพ็คเกจ com.dalaoyang.servlet; นำเข้า Javax.servlet.annotation.webinitparam นำเข้า Javax.servlet.annotation.webservlet; นำเข้า com.alibaba.druid.support.http.statviewservlet; com.dalaoyang.servlet * @email [email protected] * @date 2018/4/12 */ @webservlet (urlpatterns = "/druid/ *", initarams = {@webinitparam (ชื่อ = "อนุญาต", ค่า = "" ") @webinitparam (name = "deny", value = ""), // ip blacklist (deny มีความสำคัญกว่าอนุญาต) @webinitparam (name = "loginusername", value = "admin"), // เข้าสู่ระบบการจัดการ druid @webinitparam Druidservlet ขยาย StatViewServlet {}
จากนั้นเพิ่มคำอธิบายประกอบ @ServletComponentScan ลงในคลาสเริ่มต้นและปล่อยให้โครงการสแกนไปยัง servlet รหัสมีดังนี้:
แพ็คเกจ com.dalaoyang; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.boot.web.servlet.servletComponentsCanan คำอธิบายประกอบมิฉะนั้นจะไม่สามารถสแกนไปยัง servlet@servletComponentscan คลาสสาธารณะ Springbootdruidapplication {โมฆะสาธารณะคงที่หลัก (สตริง [] args) {springapplication.run (springbootdruidapplication.class, args); -ส่วนที่เหลือเป็นเอนทิตีเดียวกัน (คลาสเอนทิตี), ที่เก็บ (เลเยอร์การทำงานของข้อมูล), คอนโทรลเลอร์ (คอนโทรลเลอร์ที่ใช้สำหรับการทดสอบ) เป็น JPA แบบรวมซึ่งแสดงรหัสโดยตรง
เมือง
แพ็คเกจ com.dalaoyang.entity; นำเข้า Javax.persistence. *;/** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.entity * @email [email protected] @GeneratedValue (กลยุทธ์ = GenerationType.Auto) INT CITYID ส่วนตัว; สตริงส่วนตัว CityName; สตริงส่วนตัว CityIntroduce; เมืองสาธารณะ (int cityid, String CityName, String CityIntroduce) {this.cityid = cityid; this.cityName = CityName; this.cityIntroduce = CityIntroduce; } เมืองสาธารณะ (String CityName, String CityIntroduce) {this.cityName = CityName; this.cityIntroduce = CityIntroduce; } Public City () {} public int getCityId () {return cityid; } โมฆะสาธารณะ setCityID (int cityid) {this.cityid = cityid; } สตริงสาธารณะ getCityName () {return CityName; } โมฆะสาธารณะ setCityName (String CityName) {this.cityName = CityName; } สตริงสาธารณะ getCityIntroduce () {return cityIntroduce; } โมฆะสาธารณะ setCityIntroduce (String CityIntroduce) {this.cityIntroduce = CityIntroduce; -
การเก็บรักษาในเมือง
แพ็คเกจ com.dalaoyang.repository; นำเข้า com.dalaoyang.entity.city; นำเข้า org.springframework.data.jpa.repository.jparepository;/** * @author Dalaoyang * @description [email protected] * @date 2018/4/7 */ส่วนต่อประสานสาธารณะ CityRepository ขยาย JParePository <City, Integer> {}
CityController
แพ็คเกจ com.dalaoyang.controller; นำเข้า com.dalaoyang.entity.city; นำเข้า com.dalaoyang.repository.cityrepository; นำเข้า org.springframework.beans.factory.annotation.autowired; org.springframework.web.bind.annotation.restcontroller;/** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.controller * @email 397600342@ {@autowired CityRepository CityRepository; // http: // localhost: 8888/savecity? cityname = ปักกิ่ง & cityintroduce = เมืองหลวงของจีน @getMapping (ค่า = "Savecity") Savecity สาธารณะ (String CityName, String CityIntroduce) CityRepository.Save (เมือง); กลับ "ความสำเร็จ"; } // http: // localhost: 8888/deleteCity? cityid = 2 @getMapping (value = "deleteCity") การลบสตริงสาธารณะ (int cityid) {cityrepository.delete (cityid); กลับ "ความสำเร็จ"; } // http: // localhost: 8888/updateCity? cityid = 3 & cityname = Shenyang & CityIntroduce = Liaoning Capital @getMapping (ค่า = "updateCity") การอัปเดตสตริงสาธารณะ CityRepository.Save (เมือง); กลับ "ความสำเร็จ"; } // http: // localhost: 8888/getCityById? cityid = 3 @getMapping (ค่า = "getCityById") เมืองสาธารณะ getCityByid (int CityID) {เมือง = เมือง เมืองกลับ; -จากนั้นเริ่มโครงการและคุณจะเห็นว่าคอนโซลได้สร้างตารางเมือง
จากนั้นเยี่ยมชม http: // localhost: 8888/druid และคุณสามารถดูรูปต่อไปนี้:
ป้อนผู้ดูแลระบบรหัสผ่านบัญชีผู้ดูแลระบบดังที่แสดงในรูปต่อไปนี้
จากนั้นในเวลานี้เราสามารถเยี่ยมชม http: // localhost: 8888/savecity? cityname = Beijing & CityIntroduce = เมืองหลวงของจีน
จากนั้นคลิกที่การตรวจสอบ SQL ด้านบนดังแสดงในรูปด้านล่าง
จากรูปด้านบนคุณจะเห็นว่า SQL ที่เริ่มต้นตารางการสร้างโครงการเพิ่งถูกดำเนินการ การบูรณาการเสร็จสมบูรณ์ที่นี่
ดาวน์โหลดซอร์สโค้ด: https://gitee.com/dalaoyang/springboot_learn
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น