时间:2021-07-01 10:21:17 帮助过:10人阅读
SpringMVC整合MongoDB开发,架构搭建,高级操作。
首页 → 数据库技术
背景:
阅读新闻
SpringMVC整合MongoDB开发
架构搭建
[日期:2015-02-28] 来源:Linux社区 作者:dennisit [字体:]
系统环境:
操作系统: windows xp
数 据 库: mongodb2.0.6
驱 动 包: Spring3.1.2 + mongodb2.7.3 + spring-data-mongodb1.0.1
项目结构:
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 "contextConfigLocation/WEB-INF/context/spring-context.xmlorg.springframework.web.context.ContextLoaderListenerspringmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocation/WEB-INF/context/servlet-context.xml4springmvc/index.jsp
Springmvc的配置文件servlet-context.xml
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/spring-beans-3.1.xsd http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd "
spring配置文件Spring-context.xml
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/spring-beans-3.1.xsd http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop/spring-aop-3.1.xsd "
mongodb的配置文件mongodb-context.xml
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 "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
mongodb的属性配置文件mongodb.properties
mongo.hostport=127.0.0.1:27017 mongo.connectionsPerHost=8 mongo.threadsAllowedToBlockForConnectionMultiplier=4 #连接超时时间 mongo.connectTimeout=1000 #等待时间 mongo.maxWaitTime=1500 mongo.autoConnectRetry=true mongo.socketKeepAlive=true #Socket超时时间 mongo.socketTimeout=1500 mongo.slaveOk=true
持久层的操作实现
ArticleDao
package com.pudp.dao; import org.springframework.stereotype.Repository; import com.pudp.base.MongoGenDao; import com.pudp.model.Article; /** * description: * * @author Cn.若年 (En.dennisit) Copy Right since 2013-10-16 * * com.pudp.dao.ArticleDao.java * */ @Repository public class ArticleDao extends MongoGenDao{ /** * 实现钩子方法,返回反射的类型 * @author Cn.苏若年(En.dennisit) Copy Right since 2013-10-13 * * @return */ @Override protected Class getEntityClass() { return Article.class; } }
MemberDao