当前位置:网站首页>Full stack development practice | complete tutorial of SSM framework integration
Full stack development practice | complete tutorial of SSM framework integration
2022-07-21 14:37:00 【Lao Yang didn't lose his hair】
“ One's best state : Dreams are hidden in my heart , Action falls on the legs .”
Catalog
3、 Development environment construction
3.3 add to SpringMVC Framework support
3.4 To configure SpringMVC Core controller
4、 Deploy Tomcat And related environment
1、 Preface
The previous several articles in my series introduced MyBatis、Spring、SpringMVC Basic introduction to the framework and some common knowledge points , Since each framework has its own advantages , So can we integrate these frameworks ?
Now let's teach you how to build SSM frame , Before that, let's summarize these three frameworks .
2、 Basic concepts
2.1 MyBatis
MyBatis It's an excellent foundation java The persistence layer framework , It encapsulates jdbc, Let the developer only need to pay attention sql Statement itself , It doesn't take much effort to deal with the load driver 、 Create connection 、 establish statement And so on .
2.2 Spring
Spring Is an open source heavyweight application development framework , The goal is to simplify enterprise application development , Reduce the development difficulty of developers
Spring Provided IOC and AOP application , You can minimize the coupling of the build ( Decoupling ), It is convenient for future maintenance and upgrading of the system
Spring It provides an overall solution for the system , Developers can take advantage of the functions provided by itself , It can also be integrated with third-party frameworks and technologies , You can freely choose which technology to use for development
2.3 SpringMVC
SpringMVC yes Spring A module of the framework ,Spring and SpringMVC No mid tier integration
SpringMVC Provides the basis for MVC( Model — View — controller ) Architecture and for developing flexible and loosely coupled web Components of the application
3、 Development environment construction
Create one based on maven Of web engineering
Click on File, newly build Project project
Choose Maven, On the hook Creater from archetype Options , Select according to the numerical order in the figure and click Next button
Fill in... In turn groupid and artifactid Then click next that will do
Select the one configured in advance maven, Click on next
Enter the project name Spring, Click on Finish that will do
In this way, a name named SSM Of maven web project , The structure is as follows: :
- There is nothing left after the project is created java,resources,test Create the package yourself
We can see that the package is gray after it is created , You need to select the corresponding folder and right-click Mark Directory as, Select the corresponding color
Now the structure of the project is as follows :
3.1 Integrate Spring frame
stay pom.xml Import the required jar package
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--Spring Basic core tool classes of framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!--Junit Simple encapsulation of test framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency>
<!--Aop section -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
<scope>runtime</scope>
</dependency>
<!--Spriing jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- Business -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
3.2 Integrate SpringMVC box
stay pom.xml Import the required jar package
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
3.3 add to SpringMVC Framework support
In the project name SpringMVC Right click to select Add framework support
Click ok button
Be careful : If we are clicking Add framework support I can't find it in China. Spring, Then there may already exist in the project Spring file , But not necessarily perfect , We just need to delete it and add it again
Method : Click on File, choice Project Structure, choice Facets, You'll see one Spring, Right click on it , Just click delete , Then go back to the previous step and re Add framework support,Spring It will come out .
add to SpringMVC After support , We will find two more files in the project directory , I created another one alone views Used to store mapping files JSP etc.
3.4 To configure SpringMVC Core controller
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
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">
<!-- register springmvc Framework core controller -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- To configure dispatcher.xml As mvc Configuration file for -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- hold applicationContext.xml Add to the configuration file -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
stay web.xml Middle configuration SpringMVC Core information
To configure SpringMVC Scrambling filter
<!-- solve POST Submit Chinese garbled code problem -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<!-- Set the encoding format -->
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
To configure dispatcher-servlet.xml file information
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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">
<!-- This document is responsible for the whole mvc Configuration in -->
<!-- 1. Configure the front-end controller to release static resources (html/css/js etc. , Otherwise static resources will not be accessible ) -->
<mvc:default-servlet-handler/>
<!-- 2. Configure annotation driver , Used to identify comments ( such as @Controller) -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 3. Configure packages that need to be scanned :spring Auto scan base-package Next class ,
If the scanned class has @Controller、@Service、@Component Etc ,
The class will be automatically registered as bean
-->
<context:component-scan base-package="com.cn.controller"></context:component-scan>
<!-- 4. Configure the internal resource view parser
prefix: Configure path prefixes
suffix: Profile suffix
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.5 test SpringMVC frame
Create and write HelloController class
@Controller
public class HelloController {
/**
* This annotation is used for : Map the requested resource path (/hello) And current methods (hello) Correspondence of
* When the browser requests /hello When the path , Will visit ( perform ) The current method
*/
@RequestMapping("/hello")
public String hello() {
System.out.println("hello springmvc...");
return "home";
}
}
stay WEB-INF/views/ Create under directory home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>hello springmvc~~~</h1>
</body>
</html>
4、 Deploy Tomcat And related environment
stay Run Find... Under the menu item Edit Configurations
To configure Tomcat Environmental Science
Choose the local one Tomcat And modify the name
stay Deployment Complete the following operations under the page
Select... In the pop-up box SSM:war exploded Then click OK
start-up Tomcat The server , To test
The data content in the page is successfully displayed on the browser page
5、 Integrate MyBtis frame
I wrote an article about MyBatis A quick start tutorial for , Please refer to the following links :
Full stack development practice | MyBatis Framework quick start Chapter 1
stay pom.xml Import the required jar package
<!-- Integrate MyBatis frame -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySql drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
stay resources Create... Under package db.properties file
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/db1
database.username=root
database.password=erp1234
We were in MyBatis The quick start tutorial is in mybatis-config.xml Configuration in file MyBatis Core information , Now we have to do SSM Framework integration , Can be MyBatis The configuration of is given to Spring To deal with
To configure applicationContext.xml file information
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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">
<!-- 1. load jdbc.properties The location of the file -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--2. Configure database connections -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!-- 3. Integrate spring and mybatis frame
take SqlSession Wait for the creation of objects to Spring Containers
id value (sqlSessionFactory) It's a fixed value
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 3.2. Configure connection pool ( data source ) ref Point to connection pool bean Object's id value -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4.Mapper scanning , Auto import mapper class -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Scan all XxxMapper Interface , Hand over the creation of interface instances to spring Containers -->
<property name="basePackage" value="com.cn"/>
</bean>
</beans>
5.1 test MyBatis frame
establish Student Entity class , And use lombok Note in place of get、set、 Methods of parametric construction and nonparametric construction
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String id;
private String name;
private String birth;
private String sex;
}
establish StudentMapper Interface class , And provide getStudentById Method
package com.cn.dao;
import com.cn.pojo.Student;
public interface StudentMapper {
Student getStudentById(String id);
}
establish StudentMapper.xml file ( Mapping files for entity classes )
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cn.dao.StudentMapper">
<!-- according to id Search for student information -->
<select id="getStudentById" parameterType="string" resultType="com.cn.pojo.Student">
SELECT * from Student where id= #{id};
</select>
</mapper>
Create and write TestSSM class
/**
* test SSM development environment
*/
@Controller
public class TestSSM {
@Autowired
StudentMapper studentMapper;
@RequestMapping("/testSSM")
public void testSSM(){
Student student = studentMapper.getStudentById("01");
System.out.println(student);
}
}
Output results
We saw the abnormal report , I can't find it mapper Map file of , in other words JAVA When compiling mapper.xml The document is missing , The solution is pom.xml Add the following code to the file :
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
Revisit , Output results
Here's the simple ssm Even if the framework is finished , It seems like a long process , But the actual operation should not be too difficult , Just be careful
This article is an integration between frameworks based on the previous article , Convenient for our future development , If you are interested in the previous article, you can click the following link :
Full stack development practice | MyBatis Framework quick start Chapter 1
Full stack development practice | MyBatis Framework quick start Chapter 2
Full stack development practice | Spring Framework quick start Chapter 1
Full stack development practice | Spring Framework quick start Chapter 2
Full stack development practice | Spring Framework quick start Chapter 3
Full stack development practice | SpringMVC Framework quick start Chapter 1
Full stack development practice | SpringMVC Framework quick start Chapter 2
Full stack development practice | SpringMVC Framework quick start Chapter 3
边栏推荐
- Build a simple dynamic IP proxy pool
- Redis implements distributed current limiting (learning notes
- 响应式织梦模板风扇外贸类网站
- 为cv::Point 重载了&lt;,但VS2010找不到
- 源码安装PostgreSQL 13.1
- 高等数学(第七版)同济大学 习题3-2 个人解答
- Can we have both good quality and low price?
- Type personnalisé: structure (1)
- Install MySQL and MySQL workbench on MAC 10.13.6 and other versions
- solana上使用Rust进行合约开发
猜你喜欢
Map and set knowledge points
基于SSM框架+MySQL的超市订单管理系统【源码+文档+PPT】
Reading club | get rid of the fog of psychological counseling: how should ordinary people treat psychological counseling
学习笔记(1)初识uni-app
工业制造厂房vr虚拟实景展示,真实立体呈现到客户面前
mac 10.13.6 及其他版本安装MySQL和MySQL Workbench
What if win11 C disk turns red? Cleaning method of win11 C disk turning red
The B2B system of digital intelligence in the daily chemical industry simplifies the distribution process and improves the competitiveness of the supply chain of daily chemical enterprises
OSError: [WinError 1455] 页面文件太小,无法完成操作。
效率低?响应慢?报表工具痛点及其解决方案
随机推荐
July 2022 Russian database ranking: Clickhouse ranked first and gigabase ranked second
【LeetCode】在非有序数组中使用二分
读书笔记(一)——《追风筝的人》
平安证券 股票网上开户,有资金要求吗,安全吗
华为云:一切皆服务,共建全场景智慧金融
RT-thread-2022夏令营-学习总结-第二天
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
mac 10.13.6 及其他版本安装MySQL和MySQL Workbench
yarn capacity调度器小结
全栈开发实战 | SSM框架整合完整教程
自定義類型:結構體(一)
How to solve the problem of win11 Microsoft account login? Win11 Microsoft account login has been circling
How to solve the problem that win11 printer document is suspended?
科研 | 研究成果该如何署名?
快手海外产品 SnackVideo 与 PUBG MOBILE 独家合作 助力手游海外破圈
消息称苹果继续向俄罗斯市场交付产品:不进行售卖,只用于退换货
源码安装PostgreSQL 13.1
In the era of UCA, how can the enterprise supply chain respond to the enterprise procurement expenditure management quickly?
Hex burning with miniprog3
静态库制作及使用