1.1 Directory Structure
Spring+Mybatis
1.2 Dependent Collation
<! -- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
<! -- junit5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<! -- Spring test functionality -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.8</version>
</dependency>
<! -- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<! -- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
<! -- Spring Persistence layer dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.8</version>
</dependency>
<! -- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<! -- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<! -- Spring5 and Thymeleaf integration package
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12. RELEASE</version>
</dependency>
<! Mybatis core -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<! -- MySQL driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<! Connection pool data source -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
Copy the code
Configure a JDBC 1.3. The properties
jdbc.url=JDBC: mysql: / / 47.106.207.254:3306 / empdb
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=* * * * *
jdbc.password=* * * * * *
jdbc.filters=stat
jdbc.initialSize=2
jdbc.maxActive=300
jdbc.maxWait=60000
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000
jdbc.validationQuery=SELECT 1
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false
jdbc.poolPreparedStatements=false
jdbc.maxPoolPreparedStatementPerConnectionSize=200
Copy the code
1.4 to join logback. XML
<configuration debug="true">
<! -- Specify log output location -->
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<! -- Log output format -->
<! Time, log level, thread name, class to print logs, log body content, newline -->
<pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern>
</encoder>
</appender>
<! -- Set the global log level. The log levels are DEBUG, INFO, WARN, and ERROR -->.
<! -- Specifies that only logs at the current and later levels are printed at any log level. -->
<root level="INFO">
<! -- Specify the appender to print logs. Here we reference the previously configured appender with "STDOUT".
<appender-ref ref="STDOUT"/>
</root>
<! -- Specify the local log level according to special requirements -->
<! -- <logger name="mapper.EmpMapper" level="DEBUG"/>-->
</configuration>
Copy the code
1.5 introduced spring – context. The XML
<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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config></context:annotation-config>
<! -- Configure automatic scan packets -->
<context:component-scan base-package="com.model"/>
<context:component-scan base-package="com.mapper"/>
<context:component-scan base-package="com.services"/>
<! -- Configure a scanner with Mapper interface type -->
<mybatis-spring:scan base-package="com.mapper"/>
<! Import external properties file -->
<context:property-placeholder location="classpath:mysql-jdbc.properties"/>
<! -- Configure data source -->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="filters" value="${jdbc.filters}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxWait" value="${jdbc.maxWait}"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="${jdbc.validationQuery}"/>
<property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
<property name="testOnBorrow" value="${jdbc.testOnBorrow}"/>
<property name="testOnReturn" value="${jdbc.testOnReturn}"/>
<property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}"/>
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${jdbc.maxPoolPreparedStatementPerConnectionSize}"/>
</bean>
<! -- Configure JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<! Assemble data source -->
<property name="dataSource" ref="druidDataSource"/>
</bean>
<! Create a transaction manager -->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<! Inject data source -->
<property name="dataSource" ref="druidDataSource"></property>
</bean>
<! -- Register transaction manager driver -->
<tx:annotation-driven transaction-manager="TransactionManager"/>
<! SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<! -- Discard Mybatis global configuration file and use the Configuration property -->
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
<! TypeAliasesPackage: typeAliasesPackage: typeAliasesPackage
<property name="typeAliasesPackage" value="com.model"/>
<! Mapper configuration file location -->
<property name="mapperLocations" value="classpath:com/mapper/*Mapper.xml"/>
<! Assemble data source -->
<property name="dataSource" ref="druidDataSource"/>
</bean>
</beans>
Copy the code
1.6 introduction of mybatis config. XML
<! DOCTYPEconfiguration
PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<! -- Mybatis Global Configuration -->
<settings>
<! Map database table fields to camel-named Java entity class attributes
<! Mysql > select * from db;
<! -- Java entity class attributes: Camel name in lower case -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
Copy the code
1.7 Model Establishment
Entity classes, Mapper
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Seat {
private Integer seatId;
private String seatName;
}
@Component
public interface SeatMapper {
List<Seat> selectAll(a);
}
Copy the code
mapper.xml
<! DOCTYPEmapper
PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.SeatMapper">
<select id="selectAll" resultType="com.model.Seat">
select *
from seat;
</select>
</mapper>
Copy the code
1.8 Testing
import com.mapper.SeatMapper;
import com.model.Seat;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
@SpringJUnitConfig(locations = {"classpath:springConfig/spring-context.xml"})
public class SSMTest01 {
@Autowired
private DataSource dataSource;
@Autowired
private SeatMapper seatMapper;
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void testConn(a) throws SQLException {
Connection connection = dataSource.getConnection();
logger.debug(connection.toString());
}
@Test
@Transactional(readOnly = true)
public void selectTest01(a)
{
List<Seat> seatList = seatMapper.selectAll();
for(Seat seat: seatList ) { System.out.println(seat); }}}Copy the code
Spring+SpringMVC
1.9 Web. XML configuration
<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">
<! -- ContextLoaderListener -->
<! Context-param = context-param = context-param = context-param
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springConfig/spring-context.xml</param-value>
</context-param>
<! -- ContextLoaderListener listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<! -- 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:springConfig/spring-mvc.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>
<! Note the order of two filters: character set Filter first, transform request mode Filter second
<! -- CharacterEncodingFilter -->
<filter>
<filter-name>characterEncodingFilter</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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/ *</url-pattern>
</filter-mapping>
<! -- HiddenHttpMethodFilter -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/ *</url-pattern>
</filter-mapping>
</web-app>
Copy the code
1.10 spring MVC. XML (Thymeleaf)
<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">
<! -- Automatically scan packets to validate annotations under specified packets, managed by IOC containers -->
<context:component-scan base-package="com.controller"/>
<! Let Spring MVC not handle static resources -->
<mvc:default-servlet-handler/>
<! -- SpringMVC Annotation Driver (standard) -->
<mvc:annotation-driven/>
<! -- Thymeleaf view parser -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<! -- View prefix -->
<property name="prefix" value="/WEB-INF/view/"/>
<! -- View suffix -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
Copy the code
1.11 minutes controller
package com.controller;
import com.mapper.SeatMapper;
import com.model.Seat;
import com.services.PageInfoDemo01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class IndexHandle {
@Autowired
private SeatMapper seatMapper;
@RequestMapping(value = "/", method = {RequestMethod.GET})
public ModelAndView indexView( ModelAndView mv) {
List<Seat> seatList = seatMapper.selectAll();
mv.addObject("seatList", seatList);
mv.setViewName("index");
return mv;
}}
Copy the code
1.12 HTML
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The home page</title>
</head>
<body>
<br>
<h1>SSM integration test</h1>
<! - button - >
<div class="row">
<div class="col-md-4 col-md-offset-8">
<button class="btn btn-primary">new</button>
<button class="btn btn-danger">delete</button>
</div>
</div>
<! -- Display table data -->
<div class="row">
<div class="col-md-12">
<table class="table table-hover">
<tr>
<th>ID</th>
<th>NAME</th>
</tr>
<tbody th:if="${#lists.isEmpty(seatList)}">
<tr>
<td colspan="3">I'm sorry! No data found!</td>
</tr>
</tbody>
<tbody th:if="${not #lists.isEmpty(seatList)}">
<tr th:each="seat : ${seatList}">
<td th:text="${seat.seatId}">The employee ID is displayed here</td>
<td th:text="${seat.seatName}">The employee NAME is displayed here</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Copy the code
Start Tomcat 1.13
complete