一、新建一个Maven项目

1.

2.不使用骨架,直接next

3.(项目名称、工作空间、分组Id设置。按照个人喜好即可,不推荐使用中文) -》 直接Finish

4.看到如下图的pom.xml文件和左侧的目录结构即代表项目创建成功(现在目录结构还不完整,下一步需要补全目录)

5.打开项目结构 -》 Modules -》点击+号 -》 往下拉找到’web'

6.按照顺序进行修改。将webapp文件添加到src\main\目录下

7.在webapp\WEB-INF\目录下新建一个web.xml文件(web项目的核心文件)

web.xml文件的代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--    你配置的内容放这里--></web-app>

8.在webapp目录下新建一个index.jsp文件

index.jsp内容如下:

<%--Created by IntelliJ IDEA.User: AdministratorDate: 2021/11/7Time: 15:14To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body></body>
</html>

至此,项目的目录结构补充完毕!接下来在pom.xml中导入相关依赖坐标

二、导入相关坐标

<dependencies><!--spring相关--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version></dependency><!--servlet和jsp--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version></dependency><!--mybatis相关--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><finalName>ssm-test</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.0.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.20.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.0</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>

三、在java文件下新建通用的包结构:

controller:界面层,前端控制器。里面的类用于接收客户端请求

service:业务逻辑层

mapper:数据访问层,里面可以定义各种接口和接口对应的xml文件,用于访问数据库,进行CRUD操作

pojo:数据库表对应的实体类

四、编写配置文件

<u>注意:以下配置,涉及到使用包名的地方,各位小伙伴需要按照自己项目需求进行对应的包名的修改</u>

1.jdbc.properties配置文件的编写

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
jdbc.username=xxx
jdbc.password=xxx

2.spring核心配置文件的编写(这里,我将核心配置文件按照配置功能分为了两个:applicationContext_dao.xml、applicationContext_service.xml)

applicationContext_dao.xml:配置spring对于dao层的支持

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--    读取properties文件--><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><!--    创建数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--    创建sqlSessionFactoryBean--><bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        配置数据源--><property name="dataSource" ref="dataSource"></property>
<!--                  配置mybatis核心配置文件--><property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<!--        配置实体类别名--><property name="typeAliasesPackage" value="com.msn.pojo"></property></bean><!--    创建mapper文件扫描器--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.msn.mapper"></property></bean></beans>

其中,实体类别名、mapper文件扫描器中的value需按照自己的包目录决定

applicationContext_service.xml:配置spring对于service层的支持

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--    1.设置业务逻辑层包扫描器,目的是在指定的包下,使用@service注解的类,spring负责创建对象,并添加依赖--><context:component-scan base-package="com.msn.service"></context:component-scan><!--    2.设置事务管理器--><bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--        配置数据源--><property name="dataSource" ref="dataSource"></property></bean><!--    3.设置事务的切面(或增强)--><tx:advice id="advice" transaction-manager="dataSourceTransactionManager"><tx:attributes><!--设置哪些方法需要进行事务管理,propagation是事务的传播特性--><!--查询方法--><tx:method name="select*" read-only="true"/><tx:method name="find*" read-only="true"/><tx:method name="search*" read-only="true"/><tx:method name="get*" read-only="true"/><!--增加方法--><tx:method name="save*" propagation="REQUIRED"></tx:method><tx:method name="add*" propagation="REQUIRED"></tx:method><tx:method name="insert*" propagation="REQUIRED"></tx:method><!--更新方法--><tx:method name="update*" propagation="REQUIRED"></tx:method><tx:method name="change*" propagation="REQUIRED"></tx:method><!--删除方法--><tx:method name="delete*" propagation="REQUIRED"></tx:method><!--当以上方法都不匹配时:设置为支持事务--><tx:method name="*" propagation="SUPPORTS"></tx:method></tx:attributes></tx:advice><!--    4.设置切面和切点的织入--><aop:config>
<!--        配置切点--><aop:pointcut id="pointcut" expression="execution(* com.msn.service.*.*(..))"/>
<!--        织入--><aop:advisor advice-ref="advice" pointcut-ref="pointcut"></aop:advisor></aop:config></beans>

注意:配置到这一步,你会惊喜的发现applicationContext_service.xml中数据源的配置:

