当前位置:网站首页>SSM integration
SSM integration
2022-07-27 19:02:00 【It's too late】
summary :
When developing enterprise projects , We need to Spring、SpringMVC、MyBatis Framework integration , Jointly complete the development work .
SSM Division of labor :
- Spring As a basic framework , Integrate other frameworks
- SpringMVC As Web Development framework , Provide server development support
- MyBatis As ORM frame , Provide database development support
Consolidation configuration :
1, Add dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-compat</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-jstlel</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
2,SpringMVC Configuration of spring-mvc.xml
<?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">
<!-- Configure the view processor -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
3,MyBatis Foundation setup mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- Name the hump -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Print query statements -->
<setting name="logImpl" value="STDOUT_LOGGING" />
<!-- Configure lazy loading -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- Perform all attribute on-demand loading -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
</configuration>
4,Spring Integrate Mybatis To configure spring-mybatis.xml
<?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" 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">
<!-- To configure C3P0 data source -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--jdbc Configuration of -->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/book_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<!-- Not necessary -->
<property name="maxPoolSize" value="100"></property>
<property name="minPoolSize" value="10"></property>
<property name="initialPoolSize" value="10"></property>
<property name="maxStatements" value="200"></property>
</bean>
<!-- Configure session factory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- Configure data sources -->
<property name="dataSource" ref="dataSource"></property>
<!-- Package alias -->
<property name="typeAliasesPackage" value="com.blb.bookms.entity"></property>
<!--MyBatis Profile path -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- Path to map file -->
<property name="mapperLocations" value="classpath:mappers/*.xml"></property>
</bean>
<!-- Configure the scanner of the mapping interface -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Session factory name -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- The package location of the interface -->
<property name="basePackage" value="com.blb.bookms.dao"></property>
</bean>
</beans>
5,Spring The configuration file , Import SpringMVC and MyBatis To configure
<?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" 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">
<context:component-scan base-package="com.blb.bookms"></context:component-scan>
<import resource="spring-mvc.xml"></import>
<import resource="spring-mybatis.xml"></import>
</beans>
6,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<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:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Test cases
1、 Add book table 
2、 Write entity class
package com.blb.bookms.entity;
/** * Books */
public class Book {
private Integer id;
private String bookName;
private float price;
private Integer typeId;
private String author;
private String publishOrg;
private String publishTime;
private Integer state;
private String bookImage;
}
3、 To write DAO Interface
package com.blb.bookms.dao;
public interface IBookDAO {
List<Book> selectAllBooks();
}
4、 To write Service Interface
package com.blb.bookms.service;
public interface IBookService {
List<Book> findAllBooks();
}
5、 To write Service Implementation class
package com.blb.bookms.service.impl;
@Service
public class BookServiceImpl implements IBookService {
@Autowired
private IBookDAO bookDAO;
@Override
public List<Book> findAllBooks() {
return bookDAO.selectAllBooks();
}
}
6、 Write controller
package com.blb.bookms.controller;
/** * Book controller */
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping("/findAllBooks")
public String findAllBooks(Model model){
List<Book> books = bookService.findAllBooks();
model.addAttribute("books",books);
return "book";
}
}
7、 To write webapp/WEB-INF/pages/book.jsp page
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title> Book management </title>
<link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<div class="layui-container">
<div class="layui-row">
<div class="layui-col-md9">
<table class="layui-table" lay-size="sm">
<thead>
<tr>
<th> Number </th>
<th> Title </th>
<th> Price </th>
<th> type </th>
<th> author </th>
<th> Press. </th>
<th> Publication date </th>
<th> state </th>
<th> picture </th>
<th> operation </th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${books}">
<tr>
<td>${book.id}</td>
<td>${book.bookName}</td>
<td>${book.price}</td>
<td>${book.typeId}</td>
<td>${book.author}</td>
<td>${book.publishOrg}</td>
<td>${book.publishTime}</td>
<td>${book.state}</td>
<td>
<img src="/images/${book.bookImage}">
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div id="page"></div>
</div>
</div>
</div>
</body>
</html>

边栏推荐
猜你喜欢

商品名称模糊搜索:

`this.$emit` 子组件给父组件传递多个参数

Talking about JVM (frequent interview)

Product recommendation and classified product recommendation
MySQL create event execution task

Interviewer: what do you think is your biggest weakness?

CMD 命令

express get/post/delete...请求

Docker - docker installation, MySQL installation on docker, and project deployment on docker

Unity学习笔记(刚体-物理-碰撞器-触发器)
随机推荐
Was not registered for synchronization because synchronization is not active[resolved]
Aircraft collision detection
Zhaoqi scientific and technological innovation introduces high-level talents at home and abroad and connects innovation and entrepreneurship projects
Extension of ES6 value
MySQL 03 advanced query (I)
Acquisition data transmission mode and online monitoring system of vibrating wire wireless acquisition instrument for engineering instruments
用Matlab生成适用于期刊及会议的图形- plot
JDBC MySQL 02 data access and Dao mode
Leetcode brushes questions the next day
Commodity comment information and comment information classification
USB rechargeable hand warmer chip dltap602sc Jericho
Multifunctional wireless remote control moxibustion instrument chip dltap703sd
Unity显示Kinect捕获的镜头
Talking about JVM (frequent interview)
怎样产生标准分布或高斯分布的随机数
Unity-显示Kinect深度数据
LeetCode 刷题 第三天
面试官:你觉得你最大的缺点是什么?
Order submission
C#与Mysql数据库交互-Mysql配置及增删查改操作