写这篇文章的原因
这段时间自己也正是在学习这块,网上找了很多都没有详细的例子,然后基础还不够扎实的原因,花了不少时间才最后跑起来,所以写这篇文章,希望能够帮到入门的兄弟们,希望可以少走一些弯路,当然也有可能看这篇文章的时候就已经走上了弯路(开个玩笑)。同时也希望有大神看到我的这篇文章,能指出不足之处,本人也非常渴望进步以及学到新的东西和思想!
demo需求
这个demo主要的业务逻辑就是:用户通过index.jsp进入登录界面,可以注册会员,会员登录后可以对论文进行简单的增删改查。开发工具
- IntelliJ IDEA Ultimate 2018.1.3.x
- Apache-tomcat-9.0.81
- JDK 1.8.0_172
- MySQL 5.7.22
- Maven 3.3.9
工程结构
首先说明一下我对多模块的项目的理解:
1.多模块是通过各个模块的pom.xml文件联系起来的,继承或依赖关系在pom.xml文件里体现;
2.多个模块中有一个父模块,其他为它的子模块,父模块的pom.xml负责管理所有jar包;
3.web是最“靠外”的一个模块,modle是最“靠内”的一个模块,所以在pom文件的依赖中,web是需要依赖其他的一些特定的模块的,但是modle不需要,相反,很多模块需要依赖他。
最终完成后的工程结构如图:
IDEA+Maven搭建项目骨架
新建数据库:
由于数据库内容设计的非常简单,用的是以前随便做的数据库表,在这里就只直接贴图给大家看看是什么样的了。
大家创建好数据库之后再开始下面的步骤吧~
新建Maven项目:
点击File -> New -> Project ->Empty Project->next
确定后,继续点击File-->new ,如下图,新建一个module
这里因为是要把他作为父模块,没有什么其他特别的操作,所以选择quickstart
这里选择自己的maven
再点击next会需要等一下,maven会自动创建wzy.parent的内部结构
然后再右键wzy.parent,选择moudle,以同样的方式创建wzy.modle,需要注意的是,下图需要按照步骤设置,不然多模块看起来结构会不清晰。
还有下面这一处也要改成wzy.modle
点击next会生成wzy.modle模块。
接下来以wzy.modle一样的顺序,创建wzy.bll、wzy.commons、wzy.dal、wzy.factory。
创建好这些模块之后,我们开始创建wzy.web模块,创建web和其他的不一样,如下图所示(注意:此处不要错选成上面的cocoom-22-archetype-webapp)
所有模块创建完之后需要做下列操作,设置各个模块里面的一些文件夹的属性,如下
设置好之后,每一个模块里面需要建的类及相关文件及代码如下:
wzy.parent:
parent的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.wzy</groupId>
<artifactId>wzy.parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../wzy.modle</module>
<module>../wzy.web</module>
<module>../wzy.factory</module>
<module>../wzy.dal</module>
<module>../wzy.commons</module>
<module>../wzy.bll</module>
</modules>
<name>wzy.parent</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>5.0.3.RELEASE</spring.version>
<mybatis.version>3.4.4</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 第一部分:Spring 配置-->
<!-- Spring core -->
<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>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<vers