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

边栏推荐
- Throwing OutOfMemoryError “Could not allocate JNI Env“
- 手把手教你申请SSL证书
- Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
- Goldfish rhca memoirs: cl210 managing storage -- managing shared file systems
- NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
- 飞沃科技IPO过会:年营收11.3亿 湖南文旅与沅澧投资是股东
- Structure size
- PHP website design ideas
- thymeleaf通过样式控制display是否显示
- Data analysis business core
猜你喜欢

Mongodb源码部署以及配置

Initial flask and simple application

手把手教你申请SSL证书

It is predicted that 2021 will accelerate the achievement of super automation beyond RPA

Emergency science | put away this summer safety guide and let children spend the summer vacation safely!
Famous handwritten note taking software recruit CTO · coordinate Shenzhen

PS制作加载GIF图片教程
![[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster

Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%

Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount
随机推荐
Typora cannot open the prompt to install a new version solution
Interpretation of featdepth self-monitoring model for monocular depth estimation (Part I) -- paper understanding and core source code analysis
基于redis的keys、scan删除ttl为-1的key
Apple failed to synchronize on its mobile terminal, so it exited the login. As a result, it could not log in again
swiper 一侧或两侧露出一小部分
Vs2017 large factory ERP management system source code factory general ERP source code
OverTheWire-Natas
thymeleaf通过样式控制display是否显示
The security market has entered a trillion era, and the security B2B online mall platform has been accurately connected to deepen the enterprise development path
【cartographer_ros】八: 官方Demo参数配置和效果
Leetcode 205. isomorphic string (2022.07.24)
Thymeleaf setting disabled
bond0脚本
【口才】谈判说服技巧及策略
CDA level1 double disk summary
sudo rosdep init Error ROS安装问题解决方案
Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO
结构体大小
OKA通证权益解析,参与Okaleido生态建设的不二之选
Mysql表的操作