SSM框架即spring、springMVC和mybatis三大框架的整合,其中springMVC作为web项目中的前端控制器,spring作为web项目的地基,mybatis作为web项目的持久层技术。
Java EE应用架构
springMVC就是MVC控制器层的技术,mybatis是DAO层的技术,这几层我就不说了吧!!!基本上都懂。
springMVC整合spring
springMVC项目
选择apache提供的maven-archetype-web创建项目,即如下图:
一直next,创建成功后,创建相应的目录和文件,其目录结构如下所示:
给出pom.xml文件的依赖坐标:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
给出web应用web.xml配置文件内容:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
给出springmvc.xml配置文件内容:
<?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:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置控制层bean扫描-->
<context:component-scan base-package="com.ssm.controller"/>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--开启springmvc注解支持-->
<mvc:annotation-driven/>
</beans>
给出index.jsp访问页内容
<%--
Created by IntelliJ IDEA.
User: 良木
Date: 2020/12/5
Time: 15:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<h3>测试springmvc项目能否运行</h3>
<a href="test">this is test!!!</a>
</body>
</html>
给出pages/success.jsp和error.jsp文件内容:
<%--
Created by IntelliJ IDEA.
User: 良木
Date: 2020/12/5
Time: 15:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success</title>
<title>success</title>
</head>
<body>
<h1>success page!!!</h1>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: 良木
Date: 2020/12/5
Time: 15:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>error</title>
</head>
<body>
<h1>error pages!!!</h1>
</body>
</html>
给出TestController.java代码
package com.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
/**
* 测试项目是否能正常运行
* @return 如果能,返回到success界面,如果不能,则报异常
*/
@RequestMapping("/test")
public String testProject(){
System.out.println("project in running!!!");
return "success";
}
}
配置tomcat服务器,如下:
运行tomcat服务器,运行结果如下:
控制台结果:
D:\tomcat\tomcat9\bin\catalina.bat run
[2020-12-05 04:07:08,626] Artifact 20201205_SSM:war: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE: "C:\Users\良木\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\0dce22a5-5326-4826-83ee-a4a4db4af173"
Using CATALINA_HOME: "D:\tomcat\tomcat9"
Using CATALINA_TMPDIR: "D:\tomcat\tomcat9\temp"
Using JRE_HOME: "D:\Program Files\Java\jdk-11.0.7"
Using CLASSPATH: "D:\tomcat\tomcat9\bin\bootstrap.jar;D:\tomcat\tomcat9\bin\tomcat-juli.jar"
05-Dec-2020 16:07:10.009 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.服务器版本: Apache Tomcat/9.0.35
05-Dec-2020 16:07:10.011 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服务器构建: May 5 2020 20:36:20 UTC
05-Dec-2020 16:07:10.011 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服务器版本号(: 9.0.35.0
05-Dec-2020 16:07:10.011 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 操作系统名称: Windows 10
05-Dec-2020 16:07:10.011 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS.版本: 10.0
05-Dec-2020 16:07:10.012 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 架构: amd64
05-Dec-2020 16:07:10.012 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java 环境变量: D:\Program Files\Java\jdk-11.0.7
05-Dec-2020 16:07:10.012 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java虚拟机版本: 11.0.7+8-LTS
05-Dec-2020 16:07:10.012 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM.供应商: Oracle Corporation
05-Dec-2020 16:07:10.012 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\良木\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\0dce22a5-5326-4826-83ee-a4a4db4af173
05-Dec-2020 16:07:10.012 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: D:\tomcat\tomcat9
05-Dec-2020 16:07:10.014 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:--add-opens=java.base/java.lang=ALL-UNNAMED
05-Dec-2020 16:07:10.015 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:--add-opens=java.base/java.io=ALL-UNNAMED
05-Dec-2020 16:07:10.015 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
05-Dec-2020 16:07:10.017 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Djava.util.logging.config.file=C:\Users\良木\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\0dce22a5-5326-4826-83ee-a4a4db4af173\conf\logging.properties
05-Dec-2020 16:07:10.019 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
05-Dec-2020 16:07:10.019 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dcom.sun.management.jmxremote=
05-Dec-2020 16:07:10.020 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dcom.sun.management.jmxremote.port=1099
05-Dec-2020 16:07:10.020 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dcom.sun.management.jmxremote.ssl=false
05-Dec-2020 16:07:10.020 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dcom.sun.management.jmxremote.password.file=C:\Users\良木\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\0dce22a5-5326-4826-83ee-a4a4db4af173\jmxremote.password
05-Dec-2020 16:07:10.020 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dcom.sun.management.jmxremote.access.file=C:\Users\良木\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\0dce22a5-5326-4826-83ee-a4a4db4af173\jmxremote.access
05-Dec-2020 16:07:10.021 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Djava.rmi.server.hostname=127.0.0.1
05-Dec-2020 16:07:10.021 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Djdk.tls.ephemeralDHKeySize=2048
05-Dec-2020 16:07:10.021 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Djava.protocol.handler.pkgs=org.apache.catalina.webresources
05-Dec-2020 16:07:10.022 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dignore.endorsed.dirs=
05-Dec-2020 16:07:10.022 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dcatalina.base=C:\Users\良木\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\0dce22a5-5326-4826-83ee-a4a4db4af173
05-Dec-2020 16:07:10.022 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Dcatalina.home=D:\tomcat\tomcat9
05-Dec-2020 16:07:10.022 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-Djava.io.tmpdir=D:\tomcat\tomcat9\temp
05-Dec-2020 16:07:10.023 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent 使用APR版本[1.7.0]加载了基于APR的Apache Tomcat本机库[1.2.24]。
05-Dec-2020 16:07:10.023 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR功能:IPv6[true]、sendfile[true]、accept filters[false]、random[true]。
05-Dec-2020 16:07:10.023 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL配置:useAprConnector[false],useOpenSSL[true]
05-Dec-2020 16:07:10.028 信息 [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL成功初始化 [OpenSSL 1.1.1g 21 Apr 2020]
05-Dec-2020 16:07:10.527 信息 [main] org.apache.coyote.AbstractProtocol.init 初始化协议处理器 ["http-nio-8080"]
05-Dec-2020 16:07:10.595 信息 [main] org.apache.catalina.startup.Catalina.load 服务器在[1,002]毫秒内初始化
05-Dec-2020 16:07:10.668 信息 [main] org.apache.catalina.core.StandardService.startInternal 正在启动服务[Catalina]
05-Dec-2020 16:07:10.668 信息 [main] org.apache.catalina.core.StandardEngine.startInternal 正在启动 Servlet 引擎:[Apache Tomcat/9.0.35]
05-Dec-2020 16:07:10.684 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"]
05-Dec-2020 16:07:10.701 信息 [main] org.apache.catalina.startup.Catalina.start [104]毫秒后服务器启动
Connected to server
[2020-12-05 04:07:10,879] Artifact 20201205_SSM:war: Artifact is being deployed, please wait...
05-Dec-2020 16:07:12.500 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。
05-Dec-2020 16:07:12.753 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom 使用[SHA1PRNG]创建会话ID生成的SecureRandom实例花费了[216]毫秒。
05-Dec-2020 16:07:12.966 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean Initializing Servlet 'dispatcherServlet'
05-Dec-2020 16:07:14.042 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean Completed initialization in 1075 ms
[2020-12-05 04:07:14,072] Artifact 20201205_SSM:war: Artifact is deployed successfully
[2020-12-05 04:07:14,072] Artifact 20201205_SSM:war: Deploy took 3,193 milliseconds
05-Dec-2020 16:07:20.690 信息 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [D:\tomcat\tomcat9\webapps\manager]
05-Dec-2020 16:07:20.743 信息 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Web应用程序目录[D:\tomcat\tomcat9\webapps\manager]的部署已在[54]毫秒内完成
web界面:
点击超链接测试,其反馈界面如下所示:
控制台输出:
项目搭建以及测试完毕
spring整合springmvc的关键在于,springMVC的配置文件在web应用启动的时候就已经被加载了,而spring的配置文件如何去加载???
在web应用中,提供了一个工具叫监听器,监听器在应用的生命周期中,web应用启动的时候和其结束的时候可以执行它,所以我们可以利用监听器在web应用前加载spring配置文件即可。
在web.mxl中配置监听器,其web.xml代码如下所示:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置spring的监听器,在web项目被部署前加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--为监听器传入参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springConfig.xml</param-value>
</context-param>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在resource目录下创建一个spring的配置文件,其内容如下:
springConfig.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.liangmu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
注:上面的文档引用说明后面会用到。
创建web应用所需的层包以及各个层所需要的测试文件,其目录结构如下所示:
分别给出各个测试文件代码:
TestController.java
package com.ssm.controller;
import com.ssm.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@Autowired
@Qualifier("testService")
private ITestService testService;
/**
* 测试项目是否能正常运行
* @return 如果能,返回到success界面,如果不能,则报异常
*/
@RequestMapping("/test")
public String testProject(){
System.out.println("project in running!!!");
return "success";
}
/**
* 测试spring和springMVC的整合
* @return 如果能,返回到success界面,如果不能,则报异常
*/
@RequestMapping("/testController")
public String testController(){
System.out.println("controller: I'm running!!!");
testService.testService();
return "success";
}
}
TestDao.java
package com.ssm.dao.impl;
import com.ssm.dao.ITestDao;
import org.springframework.stereotype.Repository;
@Repository("testDao")
public class TestDao implements ITestDao {
@Override
public void testDao() {
System.out.println("dao: i'm running !!!");
}
}
ITestDao.java
package com.ssm.dao;
public interface ITestDao {
void testDao();
}
TestService.java
package com.ssm.service.impl;
import com.ssm.dao.ITestDao;
import com.ssm.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service("testService")
public class TestService implements ITestService {
@Autowired
@Qualifier("testDao")
private ITestDao testDao;
public void testService(){
System.out.println("service: I‘m running !!!");
testDao.testDao();
}
}
ITestService.java
package com.ssm.service;
public interface ITestService {
void testService();
}
重写index.jsp文件
<%--
Created by IntelliJ IDEA.
User: 良木
Date: 2020/12/5
Time: 15:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<h3>测试springmvc项目能否运行</h3>
<a href="test">this is test!!!</a>
<h3>测试springmvc和spring的整合</h3>
<a href="testController">spring + springmvc test!!!</a>
</body>
</html>
热部署tomcat,返回主页面如下所示:
点击第二个超链接,查看idea的控制台,如下所示:
返回成功界面就不给出了,麻烦。
由上知,springmvc和spring的整合完成。
下一节给出spring整合mybatis,地址:spring整合mybatis