首页 > 代码库 > Spring+Spring MVC+Mybatis+Maven搭建多模块项目(一)
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
接下来创建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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
按创建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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
接下来需要在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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
接下来需要引入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>