SSM框架环境配置

SSM框架环境配置

一、前置准备工作

(我的环境):附上百度云连接(密码为 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标签下做如下配置

alimaven

aliyun maven

http://maven.aliyun.com/nexus/content/groups/public/

central

nexus-osc

central

Nexus osc

http://maven.oschina.net/content/groups/public/

nexus-osc-thirdparty

thirdparty

Nexus osc thirdparty

http://maven.oschina.net/content/repositories/thirdparty/

验证是否安装成功 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">

4.0.0

com.test

test

1.0-SNAPSHOT

war

4.3.7.RELEASE

3.5.0

org.springframework

spring-webmvc

${org.springframework.version}

org.springframework

spring-jdbc

${org.springframework.version}

org.aspectj

aspectjweaver

1.7.4

mysql

mysql-connector-java

8.0.17

com.alibaba

druid

1.1.20

org.mybatis

mybatis

3.4.2

org.mybatis

mybatis-spring

1.3.1

org.springframework

spring-tx

4.3.7.RELEASE

com.fasterxml.jackson.core

jackson-databind

2.5.0

org.projectlombok

lombok

1.18.0

com.alibaba

fastjson

1.1.39

junit

junit

4.12

log4j

log4j

1.2.14

commons-logging

commons-logging

1.2

org.slf4j

slf4j-api

1.6.1

org.slf4j

slf4j-log4j12

1.6.1

src/main/java

**/*.java

src/main/resources

true

src/main/java

**/*.xml

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

UTF-8

false

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">

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

true

encoding

UTF-8

encodingFilter

/*

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.IntrospectorCleanupListener

contextConfigLocation

classpath*:spring-applicationContext.xml

log4jConfigLocation

classpath:log4j.xml

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath*:spring-mvc.xml

1

springmvc

/

/index.jsp

/index.html

/reg.html

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">

text/html;charset=UTF-8

500

org.springframework.web.servlet.handler.SimpleMappingExceptionResolver

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!

运行测试

三、参考

[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编译时间

相关推荐

中年男人:为什么劝你少戴电子表,多戴机械表?看完这些对比就懂
QQ好友克隆指南:官方方法与安全操作揭秘
365bet取款

QQ好友克隆指南:官方方法与安全操作揭秘

📅 07-12 👁️ 3794
胡子为什么会变黄
bat365在线平台

胡子为什么会变黄

📅 07-04 👁️ 6847