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

边栏推荐
- Typeerror: conv2d(): argument 'padding' (position 5) must be multiple of ints, not STR [error]
- Aircraft battle with enemy aircraft
- Express get/post/delete... Request
- npm、cnpm 淘宝镜像
- 商品名称模糊搜索:
- pygame飞机大战游戏背景实现
- Overview of Baidu map technology, and application development of basic API and webapi
- 订单超时取消 及 按类别查询商品
- MySQL 05 存储过程
- MySQL 06 transaction, view, index, backup and recovery
猜你喜欢

Whole body multifunctional massage instrument chip-dltap602sd

自控原理学习笔记-系统稳定性分析(1)-BIBO稳定及Routh判据

Extension of regular expression

C#与Mysql数据库交互-Mysql配置及增删查改操作

JDBC MySQL 01 JDBC operation MySQL (add, delete, modify and query)

Join query and subquery

npm的身份证和依赖

Netred RGB mirror light touch chip-dlt8s15b-jericho

Blog Garden beautification tutorial

Examples of map search
随机推荐
Redis annotation
Order timeout cancellation and commodity query by category
Hash、Set、List、Zset、BitMap、Scan
Openstack login dashboard prompt authentication error
Ridis command notes
Collection of software design suggestions of "high cohesion and low coupling"
WORD 2007+使用技巧
【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
你想得到想不到的MySQL面试题都在这了(2022最新版)
飞机大战敌机出场
Some advice for NS2 beginner.
C#与Mysql数据库交互-Mysql配置及增删查改操作
瑞吉外卖sql表
Music rhythm colorful gradient lamp chip -- dlt8s04a- Jericho
百度地图鹰眼轨迹服务
Kinect for Unity3d----KinectManager
商品推荐和分类商品推荐
The great idea of NS2
express
Led with fan eye protection learning table lamp touch chip-dlt8s12a