在平时的javaweb项目中便于后期的维护,我们会进行分层开发,最常见的分为utils(常用公共方法),domain(模型层),dao(数据库访问层),service(业务逻辑层),web(表现层),这样分层之后,各个层之间的职能比较明确,便于后期的维护,今天我们就使用maven多模块来构建以上各个层。
本项目详细代码下载地址:http://download.csdn.net/detail/chendaoqiu/8820949
项目结构如下:
movieInvest
|—-pom.xml
|—-movieInvest-utils
|—-pom.xml
|—-movieInvest-domain
|—-pom.xml
|—-movieInvest-dao
|—-pom.xml
|—-movieInvest-service
|—-pom.xml
|—-movieInvest-web
|—-pom.xml
1.创建movieInvest项目
创建maven项目的我们上一节有详细的讲述,这里再讲述一遍
1.1新建
1.2选择项目类型
1.3填写groupId,ArtifactId
打开pom.xml
添加一行
<packaging>pompackaging>
在把文件java Resource下的所有文件删除,因为此项目是做管理,不需要任何代码,只需要pom.xml文件
2.创建子项目movieInvest-utils
在创建好的项目上new一个,选择如下图
填写子项目名
选择类型
一直到完成
打开项目movieInvest-utils的pom.xml
添加一行
<packaging>jarpackaging>
3.创建movieInvest-domain,movieInvest-dao,movieInvest-service
创建这三个子项目都是和创建movieInvest-utils一样,这里就不再讲述了
4.创建movieInvest-web
在创建好的项目上new一个,选择如下图
填写项目名
选择项目类型
点击完成
打开pom.xml增加一行
<packaging>warpackaging>
这里我们打开movieInvest项目的pom.xml下,我们观察可以看到增加了几行
<module>movieInvest-utils</module>
<module>movieInvest-dao</module>
<module>movieInvest-domain</module>
<module>movieInvest-service</module>
<module>movieinvest-web</module>
</modules>
4.添加各个子项目的关系
dao层添加依赖domain和utils
在dao项目中的pom.xml添加
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-domainartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-utilsartifactId>
<version>${project.version}version>
dependency>
service层依赖domain,utils,dao层
在service项目中的pom.xml添加
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-domainartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-utilsartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-daoartifactId>
<version>${project.version}version>
dependency>
web层添加service,domain,utils
在web项目中的pom.xml添加
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-utilsartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-domainartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-serviceartifactId>
<version>${project.version}version>
dependency>
5,配置spring+mybatis
在movieInvest项目中的pom.xml中引入jar包
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.0modelVersion>
<groupId>com.yaozhitechgroupId>
<artifactId>movieInvest-rootartifactId>
<packaging>pompackaging>
<version>0.0.1-SNAPSHOTversion>
<name>movieInvest-rootname>
<url>http://maven.apache.orgurl>
<build>
<finalName>movieInvestfinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>2.1version>
<configuration>
<source>1.7source>
<target>1.7target>
configuration>
plugin>
plugins>
build>
<properties>
<spring-version>3.2.1.RELEASEspring-version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<commons-lang3.version>3.1commons-lang3.version>
<logback.version>0.9.29logback.version>
<aspectj.version>1.7.0aspectj.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.0.5version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>org.postgresqlgroupId>
<artifactId>postgresqlartifactId>
<version>9.3-1102-jdbc4version>
dependency>
<dependency>
<groupId>commons-netgroupId>
<artifactId>commons-netartifactId>
<version>3.3version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
<version>1.8.0version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>commons-collectionsgroupId>
<artifactId>commons-collectionsartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-jexlartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>commons-digestergroupId>
<artifactId>commons-digesterartifactId>
<version>2.0version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.1version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>net.sf.json-libgroupId>
<artifactId>json-libartifactId>
<version>2.4version>
<classifier>jdk15classifier>
dependency>
dependencies>
<modules>
<module>movieInvest-utilsmodule>
<module>movieInvest-daomodule>
<module>movieInvest-domainmodule>
<module>movieInvest-servicemodule>
<module>movieinvest-webmodule>
modules>
project>
在web项目中的web.xml配置成
<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_3_0.xsd"
version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:applicationContext*.xmlparam-value>
context-param>
<servlet>
<servlet-name>springservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:/applicationContext-MVC.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
listener>
<session-config>
<session-timeout>360session-timeout>
session-config>
web-app>
在该项目的java resouce中的src/main/resources中添加配置文件
applicationContext-MVC.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">
<context:annotation-config />
<context:component-scan base-package="com.yaozhitech.controller" />
<mvc:annotation-driven />
<bean id="handlerAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlViewvalue>
property>
<property name="prefix">
<value>/WEB-INF/jsp/value>
property>
<property name="suffix">
<value>.jspvalue>
property>
bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
bean>
beans>
添加文件application.properties
# pgsql
jdbc.driver =org.postgresql.Driver
#pgsql链接地址
jdbc.url =jdbc:postgresql://42.121.113.40:5432/zjdw
#pgsql登录名
jdbc.username=zhima
#pgsql密码
jdbc.password=zhima617
#连接数
initSize=30
maxSize=50
添加文件application-common.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:hdp="http://www.springframework.org/schema/hadoop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" 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.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
<context:annotation-config />
<context:component-scan base-package="com.yaozhitech.service" />
<context:component-scan base-package="com.yaozhitech.dao" />
<context:property-placeholder location="classpath*:*.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${initSize}"/>
<property name="maxActive" value="${maxSize}"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select 1"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath:com/yaozhitech/mapper/*Mapper.xmlvalue>
list>
property>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
bean>
<tx:annotation-driven transaction-manager="sitdbTxManager"/>
beans>
总结:本项目详细代码下载地址:http://download.csdn.net/detail/chendaoqiu/8820949