1. 程式人生 > >Spring+Spring MVC+Mybatis+Maven搭建多模組專案(一)

Spring+Spring MVC+Mybatis+Maven搭建多模組專案(一)

最近在研究Spring MVC和Maven,工作中也是使用Spring MVC、Mybatis及Maven整合,出於好奇,自己也搭建了一個Spring+Spring MVC+Mybatis+Maven的多模組框架,先介紹一下我的工程結構
bug.root:根模組,不包含任何程式碼
bug.model:物件模組,所有的實體物件都放到這裡
bug.dao:資料庫互動模組,Mybatis的mapper.xml檔案及Dao層的介面就寫到這裡
bug.service:業務模組,業務介面及其實現都放到這裡,事務控制也是在Service中
bug.web:控制層,頁面傳送的請求,都通過這裡接收和響應
bug.common

:公共模組,一些公共元件,工具類都放到這個模組中

下面開始模組的建立,首頁需要建立bug.root根模組,後面所有的模組都是基於根模組建立的
右鍵->new->選擇Maven Project,點選下一步,把Create a simple project勾上,
這裡寫圖片描述
點選下一步,輸入Group Id:com.bug,Artifact Id:bug.root,Packaging:pom,如下圖
這裡寫圖片描述

點選finish按鈕生成bug.root專案,把bug.root目錄下的其他檔案都刪除,只保留pom.xml檔案,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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bug</groupId> <artifactId>bug.root</artifactId
>
<version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> </project>

接下來建立bug.model模組,右鍵new->Maven Module,在開啟的頁面中輸入Module Name:bug.model,Parent Project:bug.root
這裡寫圖片描述
點選下一步,在Select an Archetype中選擇maven-archetype-quickstart,點選next,輸入Group Id:,Package:com.bug.model然後點選finish
這裡寫圖片描述
修改bug.model模組下的pom.xml檔案,修改後內容如下

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.bug</groupId>
    <artifactId>bug.root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>bug.model</artifactId>
  <name>bug.model</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

按建立bug.model模組的方式,建立bug.dao,bug.service,bug.common模組

由於bug.web模組和其他模組有點不一樣,所以拿出來單獨說明一下
右鍵->new->Maven Module->在開啟的頁面中輸入Module Name:bug.web,Parent Project:bug.root
這裡寫圖片描述
點選下一步,在Select an Archetype中選擇maven-archetype-webapp(這裡就是不一樣的地方)
這裡寫圖片描述
點選下一步,輸入Group Id:com.bug,Package:com.bug.web,點選Finish完成
這裡寫圖片描述

到這裡,整個專案結構就建立完成了,工程結構如下
這裡寫圖片描述

bug.root根模組下的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.bug</groupId>
  <artifactId>bug.root</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>bug.model</module>
    <module>bug.dao</module>
    <module>bug.service</module>
    <module>bug.common</module>
    <module>bug.web</module>
  </modules>
</project>

接下來需要在pom.xml檔案中設定模組之間的依賴關係
bug.dao:依賴於bug.model
bug.service:依賴於bug.model、bug.dao、bug.common
bug.web:依賴於bug.service、bug.model、bug.common,由於Maven的依賴關係是傳遞性的,因此bug.web只需要依賴於bug.service,就會間接的依賴bug.model、bug.dao、bug.common,修改後各模組的pom.xml檔案內容如下

bug.dao的pom.xml檔案

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.bug</groupId>
    <artifactId>bug.root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>bug.dao</artifactId>
  <name>bug.dao</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.bug</groupId>
      <artifactId>bug.model</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>

bug.service的pom.xml檔案

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.bug</groupId>
    <artifactId>bug.root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>bug.service</artifactId>
  <name>bug.service</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
        <groupId>com.bug</groupId>
        <artifactId>bug.model</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.bug</groupId>
        <artifactId>bug.dao</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.bug</groupId>
        <artifactId>bug.common</artifactId>
        <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>

