springmvc+mongodb+maven 项目搭建配置

操作步骤我就不再细化了

项目能运行,测试过了,先上配置,另一篇文章上代码,要源码包的请留下邮箱

项目结构

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://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>test</groupId>
	<artifactId>springmvc_mongodb</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>springmvc_mongodb Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<spring.version>3.2.3.RELEASE</spring.version>
		<slf4j.version>1.7.2</slf4j.version>
		<log4j.version>1.2.16</log4j.version>
		<junit.version>4.11</junit.version>
		<jsonlib.version>2.4</jsonlib.version>
		<shiro.version>1.2.2</shiro.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>1.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
        <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- Json -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-lgpl</artifactId>
			<version>1.8.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-lgpl</artifactId>
			<version>1.8.1</version>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>${jsonlib.version}</version>
			<classifier>jdk15</classifier>
		</dependency>
		<!-- end of Json -->

		<!-- logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- end of logging -->
		<!-- fileUpload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.2</version>
		</dependency>

		<!-- end of fileUpload -->
		<!--io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-web-api</artifactId>
			<version>7.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

	</dependencies>

	<build>
		<finalName>springmvc_mongodb</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.0-beta-1</version>
				<configuration>
					<uriEncoding>utf-8</uriEncoding>
					<port>8070</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.1.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

	<context:annotation-config />
	<!-- 使Spring支持自动检测组件,如注解的Controller -->
	<context:component-scan base-package="com.jiaoxueyun.*.web.controller" />

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/view" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 启动 Spring MVC 的注解功能,完成请求和注解 POJO 的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<!-- 启动 Spring json转换 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>

	<!-- 靜態資源訪問 -->
	<mvc:resources mapping="/resources/**" location="/resources/" />

	<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name="objectMapper" ref="customObjectMapper"></property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>  

</beans>

applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-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
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.1.xsd
                    http://www.springframework.org/schema/cache
                    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">

	<!-- 注解支持 -->
	<context:annotation-config />

	<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
	<context:component-scan base-package="com">
	</context:component-scan>

	<!-- 属性文件位置 -->
	<bean id="annotationPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:config/properties/mongodb.properties</value>
				<value>classpath:config/properties/common.properties</value>
				<value>classpath:config/properties/log4j.properties</value>
			</list>
		</property>
	</bean>
	<!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->

	<!-- 实体映射 -->
	<bean id="packagesToScan" class="org.springframework.beans.factory.config.ListFactoryBean">
		<property name="sourceList">
			<list>
				<value>com.yiyuwangluo.test.entity</value>
			</list>
		</property>
	</bean>

	<!-- 注解式事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
	</bean>

	 <import resource="classpath:/config/mongodb/mongodb.xml"/>
</beans>

mongodb.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:config/properties/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>

log4j.properties

log4j.rootLogger=INFO,stdout,file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss,SSS} [%c]-[%p] %m%n 

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${spring_springmvc_jpa.root}/shirodemo.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n

log4j.logger.org.hibernate.tool.hbm2ddl=info

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

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://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
	<display-name>test</display-name>

	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:config/properties/log4j.properties</param-value>
	</context-param>
	<!-- Spring的log4j监听器****************************************** -->
	<!-- 设置log4j配置文件路径 -->

	<!-- 开一条watchdog线程每60秒扫描一下配置文件的变化 -->
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>600000</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<!-- ************************************************** -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/spring/applicationContext.xml</param-value>
	</context-param>

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

	<servlet>
		<servlet-name>SpringDispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/spring/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringDispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern> /**/ /*</url-pattern>
	</filter-mapping>

	<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>

</web-app>
时间: 2024-10-12 18:18:43

springmvc+mongodb+maven 项目搭建配置的相关文章

springmvc+mongodb+maven 项目测试代码

你看我有一篇文章配置,或许还会有.mongodb性能测试结果.一个"快"字 源代码包,请留下邮箱 代码结构图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaDM0ODU5MjUzMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" > 直接上代码 BaseDao.java package com.yiyuwanglu.

gitlab+jenkins+maven+docker持续集成(五)——Maven 项目构建配置

首先,安装插件Maven Integration plugin 接下来我们配置Global Tool Configuration 前提先在系统里安装好jdk, maven, 创建maven项目 其它配置大同小异,主要说明下这步 pom.xml 我这里的是在项目中,如果是其它路径这里直接配置好路径就可以 pom.xml配置这里略过 配置后,我们构建

springmvc+mybatis+maven项目框架搭建

项目的目录 1.配置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" xsi:schemaLocation="http://java.sun.com

解决:springmvc maven 项目搭建完后没有src目录

前言:在搭建springmvc maven项目中遇到的问题做总结,比如搭建后没有src,同时这里也解决了搭建后maven导入很慢的问题. 问题: 1.发现创建出来的maven项目没有src文件 ,而且maven导入很慢 解决办法: 在新建项目的这个时候(如下图),点击加号,在name的输入框中填写archetypeCatalog,在value的输入框中填写internal,点击确定: 然后在所选择maven配置文件setting.xml里面加入以下代码,保存后回到 IDEA. <mirror>

Maven项目搭建(二):Maven搭建SSM框架

        上一章给大家讲解了如何使用Maven搭建web项目.       这次给大家介绍一下怎么使用Maven搭建SSM框架项目.       首先我们来看一下pom.xml的属性介绍: project: pom的xml根元素. parent:  声明继承. modules:  声明聚合,该值是一个当前POM的相对目录.用户项目的聚合. groupId:  定义当前Maven项目隶属的实际项目. artifactId:  定义实际项目中的一个Maven项目模块,一般为实际项目名称. ve

Maven项目搭建(三):Maven直接部署项目

    上一章给大家讲解了如何使用Maven搭建SSM框架项目.   这次给大家介绍一下怎么使用Maven直接部署项目.   Maven直接部署项目  1.新建系统变量CATALINA_HOME,值为:Tomcat路径  2.在系统变量 Path的最后面添加%CATALINA_HOME%\lib; %CATALINA_HOME%\lib\servlet-api.jar;%CATALINA_HOME%\lib\jsp-api.jar ,注意它们之间的分号,一定是英文的分号.   3. Tomcat

SSM(maven)项目搭建全过程+实现用户登录功能

项目创建之前确保eclipse中已经配置好了jdk,tomcat,maven如果没有配置下面有配置教程的链接   eclipse中配置jdk的教程url: 配置tomcat的教程url: 配置maven的教程url: 一,创建maven项目 1,新建一个maven项目 2,点击Next后进入下个页面直接点击Next,进入到下面这个页面 3,我们选择最后单词为webapp的这个选项选中后点击Next,进入到下面的这个页面 此处的Group Id代表的是项目的包路径, Artifact Id代表的是

freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven,创建好一个maven的web项目--Demo.我这里是jdk1.7,tomcat7. 修改pom.xml如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/

Maven项目搭建-Eclipse版

一.Maven简介 Maven是基于Java平台的项目构建(mvn clean install).依赖管理(中央仓库,Nexus)和项目信息管理的项目管理工具. Maven是基于项目对象模型(POM),是声明式的,项目构建过程和过程各个阶段所需的工作都由插件实现,并且大部分插件都是现成的,开发者只需要声明项目的基本元素,Maven就执行内置的,完整的构建过程.这在很大程度上消除了重复.在这里可以对比Make和Ant,它俩是过程式的,开发者需要显示的指定一个目标,以及完成该目标所需要执行的任务.针