您的位置:首页 > 数据库 > Mongodb

SpringMvc与Mongodb搭建

2017-08-01 16:33 225 查看
Spring版本:4.2.0

MongoDb版本:3.4.6

Spring与MongoDb关键jar:spring-data-mongodb-1.8.6.RELEASE.jar

一、项目目录结构:



二、相关配置文件

  1、mongodb.properties

mongo.host=10.0.2.20

mongo.port=27018

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

mongo.writeconcern=safe

  2.jdbc-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:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mongo="http://www.springframework.org/schema/data/mongo"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

        http://www.springframework.org/schema/data/mongo

        http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd

        http://www.springframework.org/schema/data/repository

        http://www.springframework.org/schema/data/repository/spring-repository-1.5.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

        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">

    <context:property-placeholder location="classpath:conf/jdbc/mongodb.properties" />

    <!-- 配置数据源 -->

    <mongo:mongo-client host="${mongo.host}" port="${mongo.port}" id="mongo">

    <mongo:client-options write-concern="${mongo.writeconcern}"

    connect-timeout="${mongo.connectTimeout}" socket-keep-alive="${mongo.socketKeepAlive}"/>

    </mongo:mongo-client>

    <!-- 设置使用的数据库 名-->

    <mongo:db-factory dbname="mycol" mongo-ref="mongo"/>

    <!-- mongodb的模板 -->

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>

    </bean>

    <!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->

    <mongo:mapping-converter base-package="com.model,com.common.model" />

    <!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 <mongo:repositories

        base-package="com.houyuan.repository" /> -->

</beans>

 3.annotation-spring-servlet.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:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans-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
       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">  

 

       

    

     <!-- 激活spring的注解. -->  

    <context:annotation-config />

    <context:component-scan base-package="com" />

    <!--默认注解映射的支持  配置注解驱动的Spring MVC Controller 的编程模型.注:此标签只在 Servlet MVC工作!-->

      <mvc:annotation-driven />

    

    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->

    <!-- <context:component-scan base-package="com.*" /> -->

    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 多个包已逗号分隔 -->

    <context:component-scan base-package="com.controller,com.service,com.manager,com.dao" />

    <!--

    <context:component-scan base-package="com.service" />

    <context:component-scan base-package="com.manager" />

    <context:component-scan base-package="com.dao" />

    -->

    

      <!-- 返回json 方法二 需要导入 jackson-annotations.jar,jackson-core.jar,jackson-databind.jar-->  

       <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

        <property name="messageConverters">

            <list>

                <bean class="org.springframework.http.converter.StringHttpMessageConverter">

                    <property name="supportedMediaTypes">

                        <list>

                            <value>text/html;charset=UTF-8</value>

                            <value>application/json;charset=UTF-8</value>

                        </list>

                    </property>

                </bean>

                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

                    <property name="supportedMediaTypes">

                        <list>

                            <value>text/html;charset=UTF-8</value>

                            <value>application/json;charset=UTF-8</value>

                        </list>

                    </property>

                </bean>

            </list>

        </property>

    </bean>

    

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />

    

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

        <!-- 国际化信息所在的文件名,value的值为资源文件路径+资源文件前缀 -->                     

        <property name="basename"  >

            <value>/i18n/Resource</value>

        </property>   

        <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称  -->               

        <property name="useCodeAsDefaultMessage" value="true" />           

    </bean>

    

    <mvc:resources location="/image/" mapping="/image/**"/>

    <mvc:resources location="/css/" mapping="/css/**"/>

    <mvc:resources location="/js/" mapping="/js/**"/>

    

    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->  

    <!-- <bean id="mappingJacksonHttpMessageConverter"  

        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  

        <property name="supportedMediaTypes">  

            <list>  

                <value>text/html;charset=UTF-8</value>  

            </list>  

        </property>  

    </bean>  

    <bean id="transactionManager"  

        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

        <property name="dataSource" ref="dataSource" />  

    </bean> -->

    <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务

           第一个*代表所有的返回值类型

           第二个*代表所有的类

           第三个*代表类所有方法 最后一个..代表所有的参数

    

    <aop:config proxy-target-class="true">

        <aop:pointcut id="serviceMethod"

            expression="execution(* com..*.*(..))" />

        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />

    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

        <tx:attributes>

            <tx:method name="*" />

        </tx:attributes>

    </tx:advice>      -->

    <!-- 支持文件上传 -->

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

    <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

        p:viewClass="org.springframework.web.servlet.view.JstlView"

        p:prefix="/view/"

        p:suffix=".jsp" />

    <import resource="i18n-spring.xml"/>

</beans>

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

                       http://www.springframework.org/schema/tx

                       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

                    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

      <display-name>SpringDemo练手</display-name>

    <welcome-file-list>

        <welcome-file>view/index.jsp</welcome-file>

    </welcome-file-list>

 

  <!-- ContextLoaderListener 加载 -->

    <context-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:conf/*-spring.xml</param-value>

    </context-param>

    

    <listener>

        <description>spring监听器</description>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    

    <!-- 防止spring内存溢出监听器

    <listener>

        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

    </listener>-->

    

    <servlet>

        <servlet-name>annotation-spring</servlet-name>

            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

            <init-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>classpath:conf/annotation-spring-servlet.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>annotation-spring</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

    

    <!-- <servlet>

        <servlet-name>userServlet</servlet-name>

        <servlet-class>com.servlet.UserServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>userServlet</servlet-name>

        <url-pattern>/*</url-pattern>

    </servlet-mapping> -->

    <filter>

          <filter-name>userFilter</filter-name>

          <filter-class>com.filter.UserFilter</filter-class>

      </filter>

      <filter-mapping>

          <filter-name>userFilter</filter-name>

          <url-pattern>/*</url-pattern>

      </filter-mapping>

    <!-- 统一字符编码为UTF-8 -->

    <filter>

        <filter-name>characterEncode</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>

      </filter>

    <filter-mapping>

        <filter-name>characterEncode</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

 

  <!-- 配置session超时时间,单位分钟 -->

    <session-config>

        <session-timeout>15</session-timeout>

    </session-config>

    <context-param>

      <param-name>log4jConfigLocation</param-name>

      <param-value>classpath:conf/log4j/log4j.xml</param-value>

    </context-param>

    

    <!-- 配置log4j.xml变量 -->

    <context-param>

      <param-name>rootLevel</param-name>

      <param-value>DEBUG</param-value>

    </context-param>

    

    <context-param>

      <param-name>loggingLevel</param-name>

      <param-value>INFO</param-value>

    </context-param>

    

    <!-- 配置log4j.xml监听器 -->

    <listener>

        <listener-class>com.common.listener.LogBackConfigListener</listener-class>

    </listener>

    

    <!-- 错误跳转页面 -->

    <error-page>

        <!-- 路径不正确 -->

        <error-code>404</error-code>

        <location>/view/errorPage/404.jsp</location>

    </error-page>

    <error-page>

        <!-- 没有访问权限,访问被禁止 -->

        <error-code>405</error-code>

        <location>/view/errorPage/405.jsp</location>

    </error-page>

    <error-page>

        <!-- 内部错误 -->

        <error-code>500</error-code>

        <location>/view/errorPage/500.jsp</location>

    </error-page>

    

</web-app>

简单的页面成果




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息