SPRINGMVC Integrated MONGODB Development Architecture Building

Source: Internet
Author: User
Tags aop reflection

System environment:

Operating system: Windows XP

Database: mongodb2.0.6

Driver Package: Spring3.1.2 + mongodb2.7.3 + spring-data-mongodb1.0.1

Note: For XP system users, when performing Mongod.exe installation MongoDB, can not locate the program input point InterlockedCompareExchange64 on the dynamic link library KERNEL32.dll, This is because XP is no longer supported by the latest development Branch. That is, your MongoDB installation package does not support XP. V2.0.6 is the latest and last new version to support XP. So I use database for 2.0.6

Project Structure :



Configuration Description:
The Web. xml file is configured with spring correlation related to SPRINGMVC.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" XM Lns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:/ /java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <!--Spring Configuration--<context-param> <para M-name>contextconfiglocation</param-name> <param-value>/web-inf/context/spring-context.xml</ param-value> </context-param> <listener> <listener-class>org.springframework.web.context .          Contextloaderlistener</listener-class> </listener> <!--spring MVC configuration--<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispat Cherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-                      Name>     <param-value>/WEB-INF/context/servlet-context.xml</param-value> <!--Specify an XML file location-</init- param> <load-on-startup>4</load-on-startup> </servlet> <servlet-mapping&          Gt <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-map ping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>& Lt;/web-app>

Configuration file for Springmvc Servlet-context.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:util= "Http://www.springframework.org/schema/util" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "http:/ /www.springframework.org/schema/tx "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/util/HTTP Www.springframework.org/schema/util/spring-util-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.1.xsd Http://www.springframework.org/schEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <!--dispatcherservlet contex T:defines this servlet ' s request-processing Infrastructure-<!--enables the Spring MVC @Controller progr            Amming model--<mvc:annotation-driven/> <context:component-scan base-package= "com.pudp"/> <!--Configure session-based processing to process the submitted locale parameters--<bean id= "Localeresolver" class= "Org.springframework.web.serv Let.i18n.SessionLocaleResolver "> <!--This property can be used without configuration--<property name=" Defaultlocale "value=" ja " ;</property> </bean> <!--resolves views selected for rendering by @Controllers to. JSP RESOURC Es in The/web-inf/views directory---<bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" PRefix "value="/web-inf/views/"/> <property name=" suffix "value=". jsp "/> </bean> </bea Ns>

Spring configuration file Spring-context.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:util= "Http://www.springframework.org/schema/util" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "http:/ /www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/conText/spring-context-3.1.xsd "> <context:annotation-config/> <context:component-scan base-package=" c Om.pudp "/> <!--importing MongoDB configuration Files--<import resource=" Mongodb-context.xml "/> </beans&gt ;

MongoDB configuration file Mongodb-context.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" XM lns: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.0.xsd HTTP://WWW.SP             Ringframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3 .0.xsd > <!--loading MongoDB Property profile--<context:property-placeholder location= "Classpath:mongodb . Properties "/> <!--define the MONGO object, corresponding to the Mongo,replica-set in the MONGODB official jar package set the IP address and port of the cluster replica--<mongo:mongo i D= "MONGO" replica-set= "${mongo.Hostport} "> <!--settings for some connection properties--<mongo:options connections-per-host=" ${mongo.conn Ectionsperhost} "threads-allowed-to-block-for-connection-multiplier=" ${mongo.threadsallowedtoblockforconnectio             Nmultiplier} "connect-timeout=" ${mongo.connecttimeout} "Max-wait-time=" ${mongo.maxwaittime} "             auto-connect-retry= "${mongo.autoconnectretry}" socket-keep-alive= "${mongo.socketkeepalive}"             socket-timeout= "${mongo.sockettimeout}" slave-ok= "${mongo.slaveok}" write-number= "1" write-timeout= "0" write-fsync= "true"/> </mongo:mongo> <mongo:db-factory dbname= "data Base "mongo-ref=" Mongo "/> <bean id=" mongotemplate "class=" Org.springframework.data.mongodb.core.MongoTemplat    E "> <constructor-arg ref=" Mongo "/> <constructor-arg name=" databaseName "value=" Db_mongo "/> </bean></beaNs> 

