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

边栏推荐
- Baidu map eagle eye track service
- 百度地图鹰眼轨迹服务
- Jianmu continuous integration platform v2.5.2 release
- Led learning eye protection table lamp touch chip-dlt8t10s-jericho
- The great idea of NS2
- NPM basic use
- Kinect2 for Unity3D——AvatarDemo学习
- Netred RGB mirror light touch chip-dlt8s15b-jericho
- Overview of Baidu map technology, and application development of basic API and webapi
- 连接查询和子查询
猜你喜欢

Bathroom with demister vanity mirror touch chip-dlt8t10s

商品名称模糊搜索:

LeetCode 刷题 第三天

express

MySQL 03 高级查询(一)

Leetcode first day of question brushing

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

Multifunctional wireless remote control moxibustion instrument chip dltap703sd

Unity-显示Kinect深度数据

MySQL 04 高级查询(二)
随机推荐
Unity学习笔记——物体移动六种常见函数
C file and folder input / output stream code
MySQL 01 关系型数据库设计
Zhaoqi scientific and technological innovation introduces high-level talents at home and abroad and connects innovation and entrepreneurship projects
订单超时取消 及 按类别查询商品
Docker - docker installation, MySQL installation on docker, and project deployment on docker
百度地图鹰眼轨迹服务
订单的提交
Low noise anion fan touch IC
Here are all the MySQL interview questions you can't expect (the latest version of 2022)
自控原理学习笔记-系统稳定性分析(1)-BIBO稳定及Routh判据
ERROR 1366 (HY000): Incorrect string value: ‘\xE8\xB5\xB5\xE9\x9B\xB7‘ for column ‘s_ name‘ at row 1
面试官:你觉得你最大的缺点是什么?
I'm stupid. When completable future is used with openfegin, it even reports an error
Baidu map eagle eye track service
Collection of software design suggestions of "high cohesion and low coupling"
MySQL 04 高级查询(二)
express get/post/delete...请求
normal distribution, lognormal distribution,正态随机数的生成
飞机大战敌机出场