springMVC+MongoDB配置

举报
扶醉 发表于 2020/12/31 00:47:32 2020/12/31
【摘要】 一、配置文件路径 二、pom.xml配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http:...

一、配置文件路径

二、pom.xml配置


  
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>mongod</groupId>
  6. <artifactId>mongod</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>mongod</name>
  10. <description>mongod web</description>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <!--spring测试需要的包-->
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-test</artifactId>
  19. <version>4.0.0.RELEASE</version>
  20. <scope>provided</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>4.12</version>
  26. <scope>test</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>commons-fileupload</groupId>
  30. <artifactId>commons-fileupload</artifactId>
  31. <version>1.3.1</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.apache.openejb</groupId>
  35. <artifactId>javaee-api</artifactId>
  36. <version>5.0-1</version>
  37. <scope>provided</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>javax.servlet</groupId>
  41. <artifactId>jstl</artifactId>
  42. <version>1.2</version>
  43. <scope>provided</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>javax.servlet.jsp</groupId>
  47. <artifactId>jsp-api</artifactId>
  48. <version>2.1</version>
  49. <scope>provided</scope>
  50. </dependency>
  51. <!-- aop -->
  52. <dependency>
  53. <groupId>org.aspectj</groupId>
  54. <artifactId>aspectjweaver</artifactId>
  55. <version>1.7.1</version>
  56. </dependency>
  57. <!-- mvc -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-webmvc</artifactId>
  61. <version>4.0.0.RELEASE</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-core</artifactId>
  66. <version>4.0.0.RELEASE</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.springframework</groupId>
  70. <artifactId>spring-web</artifactId>
  71. <version>4.0.0.RELEASE</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.springframework</groupId>
  75. <artifactId>spring-orm</artifactId>
  76. <version>4.0.0.RELEASE</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.springframework</groupId>
  80. <artifactId>spring-test</artifactId>
  81. <version>4.0.0.RELEASE</version>
  82. </dependency>
  83. <dependency>
  84. <groupId>org.springframework</groupId>
  85. <artifactId>spring-context</artifactId>
  86. <version>4.0.0.RELEASE</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>org.springframework</groupId>
  90. <artifactId>spring-beans</artifactId>
  91. <version>4.0.0.RELEASE</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>org.springframework</groupId>
  95. <artifactId>spring-expression</artifactId>
  96. <version>4.0.0.RELEASE</version>
  97. </dependency>
  98. <!-- jstl -->
  99. <dependency>
  100. <groupId>javax.servlet</groupId>
  101. <artifactId>jstl</artifactId>
  102. <version>1.2</version>
  103. </dependency>
  104. <!-- mongodb -->
  105. <dependency>
  106. <groupId>org.springframework.data</groupId>
  107. <artifactId>spring-data-mongodb</artifactId>
  108. <version>1.5.5.RELEASE</version>
  109. </dependency>
  110. <!--JSONObject-->
  111. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  112. <dependency>
  113. <groupId>commons-logging</groupId>
  114. <artifactId>commons-logging</artifactId>
  115. <version>1.0.4</version>
  116. </dependency>
  117. <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
  118. <dependency>
  119. <groupId>commons-lang</groupId>
  120. <artifactId>commons-lang</artifactId>
  121. <version>2.3</version>
  122. </dependency>
  123. <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
  124. <dependency>
  125. <groupId>commons-collections</groupId>
  126. <artifactId>commons-collections</artifactId>
  127. <version>3.2</version>
  128. </dependency>
  129. <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
  130. <dependency>
  131. <groupId>commons-beanutils</groupId>
  132. <artifactId>commons-beanutils</artifactId>
  133. <version>1.7.0</version>
  134. </dependency>
  135. <!--json-lib -->
  136. <dependency>
  137. <groupId>com.hynnet</groupId>
  138. <artifactId>json-lib</artifactId>
  139. <version>2.4</version>
  140. </dependency>
  141. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  142. <dependency>
  143. <groupId>com.alibaba</groupId>
  144. <artifactId>fastjson</artifactId>
  145. <version>1.2.54</version>
  146. </dependency>
  147. <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
  148. <dependency>
  149. <groupId>net.sf.ezmorph</groupId>
  150. <artifactId>ezmorph</artifactId>
  151. <version>1.0.4</version>
  152. </dependency>
  153. </dependencies>
  154. <build>
  155. <plugins>
  156. <plugin>
  157. <groupId>org.apache.maven.plugins</groupId>
  158. <artifactId>maven-compiler-plugin</artifactId>
  159. <version>2.3.2</version>
  160. <configuration>
  161. <source>1.8</source>
  162. <target>1.8</target>
  163. </configuration>
  164. </plugin>
  165. </plugins>
  166. </build>
  167. </project>