<property name="dataSource" ref="dataSource"></property>

中,dataSource爆红。原因是在applicationContext_service.xml中无法找到一个id为dataSource的Bean,因为该Bean是我们在applicationContext_dao.xml中配置的,当我们之后在web.xml中将各个配置文件注册(加载)进web容器时,该问题就得到解决,这里的编译错误忽略即可。

3.springmvc核心配置文件的编写

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"><!--    设置包扫描器-->
<context:component-scan base-package="com.msn.controller"></context:component-scan><!--    设置视图解析器--><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--        设置前缀和后缀-->
<!--        注意:当在controller中返回的字符串有forward(请求转发)或者是redirect(重定向)时,不使用设置好的前缀和后缀--><property name="prefix" value="/admin/"></property><property name="suffix" value=".jsp"></property></bean><!--    设置文件上传核心组件,注意:id的值一定要是commonsMultipartResolver,不能是其他--><bean id="commonsMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
<!--    设置注解驱动--><mvc:annotation-driven></mvc:annotation-driven>
</beans>

4.设置Mybatis核心配置文件sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>
<!--    分页插件配置--><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin></plugins></configuration>

值得一提的是,在applicationContext_dao.xml中已经进行了数据源的配置,因此在Mybatis核心配置文件sqlMapConfig.xml中就不再需要进行。这里我进行了一个分页插件的配置,因为在SSM的web项目中,分页是经常遇到的需求,各位小伙伴根据自己的需求来决定是否配置。

至此,SSM配置文件的搭建已经完成,有需要的小伙伴可以进行日志文件log4j的配置!

五、SSM配置文件的注册(加载在web.xml下)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--    1.设置字符编码过滤器(解决乱码问题)--><filter><filter-name>encode</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><!--属性参数设置--><init-param><!--设置encoding编码集--><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><!--设置请求--><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><!--设置响应--><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><!--过滤任何请求--><filter-mapping><filter-name>encode</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--2.注册springMVC框架--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param></servlet><servlet-mapping><!--拦截任何.action结尾的--><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping><!--3.注册spring框架--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext_*.xml</param-value></context-param>
</web-app>

web.xml分析:

1.首先进行请求响应的字符编码的统一,这里使用UTF-8(推荐使用),客户端的所有请求都将被拦截

2.然后注册springMVC框架:

--classpath:springmvc.xml这里classpath后的值是你自己配置的springmvc核心配置文件的文件名,本人项目中,springMVC核心配置文件就叫springmvc。

-- <url-pattern>*.action</url-pattern>:这里的*.action表示客户端的请求地址,只要是以action结尾的,都将被springMVC前端控制器拦截,并且在controller包下的对应类中的对应方法里,进行请求的处理以及响应。

3.最后注册spring框架:

<param-value>classpath:applicationContext_*.xml</param-value>中的classpath:applicationContext_*.xml表示:所有在类路径(src\main\resources)下的以applicationContext_打头,xml结尾的配置文件,都将被注册,这里符合该条件的文件有两个:applicationContext_dao.xml和applicationContext_service.xml。这样,两个spring配置文件都被加载进web容器,此时applicationContext_service.xml中的dataSource爆红的情况得到解决。

至此,Web.xml文件的配置告一段落!

补充:

一、IDEA集成Maven

file -> setting -> maven

注意:如果maven中的核心配置文件没有使用阿里云私服地址,建议在setting中进行配置:

<mirrors>
     <mirror>  
          <id>alimaven</id>  
          <name>aliyun maven</name>  
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>          
    </mirror>
  </mirrors>

二:Tomcat服务器的配置

再次打开:

然后apply -> ok即完成项目部署到tomcat服务器

好了,SSM框架的环境搭建到此结束,接下来开始你的coding表演吧~~

