最近自己在网上对着博客搭建了一次SSM框架,说难也不难,因为没遇到什么问题。最重要的是一些不容易犯错的错误如果犯了会很麻烦。不过对着错误报告查找的话,还是能查出来的。现在我把流程一步步写出来,供君参考,也加深自己的印象,但示例代码可能跟其他博客雷同(其实都是大同小异的)。请各位同仁见谅。
1.Oracle数据库的创建
我们搭建SSM首先要把数据的来源设计好,然后拿出需要的数据。我使用的数据库是本地创建的数据库(IP地址127.0.0.1)。数据表是USER_T。
CREATE TABLE USER_T(ID NUMBER(11),USER_NAME VARCHAR2(40) ,PASSWORD VARCHAR2(255),AGE NUMBER(4), constraint pk_id primary key(id));
insert into User_t values(1,'测试','123456',24);
2.创建Maven项目
2.1 Maven 开发环境的搭建
2.1.1 下载maven安装包
http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.zip
2.1.2 配置环境变量
新建变量名:MAVEN_HOME 变量值:C:\apache-maven-3.5.0(这是我的MAVEN路径)
编辑变量名:Path 在最前面加上:%MAVEN_HOME%\bin;(注意,最后要有个”;”作为分隔符)
完成之后,在命令行输入:mvn -version查看是否有以下内容,如果有表示配置成功。
2.1.3 配置Maven数据仓库
在路径C:\apache-maven-3.5.0\conf\settings.xml 下
下面是maven默认的国外仓库配置,下载非常慢(默认的注释的,可以删除),所以要改成国内阿里云的仓库
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors>
修改后的
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
2.1.4 Eclipse中集成Maven
2.1.4.1 安装Maven
如果没有此选择则要选择m2eclipse插件安装
选择完成即可,我这里已经添加过,只是演示一下过程。
2.1.4.2 自定义用户设置
我这里为了自己能够理解更清晰,就创建了一个新目录serverMaven用于保存下载的Jar包(多个盘符的话可以省空间,要在Setting.xml中设置,如下图),Setting文件复制出来一份也放在serverMaven(此步骤纯属个人意愿)。
附:m2eclipse插件的安装
2.2 Manen 项目创建
这里我已经添加过了,所以报错,改个名字就好。
创建完成后目录如下
2.2 Manen 导入Jar包
2.2.1 使用Manen 导入Jar包
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xd</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies> -->
<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- 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-oxm</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-jdbc</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</