三、applicationContext.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  7. xmlns:p="http://www.springframework.org/schema/p"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xmlns:cache="http://www.springframework.org/schema/cache"
  10. xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
  11. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  12. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  13. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  14. http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
  15. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  16. <!-- 注解支持 -->
  17. <context:annotation-config></context:annotation-config>
  18. <!-- 组件扫描,排除controller,该组件由springmvc配置文件扫描 -->
  19. <context:component-scan base-package="net"></context:component-scan>
  20. <!-- 配置文件 -->
  21. <bean id="annotationPropertyConfigurer"
  22. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  23. <property name="locations">
  24. <list>
  25. <value>classpath:config/properties/mongodb.properties</value>
  26. </list>
  27. </property>
  28. </bean>
  29. <!-- 实体映射 -->
  30. <bean id="packagesToScan"
  31. class="org.springframework.beans.factory.config.ListFactoryBean">
  32. <property name="sourceList">
  33. <list>
  34. <value>net.fuzui.pojo</value>
  35. </list>
  36. </property>
  37. </bean>
  38. <!-- 注解式事务 -->
  39. <tx:annotation-driven
  40. transaction-manager="transactionManager" />
  41. <bean id="multipartResolver"
  42. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  43. <property name="defaultEncoding" value="utf-8"></property>
  44. </bean>
  45. <import resource="classpath:config/mongodb/mongodb.xml"/>
  46. </beans>

四、springMVC.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  10. <!-- 支持注解 -->
  11. <mvc:annotation-driven />
  12. <!-- 自动装配handlermapping和hangleradapter -->
  13. <mvc:default-servlet-handler/>
  14. <!-- 扫描路由控制器 -->
  15. <context:component-scan base-package="net.fuzui" />
  16. <!-- 设置静态资源访问 -->
  17. <mvc:resources location="/" mapping="/**"/>
  18. <!-- 视图解析器 -->
  19. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
  21. <property name="prefix" value="/WEB-INF/views"></property>
  22. <property name="suffix" value=".jsp"></property>
  23. </bean>
  24. </beans>

五、mongodb.xml配置文件


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  12. http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
  13. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  14. <mongo:mongo id="mongo" replica-set="${mongo.hostport}">
  15. <!-- 一些连接属性的设置 -->
  16. <mongo:options
  17. connections-per-host="${mongo.connectionsPerHost}"
  18. threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
  19. connect-timeout="${mongo.connectTimeout}"
  20. max-wait-time="${mongo.maxWaitTime}"
  21. auto-connect-retry="${mongo.autoConnectRetry}"
  22. socket-keep-alive="${mongo.socketKeepAlive}"
  23. socket-timeout="${mongo.socketTimeout}"
  24. slave-ok="${mongo.slaveOk}"
  25. write-number="1"
  26. write-timeout="0"
  27. write-fsync="true" />
  28. </mongo:mongo>
  29. <mongo:db-factory dbname="database" mongo-ref="mongo" />
  30. <bean id="mongoTemplate"
  31. class="org.springframework.data.mongodb.core.MongoTemplate">
  32. <constructor-arg name="mongo" ref="mongo" />
  33. <constructor-arg name="databaseName" value="test" />
  34. </bean>
  35. </beans>

六、mongodb.properties配置文件


  
  1. mongo.hostport=127.0.0.1:27017
  2. mongo.connectionsPerHost=8
  3. mongo.threadsAllowedToBlockForConnectionMultiplier=4
  4. #连接超时时间
  5. mongo.connectTimeout=1000
  6. #等待时间
  7. mongo.maxWaitTime=1500
  8. mongo.autoConnectRetry=true
  9. mongo.socketKeepAlive=true
  10. #Socket超时时间
  11. mongo.socketTimeout=1500
  12. mongo.slaveOk=true

七、web.xml配置


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. - This is the Cocoon web-app configurations file
  4. -
  5. - $Id$
  6. -->
  7. <web-app version="2.4"
  8. xmlns="http://java.sun.com/xml/ns/j2ee"
  9. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  11. <!-- 加载spring ioc容器 -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:config/spring/applicationContext.xml</param-value>
  15. </context-param>
  16. <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
  17. <servlet>
  18. <servlet-name>springMVC</servlet-name>
  19. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  20. <init-param>
  21. <param-name>contextConfigLocation</param-name>
  22. <param-value>classpath:config/spring/springMVC.xml</param-value>
  23. </init-param>
  24. <load-on-startup>1</load-on-startup>
  25. </servlet>
  26. <!-- Map all requests to the DispatcherServlet for handling -->
  27. <servlet-mapping>
  28. <servlet-name>springMVC</servlet-name>
  29. <url-pattern>/</url-pattern>
  30. </servlet-mapping>
  31. <filter>
  32. <filter-name>encodingFilter</filter-name>
  33. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  34. <init-param>
  35. <param-name>encoding</param-name>
  36. <param-value>UTF-8</param-value>
  37. </init-param>
  38. <init-param>
  39. <param-name>forceEncoding</param-name>
  40. <param-value>true</param-value>
  41. </init-param>
  42. </filter>
  43. <filter-mapping>
  44. <filter-name>encodingFilter</filter-name>
  45. <url-pattern>/*</url-pattern>
  46. </filter-mapping>
  47. <listener>
  48. <listener-class>
  49. org.springframework.web.context.ContextLoaderListener
  50. </listener-class>
  51. </listener>
  52. <!--实现rest风格架构-->
  53. <filter>
  54. <filter-name>HiddenHttpMethodFilter</filter-name>
  55. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  56. </filter>
  57. <filter-mapping>
  58. <filter-name>HiddenHttpMethodFilter</filter-name>
  59. <url-pattern>/*</url-pattern>
  60. </filter-mapping>
  61. </web-app>

 

文章来源: fuzui.blog.csdn.net,作者:极客时代,版权归原作者所有,如需转载,请联系作者。

原文链接:fuzui.blog.csdn.net/article/details/88043618

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。