当前位置:网站首页>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

边栏推荐
- CTS test introduction (how to introduce interface test in interview)
- Idea error failed to determine a suitable driver class
- VS2017大型工厂ERP管理系统源码 工厂通用ERP源码
- 科隆新能源IPO被终止:拟募资6亿 先进制造与战新基金是股东
- Runtimeerror: CUDA out of memory (solved) [easy to understand]
- Typora cannot open the prompt to install a new version solution
- Sqli labs installation environment: ubuntu18 php7
- Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
- Basic theory of monocular depth estimation and paper learning summary
- Doris学习笔记之与其他系统集成
猜你喜欢

Data analysis business core

Matplotlib data visualization three minutes entry, half an hour enchanted?

Initial flask and simple application

From fish eye to look around to multi task King bombing -- a review of Valeo's classic articles on visual depth estimation (from fisheyedistancenet to omnidet) (Part I)

机械制造业数字化新“引擎”供应链协同管理系统助力企业精细化管理迈上新台阶

【口才】谈判说服技巧及策略

~4.2 CCF 2021-12-1 sequence query

Deep understanding of pytorch distributed parallel processing tool DDP -- starting from bugs in engineering practice

How to make a set of code fit all kinds of screens perfectly?

用GaussDB(for Redis)存画像,推荐业务轻松降本60%
随机推荐
Thymeleaf setting disabled
华为ensp路由器静态路由(默认路由的下一跳地址)
Structure size
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
jqgrid全选取消单行点击取消事件
R语言如何将大型Excel文件转为dta格式详解
2271. Maximum number of white bricks covered by blanket ●●
Huawei ENSP router static route (the next hop address of the default route)
Goldfish rhca memoirs: cl210 managing storage -- managing shared file systems
Deep understanding of pytorch distributed parallel processing tool DDP -- starting from bugs in engineering practice
基于PaddleOCR开发uni-app离线身份证识别插件
轻松入门自然语言处理系列 12 隐马尔可夫模型
网络安全应急响应技术实战指南(奇安信)
Flask SSTI injection learning
OverTheWire-Natas
Realsense-Ros安装配置介绍与问题解决
阿里云安装MYSQL5.7
Why do China Construction and China Railway need this certificate? What is the reason?
OKA通证权益解析,参与Okaleido生态建设的不二之选
How to make a set of code fit all kinds of screens perfectly?