//参考http://blog.csdn.net/zhshulin/article/details/30779873?utm_source=tuicool
最近因为工程需要,重新搭建了下maven工程,谈不上对各个环节都精通,但搭建出来的工程可以使用。在这里将搭建流程记录一下,供参考。
一、安装jdk,配置环境
1.下载jdk,百度自行解决,搞java的都懂。
2.配置完环境,配置完后,在 cmd里面输入 java -version测试jdk环境是否配置好。
二、安装maven,配置环境
1.到maven官网下载适合自己系统的包,例如:apache-maven-3.2.1
2.配置环境变量 --百度解决
3.配置完后,在cmd里输入mvn -version测试maven环境是否配置好
4.maven数据库仓库,默认就行。
三、tomcat8安装
1.到官网上下载http://tomcat.apache.org/download-80.cgi。
2.由于下的是安装版,所以不需要配置任何东西,一路安装到底,不需要配置环境变量。
四、myeclipse安装
1.这个不用多解释,如果这个都安装不好,趁早换行,不适合搞IT这行。
2.myeclipse中自带有jdk,我们将其换成我们自己的,myeclipse -->windows -->preference 输入jdk -->installed jres -->add 刚才我们安装的jdk。
3.myeclipse中自带有maven,我们将其换成我们自己的,myeclipse -->windows -->preference 输入maven -->maven4Myeclipse -->installations -->add刚才我们安装的maven。
4.myeclipse中自带有maven,我们将其换成我们自己的,myeclipse -->windows -->preference输入tomcat -->tomcat 8.x -->enable -->browse我们刚才安装tomcat的位置。
五、搭建maven工程
1.按照如下步骤搭建工程
2.注意
添加src/main/java的时候可能添加不上,此时应该百度下,网上有很多方法可以解决。
最终的目录结构就应该是这样
mybatis-config.xml文件内容
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 命名空间 --> <typeAliases> <typeAlias alias="SystemInfoBean" type="bean.SystemInfoBean"/> </typeAliases> <!-- 映射map --> <mappers> </mappers> </configuration>
applicationContext.xml内容
<?xml version="1.0" encoding="GBK"?> <!-- 指定Spring配置文件的Schema信息 --> <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 定义数据源Bean,使用C3P0数据源实现 --> <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 连 Oracle --> <!-- 指定连接数据库的驱动 --> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property> <!-- 指定连接数据库的URL --> <property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.2.10:1521:orcl"></property> <!-- 指定连接数据库的用户名 --> <property name="user" value="zbsw"></property> <!-- 指定连接数据库的密码 --> <property name="password" value="zbsw"></property> <!-- 指定连接数据库连接池的最大连接数 --> <property name="maxPoolSize" value="100"></property> <!-- 指定连接数据库连接池的最小连接数 --> <property name="maxIdleTime" value="10"></property> <!-- 指定连接数据库连接池的初始化连接数 --> <property name="checkoutTimeout" value="500"></property> <!-- 连接关闭时默认将所有未提交的操作回滚 --> <property name="autoCommitOnClose" value="false"></property> </bean> <!-- 创建SqlSessionFactory,同时指定数据源--> <!-- mybatis配置文件,扫描所有的*.xml文件--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!-- 配置mapper映射 --> <!-- spring与mybatis整合配置,扫描所有的*.mapper文件--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="dao.mappers" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 输出对象转JSON支持 --> <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> </list> </property> </bean> <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringConverter"/> <ref bean="jsonConverter" /> </list> </property> </bean> </beans>
springMVC-servlet.xml文件内容
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven /> <mvc:default-servlet-handler/> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> <context:component-scan base-package="controller" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp" /> <!-- 文件上传配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="10485760000"></property> <property name="maxInMemorySize" value="40960"></property> </bean> </beans>
web.xml文件内容,创建maven工程时,这个里面是空的,需要自己配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>CQR</display-name> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</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>
pom.xml文件内容,刚创建的时候,<dependencies>与</dependencies>之间只有junit这一项,其它的都是根据本工程需要自己配置的。
<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>readygo-tec</groupId> <artifactId>CQR</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>CQR Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- org.apache.maven.plugins --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> </dependency> <!-- commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- org.springframework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.0.6.RELEASE</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- hibernate的引用 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.1.0.7.0</version> </dependency> <!-- org.aspectj --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.5</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.5</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>1.6.5</version> </dependency> <!-- org.codehaus.jackson --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.2</version> </dependency> <!-- net.sf.json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- com.lowagie --> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.6.6</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.6</version> <scope>runtime</scope> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> <build> <finalName>CQR</finalName> </build> </project>
自此,完结。