当前位置:网站首页>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 error failed to determine a suitable driver class
- CDA level1 double disk summary
- PHP website design ideas
- Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount
- Bond0 script
- Throwing OutOfMemoryError “Could not allocate JNI Env“
- Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation
- idea正则表达式替换(idea正则搜索)
- What you must know about data engineering in mlops
- 【cartographer_ros】八: 官方Demo参数配置和效果
猜你喜欢

maya建模练习

Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation

Doris学习笔记之与其他系统集成

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

Gartner 2022 top technology trend: Super automation

Business data analysis of CDA level1 knowledge point summary

Oka pass rights and interests analysis is the best choice to participate in okaleido ecological construction

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

Hyperautomation for the enhancement of automation in industries

Mysql表的操作
随机推荐
It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
thymeleaf通过样式控制display是否显示
Nuc980 set up SSH xshell connection
Why do China Construction and China Railway need this certificate? What is the reason?
Interpretation of featdepth self-monitoring model for monocular depth estimation (Part 2) -- use of openmmlab framework
苹果手机端同步不成功,退出登录,结果再也登录不了
pt100测温电路图(ad590典型的测温电路)
CDA level1 multi topic selection
Problems and extensions of the monocular depth estimation model featdepth in practice
NUC980 设置SSH Xshell连接
Depth estimation self-monitoring model monodepth2 paper summary and source code analysis [theoretical part]
Teach you how to apply for SSL certificate
DVWA practice - brute force cracking
Typora无法打开提示安装新版本解决办法
CDA level1 double disk summary
Business data analysis of CDA level1 knowledge point summary
IDEA报错 Failed to determine a suitable driver class
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)
Jqgrid select all cancel single line click cancel event
如何让一套代码完美适配各种屏幕?