当前位置:网站首页>SSM framework integration, simple case
SSM framework integration, simple case
2022-07-25 14:23:00 【Dumpling_ skin】
1、SSM Introduction to the framework
SSM(Spring+SpringMVC+MyBatis) The frameset is made up of Spring、MyBatis Integration of two open source frameworks (SpringMVC yes Spring Part of ), Often used as a simple data source web Project framework .
Spring
Spring It's like assembling the whole project bean Big factory of , In the configuration file, you can specify to use specific parameters to call the constructor of entity class to instantiate the object . It can also be called the adhesive in the project .
Spring The core idea of IoC( Inversion of control ), That is, there is no need for programmers to explicitly `new` An object , Rather let Spring The framework helps you do all this .
SpringMVC
SpringMVC Intercept user requests in the project , The core of it Servlet namely DispatcherServlet Take on the role of an intermediary or front desk , Pass the user request through HandlerMapping To match Controller,Controller It is the specific operation corresponding to the request .SpringMVC amount to SSH In the frame struts.
mybatis
mybatis It's right jdbc Encapsulation , It makes the underlying operations of the database transparent .mybatis The operations are all around a sqlSessionFactory Examples expand .mybatis Through the configuration file associated with each entity class Mapper file ,Mapper The file configures what each class needs to do with the database sql Statements mapping . Every time you interact with the database , adopt sqlSessionFactory To get a sqlSession, Re execution sql command .
Page send request to controller , The controller calls the business layer processing logic , The logic layer sends requests to the persistence layer , The persistence layer interacts with the database , Then return the result to the business layer , The business layer sends the processing logic to the controller , The controller calls the view to display the data .
2、 Integrate SSM frame
(1) Create a maven-web project , Replace WEB-INF Inside web.xml file
<?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>(2) Add dependencies
<dependencies>
<!-- Paging plug-ins -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--mybatis rely on -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--mybatis and spring Integration dependency -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--mysql drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!--druid Connection pool dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<!--lombok rely on -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<!--jackson java Object to json object @ResponseBody-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
<!--servlet-api rely on -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--spring-jdbc rely on Parsing data source configuration -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--mybatis-generator Code generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<!--junit unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Upload to OSS Cloud server -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.13.0</version>
</dependency>
<!--log4j journal -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--slf4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<!-- Upload files -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>(3) To write spring Configuration file for
<?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">
<!-- Package scanning -->
<context:component-scan base-package="com.gjx"/>
<!-- Turn on special annotation scanning -->
<mvc:annotation-driven/>
<!-- Static resource release -->
<mvc:default-servlet-handler/>
<!-- Configure login interceptor -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/register"/>
<mvc:exclude-mapping path="css/**"/>
<mvc:exclude-mapping path="js/**"/>
<mvc:exclude-mapping path="images/**"/>
<bean class="com.gjx.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- Upload files id Name must be multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Maximum upload size , Unit is byte-->
<property name="maxUploadSize" value="10485760"/>
</bean>
<!--spring To configure -->
<!-- Data source configuration -->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- Database driven -->
<property name="url" value="jdbc:mysql://localhost:3306/vue01?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="990412"/>
<!-- The number of connections initializing the connection pool -->
<property name="initialSize" value="5"/>
<!-- Minimum number of idle -->
<property name="minIdle" value="5"/>
<!-- maximum connection -->
<property name="maxActive" value="10"/>
<!-- The maximum waiting time unit is milliseconds -->
<property name="maxWait" value="3000"/>
</bean>
<!--sqlSessionFactory Integrate mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- Alias package -->
<property name="typeAliasesPackage" value="com.gjx.entity"/>
<!-- Data source configuration -->
<property name="dataSource" ref="druidDataSource"/>
<!--mapper Storage location of mapping file -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--plugins plug-in unit ,array type , Paging plug-ins -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"></bean>
</array>
</property>
</bean>
<!-- by dao The interface under the package generates a proxy implementation class -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gjx.dao"/>
</bean>
</beans>(4) To configure 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">
<!-- To configure DispatcherServlet-->
<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>
<!--tomcat Create at startup DispatcherServlet The default is to create -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>(5)mybatis Code generator code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- Add a connection to the database jar The location of the package , At the bottom left of the project external libraries Copy full path found in -->
<classPathEntry location="E:\repMaven\mysql\mysql-connector-java\8.0.29\mysql-connector-java-8.0.29.jar" />
<!--
targetRuntime :MyBatis3 Generate exmple
MyBatis3Simple No generation exmple
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!-- Remove automatically generated comments or not true: yes : false: no -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- Database connection information : Drive class 、 Connection address 、 user name 、 password -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/vue01?serverTimezone=Asia/Shanghai"
userId="root"
password="990412">
<!-- solve mysql8.0 The problem of generating all tables repeatedly in the future -->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<!-- Default false, hold JDBC DECIMAL(decimal) and NUMERIC(numeric) Type resolution as Integer, by true When the JDBC DECIMAL and
NUMERIC Type resolution as java.math.BigDecimal -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- Configuration of entity class , targetProject: Where to generate entity classes -->
<javaModelGenerator targetPackage="com.gjx.entity" targetProject=".\src\main\java">
<!-- enableSubPackages: Whether to let schema As the suffix of the package -->
<property name="enableSubPackages" value="false" />
<!-- The value returned from the database is cleared before and after the space -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- The mapping file mapper Configuration of -->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--dao Package is the configuration of business logic layer -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.gjx.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!--
Configuration of database tables
-->
<table schema="" tableName="tbl_user" domainObjectName="User">
</table>
<table schema="" tableName="t_stu" domainObjectName="Stu">
</table>
<table schema="" tableName="t_teacher" domainObjectName="Teacher">
</table>
</context>
</generatorConfiguration>(6) Run the code generator
public class Run {
@Test
public void run() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mybatis-generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}3、SSM Framework directory structure

