Druid هو تجمع اتصال قاعدة بيانات مفتوح المصدر من Alibaba ، والذي يوفر وظائف مراقبة ممتازة لعمليات قاعدة البيانات. ستشرح هذه المقالة كيفية دمج Druid في مشروع Springboot.
تم تطوير هذه المقالة في إطار مشروع قائم على JPA. أولاً ، تتم إضافة تبعيات إضافية إلى ملف POM. ملف POM كما يلي:
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 <soph> 0.0.1-snapshot </version> <packaging> جرة </packaging> <ame> springboot_druid </name> <specord> springboot_druid </description> <parent> <roupiD> org.springframework.boot </rougiDid> <Thotifactid> spring-boot-parent </splistid> <REPERANTERPARPHAPT/> <!-LANEUP PARTER من المستودع-> </inpart> <ferties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <Project.reporting.outputing> uTF-8 </proport.reporting.outppouting> </properties> <ependencies> <reperence> <roupiD> org.springframework.boot </rougeid> <unsifactid> spring-boot-starter-data-jpa </ropringid> </spring-boency> <sperence> </reperency> <reperency> <roupiD> org.springframework.boot </rougeid> <StifactId> spring-boot-devtools </stifactid> <scope> وقت التشغيل </scope> </redefency> <sperency> <scope> وقت التشغيل </scope> </sependency> <redency> <roupiD> org.springframework.boot </rougeid> <StifactId> spring-boot-starter-test </stifactid> <scope> اختبار </scope> <soph> 1.0.28 </version> </sependency> </sependencies> <build> <clupins> <roupiD> org.springframework.boot </rougeid> <StifactId> spring-boot-maven-plugin </stifactid> </sultiin> </plugins> </build> </project
لم يتغير النصف الأول من التطبيق. تمت إضافة بعض تكوينات Druid أدناه. إذا كنت لا تفهم تكوين Druid ، فيمكنك التحقق من ذلك عبر الإنترنت. (أعتقد أن هذا المقال مكتوب بشكل جيد ، بوابة)
#Port Number Server.port = 8888 ## التحقق من صحة عند تحميل السبات ، تحقق من إنشاء بنية جدول قاعدة البيانات ## إنشاء كل مرة يتم فيها تحميل الإسبات ، وإعادة إنشاء بنية جدول قاعدة البيانات ، وهو سبب فقدان بيانات جدول قاعدة البيانات. ## إنشاء drop الذي تم إنشاؤه عند تحميل kibernate ، والخروج هو حذف بنية الجدول ## التحديث التحديث kibernate تحديث تلقائيا بنية قاعدة البيانات ## التحقق من صحة التحقق sqlspring.jpa.show-sql = true ## تكوين قاعدة البيانات ## عنوان قاعدة البيانات العنوان spring.datasource.url = jdbc: mysql: // localhost: 3306/test؟ directionencoding = utf8 & usessl = false ## database username spring.datasource spring.datasource.password = root ## database driver spring.datasource.driver-class-name = com.mysql.jdbc.driver#هنا مختلف#druid ، فأنت بحاجة إلى تكوين خاصية spring.datasource.typespring.datasource.type = من تجمع الاتصال # الحجم التهيئة ، الحد الأدنى ، الحد الأقصى لـ Spring.Datasource.InitialSize = 5 Spring.Datasource.Minidle = 5 Spring.Datasource.MaxActive = 20 # تكوين الوقت للوقت للحصول على اتصال في مهلة الانتظار. Spring.Datasource.TimeBetweenevictionRunsMillis = 60000 # قم بتكوين الحد الأدنى للوقت للاتصال بالبقاء على قيد الحياة في المجمع spring.datasource.testonborrow = false spring.datasource.testonreturn = false # pscache open وحدد حجم pscache على كل اتصال spring.datasource.poolpreparedstatements = true spring.datasource.maxpoolpreparedstatepresparepreconnectionize = 20 # configuter for statistics. بعد إزالته ، لا يمكن حساب واجهة المراقبة SQL. يتم استخدام "الجدار" في جدار الحماية spring.datasource.filters = stat ، الجدار ، 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 * @dripriven * eMail [email protected] * date 2018/4/12 */ @configurationpublic class druidConfig {private 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}") private int initalsize ؛ value ("$ {spring.datasource.minidle}") private int minidle ؛ Value ("$ {spring.datasource.maxactive}") private int maxaCyt ؛ value ("$ {spring.datasource.maxwait}") private int maxwait ؛ Value ("$ {spring.datasource.TimeBetweenevictionRunsmillis}") private int ttreateDeenevictionRunsmillis ؛ value ("$ {spring.datasource.minevictableDletimEmillis}") private int minevictableDletimemillis ؛ Value ("$ {spring.datasource.validationQuery}") سلسلة خاصة بالسلسلة ؛ Value ("$ {spring.datasource.testwhileIdle}") اختبار منطقي خاص ؛ Value ("$ {spring.datasource.testonborrow}") private boolean testonborrow ؛ Value ("$ {spring.datasource.testonreturn}") private boolean testOnreturn ؛ Value ("$ {spring.datasource.poolpreparedstatements}") pollepreparedStatements الخاصة ؛ Value ("$ {spring.datasource.maxpoolpreparedStatePerConnectionSize}") private int maxpoolpropedstatePerConnectionsize ؛ Value ("$ {spring.datasource.filters}") مرشحات السلسلة الخاصة ؛ value ("{spring.datasource.connectionProperties}") سلسلة connectionProperties الخاصة ؛ bean @primary // مصدر البيانات الرئيسي dataSource dataSource () {druidDataSource dataSource = new DruidDataSource () ؛ datasource.seturl (this.dburl) ؛ datasource.setUserName (اسم المستخدم) ؛ datasource.setPassword (كلمة المرور) ؛ datasource.setDriverClassName (driverClassName) ؛ // التكوين dataSource.setInitialSize (التهيئة) ؛ datasource.setminidle (minidle) ؛ datasource.setMaxActive (maxaCive) ؛ datasource.setMaxWait (maxwait) ؛ datasource.SettimeBetweenevictionRunsmillis (timeBetweenevictionRunsmillis) ؛ datasource.setMineVictableDletimemillis (MineVictableIdletimemillis) ؛ datasource.setValidationQuery (ValideDquery) ؛ datasource.settesthileIdle (testhileIdle) ؛ datasource.settestonborrow (testOnBorrow) ؛ datasource.settestonreturn (testOnreturn) ؛ datasource.setPoolPrepedStatements (poolpreparedstatements) ؛ datasource.setMaxPoolPrepedStatePerConnectionSize (maxpoolprepedstateMentPerConnectionize) ؛ حاول {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 = "requstions" ، value = "*. JS ،*. GIF ،*.
قم بإنشاء druidservlet جديد ، أضف تعليقًا توضيحيًا إلى classwebservlet ، والذي يقوم بتكوين كلمة مرور الحساب لتسجيل الدخول إلى صفحة مراقبة Druid ، وتكوينات القائمة البيضاء والقائمة السوداء ، كما يلي:
حزمة com.dalaoyang.servlet ؛ استيراد javax.servlet.annotation.webinitparam com.dalaoyang.servlet * eMail [email protected] * date 2018/4/12 */ @webservlet (urlpatterns = "/druid/ *" ، initparams = {webinitparam (name = "سمح" ، " webinitparam (name = "deny" ، value = "") ، // ip blacklist (deny يأخذ الأسبقية أكثر Druidservlet يمتد StatViewServlet {}
ثم أضف التعليق التوضيحي servletcomponentscan إلى فئة بدء التشغيل واترك المشروع مسحًا إلى servlet. الرمز كما يلي:
حزمة com.dalaoyang ؛ استيراد org.springframework.boot.springapplication ؛ استيراد org.springframework.boot.autoconfigure.springbootapplication SEVLETCOMPONENTSCAN ENOTATION ، وإلا فإنه لا يمكن فحصه إلى servlet @servletcomponentscan الفئة العامة springbootdruidapplication {public static void main (String [] args) {SpringApplication.Run (SpringBootDruidApplication.class ، args) ؛ }}الباقي هو نفس الكيان (فئة الكيان) ، المستودع (طبقة تشغيل البيانات) ، وحدة التحكم (وحدة التحكم المستخدمة في الاختبار) كـ JPA المتكاملة ، والتي تعرض الكود مباشرة.
مدينة
حزمة com.dalaoyang.entity ؛ استيراد javax.persistence. * ؛/** * author dalaoyang * description * project springboot_learn * package com.dalaoNang.entity generatedValue (الاستراتيجية = genertype.auto) private int cityid ؛ سلسلة خاصة CityName ؛ سلسلة خاصة CityIntroduce ؛ المدينة العامة (int cityid ، String CityName ، String CityIntroduce) {this.cityId = CityId ؛ this.cityName = CityName ؛ هذا. } المدينة العامة (String CityName ، String CityIntroduce) {this.cityName = CityName ؛ هذا. } المدينة العامة () {} public int getCityId () {return cityid ؛ } public void setCityId (int cityid) {this.cityId = CityId ؛ } السلسلة العامة getCityName () {return CityName ؛ } public void setCityName (String CityName) {this.cityName = CityName ؛ } السلسلة العامة getCityIntroduce () {return CityIntroduce ؛ } public void setCityIntroduce (String CityIntroduce) {this.cityIntroduce = CityIntroduce ؛ }}CityRepository
حزمة com.dalaoyang.repository ؛ استيراد com.dalaoyang.entity.city ؛ استيراد org.springframework.data.jpa.repository.jparepository 397600342تن
CityController
package com.dalaoyang.controller ؛ استيراد com.dalaoyang.entity org.springframework.web.bind.annotation.restController ؛/** * Author dalaoyang * description * @pringboot_learn * @package com.dalaoyang.controller * @email [email protected] * {autowired private CityRepository CityRepository ؛ // http: // localhost: 8888/savecity؟ cityName = beijing & cityIntroduce = China's CapitalGetMapping (value = "savecity) public savecity (String CityName ، String CityIntroduce) {City City = New City (CityName ، CityRepository.save (City) ؛ إرجاع "النجاح" ؛ } // http: // localhost: 8888/deletecity؟ cityId = 2GetMapping (value = "deletecity") السلسلة العامة deletecity (int cityid) {cityRepository.Delete (cityid) ؛ إرجاع "النجاح" ؛ } // http: // localhost: 8888/updateCity؟ cityid = 3 & cityName = Shenyang & CityIntroduce = Liaoning Provincial Capital @getMapping (value = "updatecity") CityRepository.save (City) ؛ إرجاع "النجاح" ؛ } // http: // localhost: 8888/getCityByid؟ cityId = 3GetMapping (value = "getCityByid") CitcityByid (int cityid) {City City = CityRepository.Findone (CityId) ؛ عودة المدينة ؛ }}
ثم ابدأ المشروع ويمكنك أن ترى أن وحدة التحكم قد أنشأت طاولة المدينة.
ثم تفضل بزيارة http: // localhost: 8888/druid ويمكنك رؤية الشكل التالي:
أدخل مسؤول كلمة مرور الحساب ، المسؤول ، كما هو موضح في الشكل التالي
ثم في هذا الوقت ، يمكننا زيارة http: // localhost: 8888/savecity؟ cityName = beijing & cityintroduce = china's capital
ثم انقر على مراقبة SQL أعلاه ، كما هو موضح في الشكل أدناه.
من الشكل أعلاه ، يمكنك أن ترى أن SQL الذي بدأ جدول إنشاء المشروع قد تم تنفيذه للتو. تم الانتهاء من التكامل هنا.
رمز المصدر تنزيل: https://gitee.com/dalaoyang/springboot_learn
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.