MongoDB Property configuration File Mongodb.properties

mongo.hostport=127.0.0.1:27017mongo.connectionsperhost=8mongo.threadsallowedtoblockforconnectionmultiplier=4# Connection Timeout time mongo.connecttimeout=1000# wait time mongo.maxwaittime=1500mongo.autoconnectretry=truemongo.socketkeepalive= True#socket Timeout Time Mongo.sockettimeout=1500mongo.slaveok=true

Write controller, Service, DAO related. Here we test the implementation mechanism for collection in the following Spring-data-mong. We create different entity types Member, article

Then write the corresponding service, DAO implementation. Here we focus on persistence layer implementation


Operation implementation of the persistence layer

Articledao

Package Com.pudp.dao;import Org.springframework.stereotype.repository;import Com.pudp.base.mongogendao;import com.pudp.model.article;/** * Description: * * @author <a href= ' mailto:[email protected] ' > Cn. Sujonin (En.dennisit) < /a> Copy Right Since 2013-10-16  * * Com.pudp.dao.ArticleDao.java * */@Repositorypublic class Articledao extends Mon gogendao<article>{        /**     * Implements the hook method, returns the type of reflection     * @author <a href= ' mailto:[email protected] ' >CN. Sujonin (en.dennisit) </a> Copy right since 2013-10-13      *                     * @return */    @Override    protected class< Article> Getentityclass () {        return article.class;    }}

Memberdao

Package Com.pudp.dao;import Org.springframework.stereotype.repository;import Com.pudp.base.mongogendao;import com.pudp.model.member;/** * Description: * * @author <a href= ' mailto:[email protected] ' > Cn. Sujonin (en.dennisit) </ A> Copy Right Since 2013-10-13  * * Com.pudp.dao.MemberDao.java * */@Repositorypublic class Memberdao extends Mongog endao<member>{    /**     * Implements the hook method, returns the type of reflection     * @author <a href= ' mailto:[email protected] ' >CN. Sujonin ( en.dennisit) </a> Copy right since 2013-10-13      *                     * @return */    @Override    protected class< Member> Getentityclass () {        return member.class;    }}

In Mongogendao we implemented the addition of data to the library.

Package Com.pudp.base;import Org.springframework.beans.factory.annotation.autowired;import org.springframework.data.mongodb.core.mongotemplate;/** * Description: * * @author <a href= ' mailto:[email  Protected] ' > Cn. Sujonin (en.dennisit) </a> Copy Right Since 2013-10-13 * * Com.pudp.base.MongoGenDao.java * */public        Abstract class Mongogendao<t> {@Autowired protected mongotemplate mongotemplate; /** * Save an object * * @author <a href= ' mailto:[email protected] ' &GT;CN. Sujonin (en.dennisit) </a> Copy righ T since 2013-10-13 pm 03:37:28 * * @param t * @return */public void Save (T t) {T    His.mongoTemplate.save (t); }/** * Automatically injects bean service for attributes * * @author <a href= ' mailto:[email protected] ' >cn.pudp (en.dennisit ) </a> Copy Right since 2013-10-13 pm 03:21:23 * * @param mongotemplate */public void       Setmongotemplate (Mongotemplate mongotemplate) { This.mongotemplate = mongotemplate; }    }

What needs to be explained here is the Mongotemplate management of the library.

Mongotemplate the management of library collection



We use the mongotemplate to operate the persistence layer. If we do not specify CollectionName, the name of the collection in the library will be based on the class names of the entity type, and when we perform the data inbound operation, we see the following information from the database.

Of course, if we want to define the collection name of the database, we can specify it in the Persistence layer DAO. About MongoDB can be mongodb related content. The following will introduce the CRUD + paging implementation of springmvc+ Mongodb.

Reprint please specify source: [http://www.cnblogs.com/dennisit/p/3372568.html]

SPRINGMVC Integrated MONGODB Development Architecture Building

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.