边栏推荐
- idea正则表达式替换(idea正则搜索)
- It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
- 2271. Maximum number of white bricks covered by blanket ●●
- Realize a family security and environmental monitoring system (I)
- Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount
- swiper 一侧或两侧露出一小部分
- Introduction to PHP
- einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
- Development of uni app offline ID card identification plug-in based on paddleocr
- Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
猜你喜欢

CDA level Ⅰ 2021 new version simulation question 1 (with answers)

Maya modeling exercise

That day, I installed a database for my sister... Just help her sort out another shortcut

PS制作加载GIF图片教程

swiper 一侧或两侧露出一小部分

Realize a family security and environmental monitoring system (I)

~5 new solution of CCF 2021-12-2 sequence query

Nuc980 set up SSH xshell connection

如何设计一个高并发系统?

疫情之下,生物医药行业或将迎来突破性发展
随机推荐
pytorch训练代码编写技巧、DataLoader、爱因斯坦标示
Depth estimation self-monitoring model monodepth2 paper summary and source code analysis [theoretical part]
Why do China Construction and China Railway need this certificate? What is the reason?
实现一个家庭安防与环境监测系统(二)
Matplotlib data visualization three minutes entry, half an hour enchanted?
Problems and extensions of the monocular depth estimation model featdepth in practice
为什么中建、中铁都需要这本证书?究竟是什么原因?
Basic theory of monocular depth estimation and paper learning summary
Famous handwritten note taking software recruit CTO · coordinate Shenzhen
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
CDA level Ⅰ 2021 new version simulation question 1 (with answers)
The main function of component procurement system, digital procurement helps component enterprises develop rapidly
VS2017大型工厂ERP管理系统源码 工厂通用ERP源码
依迅总经理孙峰:公司已完成股改,准备IPO
Polymorphism and interface
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
A small part is exposed on one or both sides of the swiper
maya建模练习
网络安全应急响应技术实战指南(奇安信)
Sqli labs installation environment: ubuntu18 php7