通用SSM项目环境搭建详细教程相关推荐

  1. Win10 Anaconda下TensorFlow-GPU环境搭建详细教程(包含CUDA+cuDNN安装过程)(转载)...

    win7(win10也适用)系统安装GPU/CPU版tensorflow Win10 Anaconda下TensorFlow-GPU环境搭建详细教程(包含CUDA+cuDNN安装过程) 目录 2.配置 ...

  2. Kubernetes集群环境搭建详细教程(一主两从)

    Kubernetes集群环境搭建详细教程(一主两从) 1.1 安装要求 在开始之前,部署Kubernetes 集群机器需要满足以下几个条件: 一台或多台机器,操作系统CentOS7.x-86_x64 ...

  3. win0php环境搭建,win10php环境搭建详细教程

    大家在将系统更新为Win10系统后,对php环境搭建方法并不是很清楚.那么win10php环境搭建要怎么操作呢?其实win10php环境搭建的方法跟Win7/Win8.1系统的php环境搭建是相同道理 ...

  4. 1 Go语言开发环境搭建详细教程+go常见bug合集【Go语言教程】

    Go语言开发环境搭建[Win.Linux.Mac] 1 SDK下载 官网地址:golang.org,因为一些原因国内可能无法访问.可以使用下面第二个链接. 国内地址访问:https://golang. ...

  5. Rust语言开发环境搭建详细教程

    目录 一.Rust简介 1.rust历史 2.rust吉祥物 二.Rust开发环境搭建 1.C++环境安装 2.Rust下载 3.rust安装 4.rust环境检测 查看rust版本 查看cargo版 ...

  6. PHP开发环境搭建详细教程

    首先你需要安装Apache Server,这里以当前最新版Apache Server2.4为例进行说明,可惜官网并没有提供最新版的msi安装包,连编译后的binary二进制压缩包都没提供,不过我已经编 ...

  7. 微信小程序 - Vant weapp UI 框架环境搭建详细教程(Window)

    前言 强烈推荐您打开 官方文档,对照着本教程一起对比搭建坏境. 自从 2022 年开始,小程序做了很多改变和升级, 导致网上很多搭建教程文章的教程失效了,本文来做最新的教程. 第一步 为了更贴合新手, ...

  8. Win10 Anaconda下TensorFlow-GPU环境搭建详细教程(包含CUDA+cuDNN安装过程)

    目录 前言 第一步:安装Anaconda 1.下载和安装 2.配置Anaconda环境变量 第二步:安装TensorFlow-GPU 1.创建conda环境 2.激活环境 3.安装tensorflow ...

  9. 2、Ubuntu介绍加环境搭建详细教程

    一.简介 1.1嵌入式: 嵌入式主要学习的就是一个系统,如何使用系统,如果把软件嵌入到硬件设备 物联网:万物互联,学习物联网就是学习如何将多个设备之间连接 蓝牙.wifi.NBIOT.ZIGBEE.5 ...

最新文章

  1. server缺少sqlexpress sql_SQL Server----解决SQL Server 配置管理器不见了
  2. LINUX下查看主机信息
  3. 笔记本移交_创建完美的设计移交
  4. mysql 固定符号分列显示_MySql中指定符号分割并分行展示
  5. 流量和延迟减半!挑战 TiDB 跨数据中心难题
  6. 盘点前 10 名的免费跨浏览器测试工具
  7. matlab 声卡输出,请问高手关于matlab控制声卡输出的问题
  8. 2019年前端开发工作总结
  9. 陈佩斯曾受邀喜剧综艺:被酬劳吓的恍惚好几天
  10. 「鹿班智能设计平台」是如何工作的
  11. mysql免安装版修改密码
  12. 非典型文字描边效果的实现方法
  13. python随机生成二维列表_对python产生随机的二维数组实例详解
  14. (01)ORB-SLAM2源码无死角解析-(57) 闭环线程→计算Sim3:理论推导(2)求解R,使用四元数
  15. 采集到博客数据的10个经典方法
  16. Linux系统网络桥接
  17. zookeeper集群模式(十)zookeeper的lead流程
  18. UVALive 7139 - Rotation
  19. Foxit PDF 福昕PDF 阅读器下载慢怎么办
  20. Citrix Virtual PC

热门文章

  1. 直击用户大脑——用户研究新方法(眼动与脑电数据分析)
  2. Content Delivery Networks CDN 内容分发网络
  3. Java加密1-散列函数
  4. IPv4子网划分与聚合
  5. 微信开发者工具(一)
  6. 【区块链技术与应用】(五)
  7. MikTex 和 TexStudio 输入中文日文
  8. kubernetes 部署_kubernetes应用程序部署工具概述
  9. 二分查找法--有序表
  10. 201609-3 炉石传说