Spring+Spring MVC+Mybatis+Maven搭建多模块项目(一)

来源:互联网 发布:seo外包北京华网 编辑:程序博客网 时间:2024/05/20 03:44

最近在研究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=truejdbc.username=rootjdbc.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搭建多模块项目(二)》

0 0
原创粉丝点击