一、前置准备工作
(我的环境):附上百度云连接(密码为 k5lm) SSM项目所需
IDE: IDEA 2020.1.3 (Ultimate Edition)
项目管理工具: MAVEN 3.6.3
JAVA环境: JDK 1.8.0_271
Web服务器: Tomcat 9.0.41
数据库: MySQL 8.0.22
数据库管理工具: Navicat 12.1.16
1.1. jdk环境安装
下载地址:jdk8,环境变量的配置去网上搜一下,如windows环境中JDK环境变量配置。
1.2. MySQL安装
下载地址:MySQL,安装教程见MySQL下载与安装。
将 MySQL下的 bin 文件夹加入环境变量。
在 MySQL文件夹下 添加 data 文件夹和 mysql.ini配置文件。
打开cmd(或者powershell),不需要进入安装目录(∵之前配置过环境变量),输入下面命令,ps回车后并没有反应。
mysqld --initialize-insecure --user=mysql
安装及启动
mysqld install
net start mysql
服务启动成功之后,需要登录的时候输入命令(第一次登录没有密码,直接按回车过)进入后 exit 退出。
mysql -u root -p
修改密码(cd 至 bin 目录下),执行如下命令回车,enter password也回车,密码一般设置为root,方便记忆。之后 exit 退出。
mysqladmin -u root -p password
Navicat图形化界面连接mysql,新建连接
填写连接ip和密码,并测试连接
1.3. Tomcat安装
下载地址:Tomcat,下载完后直接解压到 D:\Program Files\apache-tomcat-9.0.41 下。
这里我原先放在C:\Program Files\apache-tomcat-9.0.41下,但是在 IDEA 运行时会产生没有权限生成写入日志的错误,需要在去赋给权限,故这里推荐放在 D 盘。
1.4. Maven安装
下载地址:Maven,下载完成后解压到你想放的文件夹,接下来配置环境变量。
配置环境变量:MAVEN_HOME=“maven安装目录”; 配置 bin 目录。
配置 Maven
配置本地仓库位置
配置aliyun公共仓库,在mirrors标签下做如下配置
验证是否安装成功 mvn -v
1.5. IDEA安装
下载地址:Idea,直接安装就好,推荐最终版!
二、SSM项目搭建
多图介绍!
2.1. 创建项目
新建项目:①选择 Maven -> ②选择 JAVA 环境 -> ③选择 webapp.(记得勾选 Create from archtype)
进入到项目名字配置和 GAV 名字配置界面:
Name:根据自己的需要填写项目名称
Location:同样是根据需要选择项目存放的位置
Artifact Coordinates:点击小三角即可出现 GAV 的配置,一般只需要配置 GroupId 和 ArtifactId
注意:GroupId一般是公司域名的反写,就和 java 的 package 类似。ArtifactId 需要和设置的项目名称一样,否则会报错
设置Maven home directory,一般我们不选择idea自带的,因为太容易出错了。最重要的就是User settings file和Local repository这两项,需要改变记得勾上 Override。
第一个是用户配置文件,在 settings.xml 文件里我们设置了仓库的路径,也设置了阿里云等国内的镜像仓库。我们不需要把maven安装目录里 conf 文件夹的 settings.xml 文件导入,maven会自己寻找这个 settings 配置文件(PS 如果不行的话还是手动导入吧)
成功创建后的项目列表,接下来完善项目。
如果左下角出现以下问题那就重启一下 IDEA 吧!
新建 java 目录和 resources 目录
我是分割线---
此时创建完毕目录如下,接下来配置 Tomcat
2.2. 配置Tomcat
添加 Tomcat
我是分割线--- 之后其他配置不用动;
Tomcat Deployment 配置完成 ok 就完事了
我是分割线---
运行tomcat 右上角面板选择debug模式运行(也可以普通模式)
我是分割线--- ps:8080端口后是在第1步时配置的
2.3. 配置spring容器等相关配置文件
1. poom.xml
在poom.xml(位置:项目的根目录下)中引入必要的poom依赖(其中包括了spring、springmvc、jdbc、mysql、druid连接池、mybatis、lombok插件、juni他、其他工具包 )
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">
2. 完事刷新。
3. web.xml
(位置:webapp下的WEB-INF中)
tomcat启动时,会加载此文件,从而扫描加载其他的配置文件
其中包括了编码过滤、监听器、springmvc配置、spring容器中其他配置包括mybatis等、log4j配置、项目访问默认欢迎页,如需添加自定义过滤器,也许在此配置
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> classpath*:spring-applicationContext.xml classpath:log4j.xml
4. 新建其他配置文件
(位置:resoures下) applicationContext.xml、springmvc.xml、mybatis.xml、jdbc.properties、日志配置(log4j.properties和log4j.xml二选一,我用的log4j.xml)
4.1 spring-mvc.xml
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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
4.2 spring-applicationContext.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
4.3 spring-mybatis.xml
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
4.4 mybatis-config.xml
"http://mybatis.org/dtd/mybatis-3-config.dtd">
4.5 jdbc.properties
#mysql数据库配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
##Druid配置
#初始化大小
jdbc.initialSize=3
#连接池最大使用连接数量
jdbc.maxActive=1000
#连接池最小空闲
jdbc.minIdle=0
#最大建立连接等待时间
jdbc.maxWait=6000
#是否自动回收超时连接
jdbc.removeAbandoned=true
#超时时间(以秒数为单位)
jdbc.removeAbandonedTimeout=1800
#失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程 (毫秒)
jdbc.timeBetweenEvictionRunsMillis=60000
#大于0 ,进行连接空闲时间判断,或为0,对空闲的连接不进行验证;默认30分钟 (毫秒)
jdbc.minEvictableIdleTimeMillis=25200000
#验证连接是否有效的SQL文
jdbc.validationQuery=select getdate()
#空闲时是否进行验证,检查对象是否有效,默认为false
jdbc.testWhileIdle=false
#取得对象时是否进行验证,检查对象是否有效,默认为false
jdbc.testOnBorrow=false
#返回对象时是否进行验证,检查对象是否有效,默认为false
jdbc.testOnReturn=false
4.6 log4j.xml
2.4. 编写测试代码
经过以上的步骤,SSM框架环境算是配好了,接下来我们用一个实例演示一下。
首先建立如图所示的文件结构
1. TestController.java
接口逻辑层controller.
package com.test.controller;
import com.alibaba.fastjson.JSONObject;
import com.test.model.TpmTestLog;
import com.test.service.TpmTestLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class TestController {
@Autowired
private TpmTestLogService tpmTestLogService;
@GetMapping("/test")
public JSONObject test(){
JSONObject jsonObject = new JSONObject();
TpmTestLog tpmTestLog = new TpmTestLog();
tpmTestLog.setSyscreatedate("asfasgags");
tpmTestLog.setTestmainUuid("fdasgassfsarew");
tpmTestLog.setOperation("新增");
tpmTestLogService.ceshi(tpmTestLog);
jsonObject.put("title", "测试成功" + tpmTestLog.getLogUuid());
return jsonObject;
}
}
2. TpmTestLogMapper.java
数据库交互层dao接口.
package com.test.mapper;
import com.test.model.TpmTestLog;
/**
* @TpmTestLogMapper
* @Mapper
* @version : Ver 1.0
*/
public interface TpmTestLogMapper {
Long insert(TpmTestLog tpmTestLog);
}
3. TpmTestLogMapper.xml
数据库交互对应的xml.
LOG_UUID AS logUuid,
TESTMAIN_UUID AS testmainUuid,
OPERATION AS operation,
SYSCREATEDATE AS syscreatedate
INSERT INTO TPM_TEST_LOG (LOG_UUID,
TESTMAIN_UUID,
OPERATION,
SYSCREATEDATE)
VALUES (#{logUuid},
#{testmainUuid},
#{operation},
#{syscreatedate})
4. TpmTestLog.java
实体模型.
package com.test.model;
import lombok.Data;
import java.io.Serializable;
/**
* @TpmTestLog
* @(TPM_TEST_LOG)
* @version : Ver 1.0
*/
@Data
public class TpmTestLog implements Serializable {
/**
* @备注: 主键
* @字段:LOG_UUID bigint
*/
private Long logUuid;
/**
* @备注:
* @字段:TESTMAIN_UUID CHAR(32)
*/
private String testmainUuid;
/**
* @备注:
* @字段:OPERATION VARCHAR(20)
*/
private String operation;
/**
* @备注:
* @字段:SYSCREATEDATE VARCHAR(25)
*/
private String syscreatedate;
public Long getLogUuid() {
return logUuid;
}
public void setLogUuid(Long logUuid) {
this.logUuid = logUuid;
}
public String getTestmainUuid() {
return testmainUuid;
}
public void setTestmainUuid(String testmainUuid) {
this.testmainUuid = testmainUuid;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getSyscreatedate() {
return syscreatedate;
}
public void setSyscreatedate(String syscreatedate) {
this.syscreatedate = syscreatedate;
}
}
5. TpmTestLogService.java
业务层接口类sercice.
package com.test.service;
import com.test.model.TpmTestLog;
/**
* @TpmTestLogService
* @Service
* @version : Ver 1.0
*/
public interface TpmTestLogService{
Long ceshi(TpmTestLog tpmTestLog);
}
6. TpmTestLogServiceImpl.java
业务层的实现类impl.
package com.test.service.impl;
import com.test.mapper.TpmTestLogMapper;
import com.test.model.TpmTestLog;
import com.test.service.TpmTestLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @TpmTestLogServiceImpl
* @ServiceImpl
* @version : Ver 1.0
*/
@Service
@Transactional
public class TpmTestLogServiceImpl implements TpmTestLogService {
@Autowired
private TpmTestLogMapper tpmTestLogMapper;
@Override
public Long ceshi(TpmTestLog tpmTestLog) {
return tpmTestLogMapper.insert(tpmTestLog);
}
}
7. 数据库表建立
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `TPM_TEST_LOG`
-- ----------------------------
DROP TABLE IF EXISTS `TPM_TEST_LOG`;
CREATE TABLE `TPM_TEST_LOG` (
`LOG_UUID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`TESTMAIN_UUID` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`OPERATION` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`SYSCREATEDATE` varchar(25) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`LOG_UUID`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
SET FOREIGN_KEY_CHECKS = 1;
8. 测试接口
9. 附上可能的XML乱码问题解决
2.5. 测试前端页面和接口交互测试
改动 index.jsp
Hello World!
window.onload = function () {
//步骤一:创建异步对象
let ajax = new XMLHttpRequest();
//步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
ajax.open('get', '/final01_war_exploded/api/test');
//步骤三:发送请求
ajax.send();
//步骤四:注册事件 onreadystatechange 状态改变就会调用
ajax.onreadystatechange = function () {
if (ajax.readyState == 4 && ajax.status == 200) {
//步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
console.log(ajax.responseText);//输入相应的内容
console.log(JSON.parse(ajax.responseText).title);
console.log(document.getElementsByTagName("h2")[0])
document.getElementsByTagName("h2")[0].innerText = JSON.parse(ajax.responseText).title;
}
}
}
运行测试
三、参考
[0] idea搭建简单ssm框架的最详细教程(新)
[1] IDEA创建maven项目踩坑指南
[2] Maven安装配置和目录结构详解(适合了解Maven结构)
[3] maven全局配置文件settings.xml详解
[4] Maven安装(适合安装教程)
[5] idea中的xml文件的中文乱码问题
[6] 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间