maven创建多模块项目+springMVC+mybatis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chendaoqiu/article/details/46554139

在平时的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>pom</packaging>

在把文件java Resource下的所有文件删除,因为此项目是做管理,不需要任何代码,只需要pom.xml文件
2.创建子项目movieInvest-utils
在创建好的项目上new一个,选择如下图
这里写图片描述
填写子项目名
这里写图片描述
选择类型
这里写图片描述
一直到完成
打开项目movieInvest-utils的pom.xml
添加一行

<packaging>jar</packaging>

3.创建movieInvest-domain,movieInvest-dao,movieInvest-service
创建这三个子项目都是和创建movieInvest-utils一样,这里就不再讲述了
4.创建movieInvest-web
在创建好的项目上new一个,选择如下图
这里写图片描述
填写项目名
这里写图片描述
选择项目类型
这里写图片描述
点击完成
这里写图片描述
打开pom.xml增加一行

<packaging>war</packaging>

这里我们打开movieInvest项目的pom.xml下,我们观察可以看到增加了几行

<modules>
    <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.yaozhitech</groupId>
      <artifactId>movieInvest-domain</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.yaozhitech</groupId>
      <artifactId>movieInvest-utils</artifactId>
      <version>${project.version}</version>
    </dependency>

service层依赖domain,utils,dao层
在service项目中的pom.xml添加

 <dependency>
      <groupId>com.yaozhitech</groupId>
      <artifactId>movieInvest-domain</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.yaozhitech</groupId>
      <artifactId>movieInvest-utils</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.yaozhitech</groupId>
      <artifactId>movieInvest-dao</artifactId>
      <version>${project.version}</version>
    </dependency>

web层添加service,domain,utils
在web项目中的pom.xml添加

 <dependency>
      <groupId>com.yaozhitech</groupId>
      <artifactId>movieInvest-utils</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.yaozhitech</groupId>
      <artifactId>movieInvest-domain</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.yaozhitech</groupId>
      <artifactId>movieInvest-service</artifactId>
      <version>${project.version}</version>
    </dependency>

5,配置spring+mybatis
在movieInvest项目中的pom.xml中引入jar包
pom.xml配置信息如下

<?xml version="1.0" encoding="UTF-8"?>
<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>com.yaozhitech</groupId>
  <artifactId>movieInvest-root</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>movieInvest-root</name>
  <url>http://maven.apache.org</url>
  <build>
    <finalName>movieInvest</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
  <properties>
        <spring-version>3.2.1.RELEASE</spring-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <commons-lang3.version>3.1</commons-lang3.version>
        <logback.version>0.9.29</logback.version>
        <aspectj.version>1.7.0</aspectj.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.0.5</version>
        </dependency>
         <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis-spring</artifactId>  
            <version>1.0.0</version>  
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1102-jdbc4</version>
        </dependency>
    <dependency>  
            <groupId>commons-net</groupId>  
            <artifactId>commons-net</artifactId>  
            <version>3.3</version>  
        </dependency> 
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.0</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-jexl</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
  </dependencies>
  <modules>
    <module>movieInvest-utils</module>
    <module>movieInvest-dao</module>
    <module>movieInvest-domain</module>
    <module>movieInvest-service</module>
    <module>movieinvest-web</module>
  </modules>
</project>

在web项目中的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_3_0.xsd"
    version="3.0">
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/applicationContext-MVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <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.request.RequestContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <session-config>
        <session-timeout>360</session-timeout>
    </session-config>
</web-app>

在该项目的java resouce中的src/main/resources中添加配置文件
applicationContext-MVC.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.JstlView</value>
        </property>
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </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

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- ====================================================================================== -->
    <!-- 启用基于注解(Annotation-based)的配置 -->
    <!-- ====================================================================================== -->
    <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>
    <!-- ====================================================================================== -->
    <!-- 配置 SqlMapClient -->
    <!-- ====================================================================================== -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations">
            <list>
                <value>classpath:com/yaozhitech/mapper/*Mapper.xml</value>
            </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

展开阅读全文

没有更多推荐了,返回首页