bug.web的pom.xml檔案

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.bug</groupId>
    <artifactId>bug.root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>bug.web</artifactId>
  <packaging>war</packaging>
  <name>bug.web</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
        <groupId>com.bug</groupId>
        <artifactId>bug.model</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.bug</groupId>
        <artifactId>bug.service</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.bug</groupId>
        <artifactId>bug.common</artifactId>
        <version>${project.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>bug.web</finalName>
  </build>
</project>

接下來需要引入Spring+Spring MVC+Mybatis相關Jar包到專案中,由於我使用的是Maven,因此就不需要將Jar包放到在WEB-INF/lib下了,Maven會自己管理Jar包,但是jar包的依賴放到哪個模組下的pom.xml檔案中呢,由於bug.root是所有模組的根模組,因此jar包的依賴需要放到bug.root根模組下的pom.xml檔案中,最後完整的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.bug</groupId>
  <artifactId>bug.root</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>bug.model</module>
    <module>bug.dao</module>
    <module>bug.service</module>
    <module>bug.common</module>
    <module>bug.web</module>
  </modules>

  <properties>
    <spring.version>4.0.6.RELEASE</spring.version>
    <mybatis.version>3.2.7</mybatis.version>
    <mysql.version>5.1.29</mysql.version>
  </properties>

  <dependencies>
    <!-- Spring包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</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-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- mybatis驅動包 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.2</version>
    </dependency>

    <!-- mysql驅動包 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <!-- dbcp資料來源 -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.6</version>
    </dependency>
    <dependency>
    <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>

    <!-- junit測試包 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>

    <!-- json資料 -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <!-- log4j日誌包 -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>

    <!-- jstl 標籤庫 -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>bug.root</finalName>
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <version>2.3.2</version>  
            <configuration>  
                <source>1.7</source>  
                <target>1.7</target>  
            </configuration>  
        </plugin>  
    </plugins> 
  </build>
</project>

接下來需要配置spring mvc需要的支援檔案spring-servlet.xml及spring和mybatis整合需要的applicationContext.xml配置檔案
在bug.web模組中的resources資原始檔夾下建立config目錄,在目錄中建立以下檔案
applicationContext.xml
jdbc.properties
log4j.properties – 內容暫時可以不寫,留空
spring-servlet.xml

applicationContext.xml是Spring及Mybatis整合的配置檔案,資料庫的資料來源,事務控制,Service層,Dao層的掃描都到這裡配置,完整的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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- jdbc配置 -->
    <context:property-placeholder location="classpath:config/jdbc.properties" />

    <!-- 自動將Service層注入-->
    <context:component-scan base-package="com.bug.service" />

    <!-- dbcp資料來源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!--這裡如果寫成${jdbc.driver},就會出現載入jdbc驅動失敗的問題,暫時不清楚什麼原因-->         
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />         
        <property name="url" value="${jdbc.url}" />         
        <property name="username" value="${jdbc.username}" />         
        <property name="password" value="${jdbc.password}" />   

        <property name="maxActive" value="20" /> 
        <property name="maxIdle" value="10"/>
        <property name="maxWait" value="10000" />
        <property name="defaultAutoCommit" value="false"/>     
    </bean>

    <!-- mybatis的配置檔案 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/bug/dao/*/*.xml"/>
    </bean>

    <!-- spring與mybatis整合配置,掃描所有dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.bug.dao"/>
    </bean>

    <!-- 事務 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>

            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>

            <tx:method name="*" propagation="REQUIRED" />
            <tx:method name="*" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.bug.service.*.impl..*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
</beans>           

從上面的配置可看出,事務在Servie層控制,如果Service層成功,在Controller層異常,事務一樣會提交,這裡要強調一點,如果是Service A調Service B,要做到B的失敗不影響A事務的提交,只需要在B異常時,在A中try…catch掉,異常不拋給Controller層就可以了,但是要注意,B失敗後事務要回滾,在B中異常,異常一定要往外拋,不然B的事務回滾不了

jdbc.properties是資料庫連,賬號,密碼配置檔案,內容如下

jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://127.0.0.1:3306/bigmouth?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
jdbc.username=root
jdbc.password=123456

spring-servlet.xml是mvc控制層的配置檔案,控制層的掃描,靜態資原始檔,檢視解析器都到這裡設定,完整的spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 掃描controller(controller層注入) -->
    <context:component-scan base-package="com.bug.controller"/>

    <!-- 啟動註解支援 -->  
    <mvc:annotation-driven />

    <!-- 靜態資源 -->
    <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
    <mvc:resources location="/WEB-INF/image/" mapping="/image/**"/>

    <!-- 檢視解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>          

這裡說明一下,由於我使用的是Spring的Restful風格,URL路徑不需要有.do,.action之類的後輟,因此在web.xml中設定攔截所有請求,這時候就有一個問題,靜態資源也會被攔截,因此我就使用標籤指定靜態資源路徑,凡是到指定路徑下的檔案,都不會被攔截,這只是其中一種處理方式,還有另外一種是啟用servlet-default,在web.xml中指定不需要攔截的檔案

檢視解析器指定了只能以.jsp結尾的檔案為渲染檔案,並且所有的jsp檔案都需要放到/WEB-INF/jsp/目錄下才能被渲染

最後一步,修改web.xml檔案,修改後的web.xml檔案內容如下

<!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>Archetype Created Web Application</display-name>

    <!-- Spring配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Spring MVC -->
     <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/spring-servlet.xml</param-value>
        </init-param>
    </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>

到這裡,整個配置就完成了,接下來寫一個示例,從頁面傳送請求到Controller –>service–>Dao層,體驗一下MVC的強大之處,請看《Spring+Spring MVC+Mybatis+Maven搭建多模組專案(二)