热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

spring整合MongoDB(吐血经验)

最近老师叫我们写一个项目练练MongoDB的使用, 我想着就用spring + springMVC + Mongodb来写. 这简直就是给自己挖坑往里面跳 哎哎哎!!!下面就分享下我的经历1.新建一个

最近老师叫我们写一个项目练练MongoDB的使用, 我想着就用spring + springMVC + Mongodb来写. 这简直就是给自己挖坑往里面跳 哎哎哎!!!

下面就分享下我的经历

1.新建一个spring +web 的项目 (我用的是idea) , 开始项目部署

第一步 添加Exploded到项目中

在这里插入图片描述第二步配置tomcat(这就不演示了,基本配置)

第三步 添加tomcat到项目中(这一步很重要)
在这里插入图片描述
点击右面的 + 号 添加tomcat

第四步添加MongoDB所需要的jar包

lib包

在这里插入图片描述
在这里插入图片描述
上面那几个对勾一定要和我的一样, 第二对勾是添加MongoDB 所依赖的库.
由于我们在新建项目的时候idea就帮我们添加了spring5 最新版的jar包. 所以我们要选
spring-data-mongodb-2.2.4.RELEASE.jar 也是最新版jar包 然后他就自动帮你添加整合MongoDB所有的jar包 包括驱动包
在这里插入图片描述
要是springMVC的jar 没导进来 在这可以添加

在这里插入图片描述
到此jar包依赖完成

下面sping 整合MongoDB配置

在这里插入图片描述

spring.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.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<aop:aspectj-autoproxy proxy-target-class="true"/>-->
<!--================controller不归spring管==================-->
<context:component-scan base-package="com.liang">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--使用注解管理bean -->
<context:annotation-config/>
<!-- 加载mongodb的属性配置文件 -->
<context:property-placeholder location="classpath:mongodb.properties"/>
<mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}">
<mongo:client-options
connections-per-host="${mongo.connectionsPerHost}"
min-connections-per-host="${mongo.minConnectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}"
max-wait-time="${mongo.maxWaitTime}"
socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}"
description="${mongo.description}"
max-connection-idle-time="${mongo.maxConnectionIdleTime}"
max-connection-life-time="${mongo.maxConnectionLifeTime}"
heartbeat-socket-timeout="${mongo.heartbeatSocketTimeout}"
heartbeat-connect-timeout="${mongo.heartbeatConnectTimeout}"
min-heartbeat-frequency="${mongo.minHeartbeatFrequency}"
heartbeat-frequency="${mongo.heartbeatFrequency}"
/>
</mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.defaultDbName}" mongo-ref="mongoClient"/>
<mongo:template id="mongoTemplate" db-factory-ref="mongoDbFactory" write-concern="NORMAL"/>
<!--激活注解-->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>

springMVC配置

<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- ================扫包controller==================== -->
<context:component-scan base-package="com.liang" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- =================视图解析器======================= -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--
自己映射的就自己处理,不能处理的就交给tomcat。(例如js , css)
-->
<mvc:default-servlet-handler/>
<!-- ========springMVC保证动静态资源都能被访问到========= -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>

mongodb.properties配置

mongo.host=127.0.0.1
mongo.port=27017
mongo.defaultDbName=ems
#mongo.user=joyven
#mongo.pwd=123456
mongo.connectionsPerHost=10
mongo.threadsAllowedToBlockForConnectionMultiplier=5
mongo.minConnectionsPerHost=5
#连接超时时间
mongo.connectTimeout=10000
#等待时间
mongo.maxWaitTime=120000
#Socket超时时间
mongo.socketTimeout=0
mongo.socketKeepAlive=true
mongo.description=ems test mongodb database
mongo.maxConnectionIdleTime=1500
mongo.maxConnectionLifeTime=0
#mongo slave
mongo.heartbeatSocketTimeout=1000
mongo.heartbeatConnectTimeout=1500
mongo.minHeartbeatFrequency=5
mongo.heartbeatFrequency=10

web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>ems</display-name>
<welcome-file-list>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- =========加载spring配置文件========== -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- =========监听器 加载spring ioc容器 实现自动装配ApplicationContext========= -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ==========加载springMVC配置文件=========== -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- =======过滤器设置复编码========= -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<!-- =======响应体编码设置========= -->
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*



HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter


HiddenHttpMethodFilter
/*



推荐阅读
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Allegro总结:1.防焊层(SolderMask):又称绿油层,PCB非布线层,用于制成丝网印板,将不需要焊接的地方涂上防焊剂.在防焊层上预留的焊盘大小要比实际的焊盘大一些,其差值一般 ... [详细]
  • 开发笔记:(002)spring容器中bean初始化销毁时执行的方法及其3种实现方式
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了(002)spring容器中bean初始化销毁时执行的方法及其3种实现方式相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • http头_http头部注入
    1、http头部注入分析1、原理 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 解决IDEA配置xml文件头报错的方法
    本文介绍了解决IDEA配置xml文件头报错的方法,包括了具体的解决方案和步骤。通过本文的指导,读者可以轻松解决这个问题并正常使用IDEA进行开发工作。 ... [详细]
author-avatar
也许_枉然
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有