標籤:des blog http ar io os 使用 sp for
系統內容:
作業系統: windows xp
數 據 庫: mongodb2.0.6
驅 動 包: Spring3.1.2 + mongodb2.7.3 + spring-data-mongodb1.0.1
說明: 對於xp系統使用者,在執行mongod.exe安裝mongodb時 出現,無法定位程式輸入焦點 InterlockedCompareExchange64 於動態連結程式庫KERNEL32.dll上,這是因為最新的開發分支已經不再支援xp.也就是說你的mongodb安裝包不支援xp系統.V2.0.6是最新也是最後一個新版本支援xp.所以筆者使用資料庫為2.0.6
項目結構:
配置說明:
Web.xml檔案配置spring相關與springmvc相關.
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns: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配置 --> <context-param> <param-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配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/context/servlet-context.xml</param-value> <!--指定XML檔案位置--> </init-param> <load-on-startup>4</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
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 Context: defines this servlet‘s request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <mvc:annotation-driven /> <context:component-scan base-package="com.pudp" /> <!-- 配置基於Session的處理,將提交上來的locale參數進行處理 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <!-- 該屬性可以不用配置 --> <property name="defaultLocale" value="ja"></property> </bean> <!-- Resolves views selected for rendering by @Controllers to .jsp resources 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> </beans>
spring設定檔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="com.pudp" /> <!-- 匯入mongodb的設定檔 --> <import resource="mongodb-context.xml"/> </beans>
mongodb的設定檔mongodb-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: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.0.xsd http://www.springframework.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"> <!-- 載入mongodb的屬性設定檔 --> <context:property-placeholder location="classpath:mongodb.properties" /> <!-- 定義mongo對象,對應的是mongodb官方jar包中的Mongo,replica-set設定叢集副本的ip地址和連接埠 --> <mongo:mongo id="mongo" replica-set="${mongo.hostport}"> <!-- 一些串連屬性的設定 --> <mongo:options connections-per-host="${mongo.connectionsPerHost}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" 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="database" mongo-ref="mongo" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="db_mongo" /> </bean></beans>
mongodb的屬性設定檔mongodb.properties
mongo.hostport=127.0.0.1:27017mongo.connectionsPerHost=8mongo.threadsAllowedToBlockForConnectionMultiplier=4#連線逾時時間mongo.connectTimeout=1000#等待時間mongo.maxWaitTime=1500mongo.autoConnectRetry=truemongo.socketKeepAlive=true#Socket逾時時間mongo.socketTimeout=1500mongo.slaveOk=true
編寫Controller、Service、Dao相關.這裡我們測試以下Spring-data-mong中對Collection的實現機制. 我們建立不同的實體類型Member、Article
然後編寫對應的Service、Dao實現.這裡我們側重點持久層實現
持久層的操作實現
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.蘇若年 (En.dennisit)</a> Copy Right since 2013-10-16 * * com.pudp.dao.ArticleDao.java * */@Repositorypublic class ArticleDao extends MongoGenDao<Article>{ /** * 實現鉤子方法,返回反射的類型 * @author <a href=‘mailto:[email protected]‘>Cn.蘇若年(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.蘇若年 (En.dennisit)</a> Copy Right since 2013-10-13 * * com.pudp.dao.MemberDao.java * */@Repositorypublic class MemberDao extends MongoGenDao<Member>{ /** * 實現鉤子方法,返回反射的類型 * @author <a href=‘mailto:[email protected]‘>Cn.蘇若年(En.dennisit)</a> Copy Right since 2013-10-13 * * @return */ @Override protected Class<Member> getEntityClass() { return Member.class; }}
MongoGenDao中我們實現了對庫中添加資料
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.蘇若年 (En.dennisit)</a> Copy Right since 2013-10-13 * * com.pudp.base.MongoGenDao.java * */public abstract class MongoGenDao<T> { @Autowired protected MongoTemplate mongoTemplate; /** * 儲存一個對象 * * @author <a href=‘mailto:[email protected]‘>Cn.蘇若年(En.dennisit)</a> Copy Right since 2013-10-13 下午03:37:28 * * @param t * @return */ public void save(T t){ this.mongoTemplate.save(t); } /** * 為屬性自動注入bean服務 * * @author <a href=‘mailto:[email protected]‘>Cn.pudp(En.dennisit)</a> Copy Right since 2013-10-13 下午03:21:23 * * @param mongoTemplate */ public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } }
這裡需要說明的是MongoTemplate對庫的管理。
MongoTemplate對庫Collection的管理
我們使用MongoTemplate操作持久層.這裡如果我們沒有指定CollectionName的話,會依實體類型的類名作為庫中的集合名,當我們執行資料入庫操作之後,從資料庫中查看到如下資訊.
當然,如果我們想自己定義資料庫的Collection名的化,可以在持久層Dao中指定. 關於Mongodb的可以Mongodb相關的內容. 後續將介紹SpringMVC+ Mongodb 的CRUD + 分頁實現.
SpringMVC整合Mongodb開發 架構搭建