系统环境:操作系统: 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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://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.propertiesmongo.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实现.这里我们侧重点持久层实现
持久层的操作实现ArticleDaopackage com.pudp.dao;import org.springframework.stereotype.Repository;import com.pudp.base.MongoGenDao;import com.pudp.model.Article;/** * description: * * @author <a href="mailto:dennisit@163.com"> 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:dennisit@163.com">Cn.苏若年(En.dennisit)</a> Copy Right since 2013-10-13* * @return */@Overrideprotected Class<Article> getEntityClass() {return Article.class;}}MemberDaopackage com.pudp.dao;import org.springframework.stereotype.Repository;import com.pudp.base.MongoGenDao;import com.pudp.model.Member;/** * description: * * @author <a href="mailto:dennisit@163.com"> 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:dennisit@163.com">Cn.苏若年(En.dennisit)</a> Copy Right since 2013-10-13* * @return */@Overrideprotected 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:dennisit@163.com"> Cn.苏若年 (En.dennisit)</a> Copy Right since 2013-10-13* * com.pudp.base.MongoGenDao.java * */public abstract class MongoGenDao<T> {@Autowiredprotected MongoTemplate mongoTemplate;/** * 保存一个对象 * * @author <a href="mailto:dennisit@163.com">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:dennisit@163.com">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 + 分页实现.
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2015-02/114229p2.htm ------------------------------------------分割线------------------------------------------CentOS 6 使用 yum 安装MongoDB及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htmUbuntu 13.04下安装MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htmMongoDB入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htmUbunu 14.04下MongoDB的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htmNagios监控MongoDB分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm基于CentOS 6.5操作系统搭建MongoDB服务 http://www.linuxidc.com/Linux/2014-11/108900.htm
Linux环境下MongoDB数据库安装指导文档Oracle计算表所占用空间大小相关资讯 MongoDB SpringMVC
- SpringMVC+MyBatis集成配置 (今 07:25)
- 何用Eclipse搭建SpringMVC最小系统 (09月03日)
- 使用SSM框架搭建Web服务器实现登录 (08月15日)
| - SpringMVC框架入门配置 IDEA下搭建 (09月06日)
- SpringMVC中使用Json传数据 (08月30日)
- SpringMVC——一个简单的例子 (08月02日)
|
本文评论 查看全部评论 (1)
评论声明- 尊重网上道德,遵守中华人民共和国的各项有关法律法规
- 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
- 本站管理人员有权保留或删除其管辖留言中的任意内容
- 本站有权在网站内转载或引用您的评论
- 参与本评论即表明您已经阅读并接受上述条款
| |