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="new_*" propagation="REQUIRES_NEW" rollback-for="Exception" />
    			<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的事务回滚不了;
REQUIRES_NEW表示新事务,即自治事务,如Service A中调Service B的方法完成后,Service B方法的事务要立马提交,则B方法的命名规则为:new_*,如new_saveLog()

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中设置拦截所有请求,这时候就有一个问题,静态资源也会被拦截,因此我就使用mvc:resources/标签指定静态资源路径,凡是到指定路径下的文件,都不会被拦截,这只是其中一种处理方式,还有另外一种是启用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搭建多模块项目(二)

  • 4
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值