คำนำ
จุดเน้นของบทความนี้คือการรวม MongoDB และฤดูใบไม้ผลิเข้ากับโครงการค้นพบปัญหาในทางปฏิบัติติดตามปัญหาและแก้ปัญหา ฉันจะไม่พูดด้านล่างมากนักลองมาดูการแนะนำรายละเอียดด้วยกัน
1. การเตรียม
2. ลักษณะ
3. แพ็คเกจการพึ่งพา
<การพึ่งพา> <roupId> org.springframework.data </groupId> <ratifactid> Spring-Data-MongoDB </artifactid> <sersion> 1.5.0.Release </เวอร์ชัน> <Sersion> 1.5.10 </Sention> </derctency>
การพึ่งพาที่เกี่ยวข้องกับฤดูใบไม้ผลิ
<!-การพึ่งพาที่เกี่ยวข้องกับเว็บในฤดูใบไม้ผลิ-> <การพึ่งพา> <roupId> org.springframework </groupId> <ArtifactId> Spring-Web </Artifactid> <Sersion> $ {Spring.Version} </เวอร์ชัน> </การพึ่งพา <Sersion> 3.1.2.Release </เวอร์ชัน> </การพึ่งพา> <!-การทดสอบการทดสอบฤดูใบไม้ผลิ-> <การพึ่งพา> <GroupId> org.springframework </groupId>4. รวม MongoDB
[หมายเหตุ: โปรดดูบทความของฉันเกี่ยวกับการเพิ่มการจัดการการอนุญาตใน MongoDB: เปิดใช้งานการรับรองความถูกต้องใน MongDB]
MongoDB.Properties
Mongo.hostport = 172.16.4.166: 27017Mongo.dbname = Ad_api_CountMongo.username = hehaitaomongo.password = hehaitao Mongo.ConnectionsPerhost = 8Mongo.ThreadsallowedtoblockForConnectionMultiplier = 4#/u8fde/u63a5/u8d85/u65f6/ U65F6/U95F4MONGO.CONNECTTIMEOUT = 1,000#/U7B49/U5F85/U65F6/U95F4MONGO.MAXWAITTIME = 1500Mongo.AUTOCONNECTRETH y = truemongo.socketkeepalive = true#socket/u8d85/u65f6/u65f6/u95f4mongo.sockettimeout = 1500mongo.slaveok = true
mongoDb.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: mongo = "http://www.springframework.org/schema/data/mongo" XSI: schemalocation = "http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3 http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://ww.springframeworkwork.orgorork <!-โหลดไฟล์การกำหนดค่าคุณสมบัติของ MongoDB-> <บริบท: สถานที่ตั้งของผู้ถือครองตำแหน่ง = "classpath: mongoDB.properties" ไม่สนใจ-Unresolvable = "true"/> <!-กำหนดวัตถุ Mongo ซึ่งสอดคล้องกับ Mongo ชุดจำลองชุดที่อยู่ IP และพอร์ตของแบบจำลองคลัสเตอร์-> <Mongo: Mongo ID = "Mongo" Replica-set = "$ {Mongo.hostport}"> <Mongo: ตัวเลือกการเชื่อมต่อ-per-host = "$ {mongo.connections}" Threads-allowed-to-block-for-connection-multiplier = "$ {mongo.threadsallowedtoblockforconnectionmultiplier}" Connect-timeout = "$ {mongo.connecttimeout}" max-wait-time = "$ {mongo.maxwaittime ซ็อกเก็ต-keep-alive = "$ {mongo.socketkeepalive}" socket-timeout = "$ {mongo.sockettimeout}" slave-ok = "$ {mongo.slaveok}" mongo: mongo: 1 " id = "mgfactory" dbname = "$ {mongo.dbname}" username = "$ {mongo.username}" รหัสผ่าน = "$ {mongo.password}" Mongo-ref = "Mongo" /> <bean id = "Mongotemplate" </ebean> </ebeans>Spring-contex.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://ww.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop/aop/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/task/spring-task-3.1.xsd "> <aop: Aspectj-autopproxy proxy-target-class =" True "/> <บริบท: Annotation-config/> <! base-package = "com.lutong.cps"> <บริบท: exclude-filter type = "Annotation" expression = "org.springframework.steretype.controller"/> </บริบท: Component-Scan>
5. การใช้งานรหัส
การใช้งานขั้นพื้นฐานของ Mongodbservice
/** * ชื่อไฟล์: MongoDbservice.java * แพ็คเกจ: com.lutongnet.ad.service * คำอธิบาย: todo * ผู้แต่ง: zhangfj * วันที่: 2012-11-29 * เวอร์ชัน: v1.0 */แพ็คเกจ com.lutong.cps.schedule.service.fj; org.springframework.context.support.classpathxmlapplicationContext; นำเข้า org.springframework.data.mongodb.core.mongotemplate; นำเข้า org.springframework.data.mongodb.core.query.query.query.query.query.query.query.query. @author Zhangfj * */ @Service ("MongoDbservice") คลาสสาธารณะ MongoDbservice {/ * @resource (name = "Mongotemplate") mongotemplate mongotemplate ที่ป้องกันได้ บริบท = ใหม่ classpathxmlapplicationContext ("mongoDb.xml"); Mongotemplate Mongotemplate = (Mongotemplate) บริบท. getBean ("Mongotemplate"); // คุณสามารถโทรกลับ mongotemplate.findone โดยตรง (แบบสอบถาม, EntityClass); -การสืบทอดคลาส useradcountservice
/** * ชื่อไฟล์: useradcountservice.java * แพ็คเกจ: com.lutongnet.ad.service * คำอธิบาย: todo * ผู้แต่ง: zhangfj * วันที่: 2012-11-29 * เวอร์ชัน: v1.0 */แพ็คเกจ com.lutong.cps.schedule.service.fj; org.springframework.data.mongodb.core.query.query; นำเข้า org.springframework.stereotype.service; นำเข้า com.lutong.cps.schedule.entity.useradcount; MongoDbservice { / ** * รับจำนวนมุมมองของโฆษณาเดียวและหากไม่สามารถสอบถามได้มันจะส่งคืน 0 * * @param adcode * @return int * / public int getUserAdcount (useradcount adcode) {เกณฑ์เกณฑ์ = เกณฑ์ใหม่ (); Criteria.andoperator (Criteria.where ("Userad") คือ (adcode.getUserad ()), เกณฑ์. ที่ไหน ("adcode"). คือ (adcode.getAdcode ()), เกณฑ์. ("countDate") Query Query = แบบสอบถามใหม่ (เกณฑ์); useradCount result = findOne (แบบสอบถาม, useradcount.class); if (null! = ผลลัพธ์) {return result.getTimesCount (); } return 0; -entity class useradcount
แพ็คเกจ com.lutong.cps.schedule.entity; นำเข้า java.util.date; นำเข้า org.springframework.data.annotation.persistenceconstructor; นำเข้า org.springframework.data.mongoDb.core.mapping.document; */@document (collection = "useradcount") คลาสสาธารณะ useradcount {timescount ส่วนตัวภายใน; / *** บัญชีผู้ใช้*/ ผู้ใช้สตริงส่วนตัว; adcode สตริงส่วนตัว; CountDate สตริงส่วนตัว; วันที่ส่วนตัวหมดอายุ; @persistenceConstructor public useradcount (int timescount, string userad, adcode สตริง, สตริง countdate, วันที่หมดอายุ) {this.timescount = timescount; this.userad = userad; this.adcode = adcode; this.countDate = CountDate; this.expireat = หมดอายุ; } public userAdCount (string userad, adcode สตริง, สตริง countDate) {this.userad = userad; this.adcode = adcode; this.countDate = CountDate; } Public UserAdCount (String Userad, String AdCode, String CountDate, วันที่หมดอายุ) {this.userad = userad; this.adcode = adcode; this.countDate = CountDate; this.expireat = หมดอายุ; } public userAdCount (สตริง countDate) {this.countDate = CountDate; } public int getTimesCount () {return timescount; } โมฆะสาธารณะ settimescount (int timescount) {this.timesCount = timescount; } สตริงสาธารณะ getUserad () {return userad; } โมฆะสาธารณะ setUserAd (สตริง userad) {this.userad = userad; } สตริงสาธารณะ getAdCode () {return adcode; } โมฆะสาธารณะ setAdCode (สตริง adcode) {this.adCode = adCode; } สตริงสาธารณะ getCountDate () {return countDate; } โมฆะสาธารณะ setCountDate (String CountDate) {this.countDate = CountDate; } วันที่สาธารณะ getExpiReat () {ส่งคืนหมดอายุ; } โมฆะสาธารณะ setExpireat (วันที่หมดอายุ) {this.expireat = หมดอายุ; -สุดท้ายเขียนคลาสทดสอบเพื่อทดสอบ
นำเข้า java.util.list; นำเข้า javax.annotation.resource; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.springframework.test.context.contextconfiguration; com.acts.web.modules.mark.model.users; นำเข้า com.lutong.cps.schedule.service.fj.useradcountservice; @contextconfiguration ({"classpath: Spring-context.xml", "classpath: mongoDb.xml"}) @Resource (name = "useradCountService") UserAdCountService ส่วนตัว UserAdCountService; @Test โมฆะสาธารณะ testDao () {ลอง {userAdCount userAdCount = ใหม่ userAdCount ("hehaitao", "pos.001", datetime.now (). toString ("yyyy-mm-dd")); int count = useradCountService .getUserAdCount (USERADCOUNT); System.out.println (นับ); } catch (exception e) {e.printstacktrace (); -สรุป
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่าเนื้อหาของบทความนี้จะมีค่าอ้างอิงบางอย่างสำหรับการศึกษาหรือที่ทำงานของทุกคน หากคุณมีคำถามใด ๆ คุณสามารถฝากข้อความไว้เพื่อสื่อสาร ขอบคุณสำหรับการสนับสนุน Wulin.com