SSM整合(配置文件详解)
本文开发环境①数据库版本8.0+;②前端模板引擎使用Thymeleaf,因此配置的是Thymeleaf视图解析器视图解析器③使用idea开发
1.引入依赖
<packaging>war</packaging>
<properties>
<spring.version>5.3.2</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
2.配置web.xml
配置两个过滤器,一个前端控制器,一个spring监听器,设置Spring和SpringMVC的配置文件的自定义位置和名称
<?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">
<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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
</web-app>
3.创建spring和springmvc配置文件
3.1 springmvc.xml配置文件
将其放到resources目录下
<?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">
<context:component-scan base-package="com.b0.controller"/>
<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">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:view-controller path="/" view-name="index"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
</beans>
3.2 spring与mybatis整合
将其放到放到resources目录下
1.编写jdbc.properties文件
jdbc.user=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
2.引入日志文件log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}
%m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
3.编写mybatis-config.xml核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
3.编写spring.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">
<context:component-scan base-package="com.b0">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.b0.pojo"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.b0.mapper"></property>
</bean>
</beans>
ssm整合案例(实现增删改查,数据分页)
文件存放位置
1.创建数据库-数据表插入30条示例数据
数据库名ssm表名t_emp
CREATE TABLE `t_emp` (
`emp_id` int NOT NULL AUTO_INCREMENT,
`emp_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int NULL DEFAULT NULL,
`sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`emp_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `t_emp` VALUES (1, 'a', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (2, 'd', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (3, 'df', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (4, 'sfdsf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (5, 'dsfsd', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (6, 'fsdf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (7, 'kkjhgf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (8, 'safsdf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (9, 'asdas', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (10, 'asfdf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (11, 'afas', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (12, 'asfs', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (13, 'fasfs', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (14, 'ddfdsd', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (15, 'adfss', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (16, 'sdgfds', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (17, 'sdfdsf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (18, 'fgghjytre', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (19, 'werewtr', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (20, 'wehjhbg', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (21, 'rtytt', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (22, 'yukjmhg', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (23, 'jkl;lgfd', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (24, 'ghjm,jhgf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (25, 'rtkd', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (26, 'ghjkhg', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (27, 'fghjgf', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (28, 'fghjktr', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (29, 'ewrtytr', NULL, NULL, NULL);
INSERT INTO `t_emp` VALUES (30, 'rtyr', NULL, NULL, NULL);
2.编写实体类Employee.java
public class Employee {
private Integer empId;
private String empName;
private Integer age;
private String sex;
private String email;
}
3.编写xxMapper.java接口和对应映射文件*Mapper.xml
public interface EmployeeMapper {
List<Employee> getAllEmployee();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.b0.mapper.EmployeeMapper">
<select id="getAllEmployee" resultType="Employee">
select * from t_emp
</select>
</mapper>
4.编写service层以及实现类
public interface EmployeeService {
List<Employee> getAllEmployee();
PageInfo<Employee> getEmployeePage(Integer pageNum);
}
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public List<Employee> getAllEmployee() {
return employeeMapper.getAllEmployee();
}
@Override
public PageInfo<Employee> getEmployeePage(Integer pageNum) {
PageHelper.startPage(pageNum,4);
List<Employee> list = employeeMapper.getAllEmployee();
PageInfo<Employee> page = new PageInfo<>(list,5);
return page;
}
}
5.编写控制类
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/employee/page/{pageNum}")
public String getEmployeePage(@PathVariable("pageNum")Integer pageNum,Model model){
PageInfo<Employee> page = employeeService.getEmployeePage(pageNum);
model.addAttribute("page",page);
return "employee_list";
}
@GetMapping("/employee")
public String getAllEmployee(Model model){
List<Employee> list = employeeService.getAllEmployee();
model.addAttribute("list",list);
return "employee_list";
}
}
6.编写前端网页处理数据
将全部网页存放到templates目录下。 首页index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>视图控制器默认访问页面</h1>
<a th:href="@{/employee/page/1}">查询员工的分页信息</a>
</body>
</html>
信息展示页
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>员工列表</title>
</head>
<body>
<table>
<tr>
<th colspan="6">员工列表</th>
</tr>
<tr>
<th>流水号</th>
<th>员工姓名</th>
<th>年龄</th>
<th>性别</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<tr th:each="employee,status:${page.list}">
<td th:text="${status.count}"></td>
<td th:text="${employee.empName}"></td>
<td th:text="${employee.age}"></td>
<td th:text="${employee.sex}"></td>
<td th:text="${employee.email}"></td>
<td>
<a href="">删除</a>
<a href="">修改</a>
</td>
</tr>
</table>
<div style="text-align: center;">
<a th:if="${page.hasPreviousPage}" th:href="@{/employee/page/1}">首页</a>
<a th:if="${page.hasPreviousPage}" th:href="@{'/employee/page/'+${page.prePage}}">上一页</a>
<span th:each="num:${page.navigatepageNums}">
<a th:if="${page.pageNum == num}" style="color: red;" th:href="@{'/employee/page/'+${num}}" th:text="'['+${num}+']'"></a>
<a th:if="${page.pageNum != num}" th:href="@{'/employee/page/'+${num}}" th:text="${num}"></a>
</span>
<a th:if="${page.hasNextPage}" th:href="@{'/employee/page/'+${page.nextPage}}">下一页</a>
<a th:if="${page.hasNextPage}" th:href="@{'/employee/page/'+${page.pages}}">尾页</a>
</div>
</body>
</html>
资料来源:B站尚硅谷
|