SSM整合(经典自我版)
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<h1><a id="SSM_0"></a>SSM整合</h1>
提示:自我练习之后参考进行自主整合
文章目录
前言
代码要多练,自己练,独立练,才能有效,不能做脱离视频课件的三不知
这篇文章能够帮助你了解基础框架SSM的整合及进行相应的增删改查操作
一、SSM整合搭建环境持久化层操作(spring和mybatis)
本质:Spring接管一切,代码更加简洁。
- Spring管理数据源,连接数据库
- Spring管理SqlSessionFactory,开启SqlSession的细节被屏蔽了
- Spring的IOC容器负责注入Mapper接口
- Spring管理声明式事务。
1 准备工作
1.1 物理建模
所需工具:
工具 | 版本 |
---|---|
IDEA | 2021.3 |
mysql | 5.7 |
maven | 3.8.4 |
物理建模顾名思义就是在数据库中创建一个具体的table表,在数据库中实实在在存在的。本文以t-emp表为例。
下图给出数据库名称和表中字段名。
1.2 创建Project 或者 module
在idea中创建对应工程或者模块
包名 | 作用 |
---|---|
entity | 存放实体类 |
handler | 存放控制器类 |
service | 存放service类和其实现类 |
mapper | 存放mapper接口 |
test | 存放测试类 |
1.3 逻辑建模
在entity
包中创建和数据库对应的实体类Employee
代码示例如下:
package com.atguigu.ssm.entity; public class Emp {
<span class="token keyword">private</span> <span class="token class-name">Integer</span> empId<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">String</span> empName<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">Double</span> empSalary<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token class-name">Emp</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">Emp</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> empId<span class="token punctuation">,</span> <span class="token class-name">String</span> empName<span class="token punctuation">,</span> <span class="token class-name">Double</span> empSalary<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>empId <span class="token operator">=</span> empId<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>empName <span class="token operator">=</span> empName<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>empSalary <span class="token operator">=</span> empSalary<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">Integer</span> <span class="token function">getEmpId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> empId<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEmpId</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> empId<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>empId <span class="token operator">=</span> empId<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getEmpName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> empName<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEmpName</span><span class="token punctuation">(</span><span class="token class-name">String</span> empName<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>empName <span class="token operator">=</span> empName<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">Double</span> <span class="token function">getEmpSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> empSalary<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEmpSalary</span><span class="token punctuation">(</span><span class="token class-name">Double</span> empSalary<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>empSalary <span class="token operator">=</span> empSalary<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token string">"Emp{"</span> <span class="token operator">+</span> <span class="token string">"empId="</span> <span class="token operator">+</span> empId <span class="token operator">+</span> <span class="token string">", empName='"</span> <span class="token operator">+</span> empName <span class="token operator">+</span> <span class="token char">'\''</span> <span class="token operator">+</span> <span class="token string">", empSalary="</span> <span class="token operator">+</span> empSalary <span class="token operator">+</span> <span class="token char">'}'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
1.4 引入依赖
将整个SSM框架整合所需依赖添加,这里属于基础依赖,后续其他依赖需要按相应功能使用添加。
<dependencies>
<span class="token comment"><!-- SpringMVC --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-webmvc<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.3.1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- Spring 持久化层所需依赖 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-orm<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.3.1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 日志 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>ch.qos.logback<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>logback-classic<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.2.3<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- ServletAPI --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>javax.servlet<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>javax.servlet-api<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>3.1.0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>scope</span><span class="token punctuation">></span></span>provided<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>scope</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- Spring5和Thymeleaf整合包 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.thymeleaf<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>thymeleaf-spring5<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>3.0.12.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- Mybatis核心 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.mybatis<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>mybatis<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>3.5.7<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- MySQL驱动 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.1.3<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 数据源 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>druid<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.0.31<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- junit5 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.junit.jupiter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>junit-jupiter-api<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.7.0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>scope</span><span class="token punctuation">></span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>scope</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- Spring 的测试功能 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-test<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.3.1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- Mybatis 和 Spring 的整合包 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.mybatis<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>mybatis-spring<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>2.0.6<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!-- MyBatis分页插件 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>com.github.pagehelper<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>pagehelper<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.2.0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span>
1.5 加入日志配置文件 logback.xml
加入配置文件主要是为了能够调试代码,输出日志,进而了解异常等问题所在根源。
注意:配置文件全部放在resources目录下。
loback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="true"> <!-- 指定日志输出的位置 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder>
<span class="token comment"><!-- 日志输出的格式 --></span> <span class="token comment"><!-- 按照顺序分别是:时间、日志级别、线程名称、打印日志的类、日志主体内容、换行 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>pattern</span><span class="token punctuation">></span></span>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>pattern</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>encoder</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>appender</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 设置全局日志级别。日志级别按顺序分别是:DEBUG、INFO、WARN、ERROR --></span> <span class="token comment"><!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>root</span> <span class="token attr-name">level</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>INFO<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!-- 指定打印日志的appender,这里通过“STDOUT”引用了前面配置的appender --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>appender-ref</span> <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>STDOUT<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>root</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 根据特殊需求指定局部日志级别 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>logger</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.springframework.web.servlet.DispatcherServlet<span class="token punctuation">"</span></span> <span class="token attr-name">level</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>DEBUG<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
</configuration>
全局日志级别我们一般都设置为debug。其余根据需求进行设置。比如说test测试类。
2 连接数据库
2.1 创建jdbc.properties文件
代码如下(示例):
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:13306/mybatis_example
jdbc.username=root
jdbc.password=abc123
2.2 创建 Spring 配置文件
spring-presist.xml
创建spring-persist.xml配置文件引入jdbc.properties要注意引入context空间,如果不引入,接下来的操作会报各种错误。比如说:
2.3 在 Spring 配置文件中引入 jdbc.properties
<!-- 引入外部属性文件 jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
2.4 在 Spring 配置文件中配置数据源
<!--配置数据源--> <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>driverClassName<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.driver}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>url<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.url}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>username<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.user}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>password<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.password}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span>
注意:这里使用的是druid 数据库连接池 DruidDataSource
并且 必须写成 driverClassName
2.5 创建Junit测试类测试
① 在日志配置文件中为测试类设置日志级别
<logger name="com.atguigu.ssm.test.SSMTest" level="DEBUG"/>
② 在 Junit 测试中装配数据源
@Autowired
private DataSource dataSource;
注意:引入的是import javax.sql.DataSource;
③ 类上面添加 @SpringJUnitConfig
@SpringJUnitConfig(locations = {"classpath:spring-persist.xml"}) public class SSMTest {
@SpringJUnitConfig -----spring整合junit5单元测试注解,指定spring配置文件位置,自动加载上下文。
完整代码如下:
@SpringJUnitConfig(locations = {"classpath:spring-persist.xml"}) public class SSMTest {
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">DataSource</span> dataSource<span class="token punctuation">;</span> <span class="token comment">//使用日志进行输出,需要在logback.xml中设置类的日志级别为DEBUG</span> <span class="token class-name">Logger</span> logger <span class="token operator">=</span> <span class="token class-name">LoggerFactory</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testConn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">Connection</span> connection <span class="token operator">=</span> dataSource<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//使用日志级别输出</span> logger<span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span>connection<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
注意:
我们使用的日志文件导入的包是:org.slf4j.Logge
3 Spring整合MyBatis
3.1 框架环境层面
3.1.1 创建 MyBatis 的全局配置文件
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>
<span class="token comment"><!-- Mybatis全局配置 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>settings</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 将数据库表字段映射到驼峰式命名的Java实体类属性中 --></span> <span class="token comment"><!-- 数据库表字段格式:单词_单词 --></span> <span class="token comment"><!-- Java实体类属性:首字母小写的驼峰式命名 --></span> <span class="token comment"><!--将_自动映射为驼峰,emp_name: empName--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>setting</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mapUnderscoreToCamelCase<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>settings</span><span class="token punctuation">></span></span> <span class="token comment"><!--指定实体类别名--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>typeAliases</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>package</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.atguigu.ssm.entity<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>typeAliases</span><span class="token punctuation">></span></span>
</configuration>
3.1.2 在 Spring 配置文件中配置 SqlSessionFactoryBean
①指定mybatis全局配置文件的位置
- 方式一 configLocation
<!-- 指定MyBatis全局配置文件位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"/>
- 方式二 configuration
<!--舍弃MyBatis 全局配置文件,使用 configuration 属性--> <property name="configuration"> <bean class="org.apache.ibatis.session.Configuration"> <!-- 将_自动映射为驼峰,emp_name: emp_Name--> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> </property> <!--舍弃MyBatis 全局配置文件,使用 typeAliasesPackage 属性配置实体类所在包--> <!--指定实体类别名--> <property name="typeAliasesPackage" value="com/atguigu/ssm/entity"/>
② 指定 Mapper 配置文件 /*Mapper.xml 的位置
<!-- 指定 Mapper 配置文件位置 --> <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/>
/*Mapper.xml代表对所有的mapper配置文件有效
③装配数据源
<!-- 装配数据源 -->
<property name="dataSource" ref="druidDataSource"/>
3.1.3 把 Mapper 接口所在的包扫描到IOC容器
- 方式一 mybatis-spring:scan
<!--使用 mybatis-spring 名称空间--> <mybatis-spring:scan base-package="com.atguigu.ssm.mapper"/>
- 方式二 mapperScannerConfigurer
<!-- 配置 Mapper 接口类型的bean的扫描器 --> <!--使用扫描器--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.atguigu.ssm.mapper"/> </bean>
完整配置如下:
<!--配置sqlSessionFactoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<span class="token comment"><!-- 指定MyBatis全局配置文件位置 --></span> <span class="token comment"><!-- <property name="configLocation" value="classpath:mybatis-config.xml"/>--></span> <span class="token comment"><!--舍弃MyBatis 全局配置文件,使用 configuration 属性--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>configuration<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.apache.ibatis.session.Configuration<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!-- 将_自动映射为驼峰,emp_name: emp_Name--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mapUnderscoreToCamelCase<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token comment"><!--舍弃MyBatis 全局配置文件,使用 typeAliasesPackage 属性配置实体类所在包--></span> <span class="token comment"><!--指定实体类别名--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>typeAliasesPackage<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com/atguigu/ssm/entity<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment"><!-- 指定 Mapper 配置文件位置 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mapperLocations<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>classpath:mappers/*Mapper.xml<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment"><!-- 装配数据源 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>dataSource<span class="token punctuation">"</span></span> <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>druidDataSource<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment"><!-- 配置 Mapper 接口类型的bean的扫描器 --></span> <span class="token comment"><!--使用扫描器--></span> <span class="token comment"><!--<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">--></span> <span class="token comment"><!-- <property name="basePackage" value="com.atguigu.ssm.mapper"/>--></span> <span class="token comment"><!--</bean>--></span> <span class="token comment"><!--使用 mybatis-spring 名称空间--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">mybatis-spring:</span>scan</span> <span class="token attr-name">base-package</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.atguigu.ssm.mapper<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
3.2 具体功能层面
① 创建Mapper接口
public interface EmpMapper {
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Emp</span><span class="token punctuation">></span></span> <span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
② 创建Mapper配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--mapper是根标签,namespace属性是找到当前配置的依据-->
<!--由于是最理想的MyBatis使用方式:通过Mapper接口调用接口方法,访问数据库-->
<!--这样的理想方式要求:能过通过接口全类名找到Mapper配置-->
<!--所以:我们就用Mapper接口的全类名来给namespace属性赋值-->
<mapper namespace="com.atguigu.ssm.mapper.EmpMapper"><span class="token comment"><!--List<Emp> selectAll();--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select emp_id,emp_name,emp_salary from t_emp <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span>
③ 在 Junit 测试类中装配 Mapper 接口并测试
@SpringJUnitConfig(locations = {"classpath:spring-persist.xml"}) public class SSMTest {
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">DataSource</span> dataSource<span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">EmpMapper</span> empMapper<span class="token punctuation">;</span> <span class="token comment">//使用日志进行输出,需要在logback.xml中设置类的日志级别为DEBUG</span> <span class="token class-name">Logger</span> logger <span class="token operator">=</span> <span class="token class-name">LoggerFactory</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testTx</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Emp</span><span class="token punctuation">></span></span> empList <span class="token operator">=</span> empService<span class="token punctuation">.</span><span class="token function">getAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Emp</span> emp <span class="token operator">:</span> empList<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"emp = "</span> <span class="token operator">+</span> emp<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//打 soutv 字母即为快捷键,可以快速生成foreach循环</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
4 配置Spring声明式事务
4.1 框架环境层面
① 在 Spring 的配置文件中配置事务管理器
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 装配数据源 -->
<property name="dataSource" ref="druidDataSource"/>
</bean>
② 在 Spring 的配置文件中开启基于 注解的声明式事务
<!-- 开启基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
注意:引入tx名称空间时千万注意名称空间的值
③ 配置针对 Service 类的自动扫描的包
<!--配置自动扫描的包-->
<context:component-scan base-package="com.atguigu.ssm.service"/>
4.2 具体功能层面
4.2.1 创建 Service 接口
public interface EmpService {
List<Emp> getAll();
4.2.2 创建 Service 接口实现类
@Service public class EmpServiceImpl implements EmpService { @Autowired private EmpMapper empMapper;
<span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Emp</span><span class="token punctuation">></span></span> <span class="token function">getAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> empMapper<span class="token punctuation">.</span><span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
4.3 在需要使用事务的方法上加 @Transactional 注解
@Transactional(readOnly = true) //为当前业务层添加事务 “只读” public List<Emp> getAll() {
4.4 在 Junit 测试类中装配 Service 接口并测试
@SpringJUnitConfig(locations = {"classpath:spring-persist.xml"}) public class SSMTest {
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">DataSource</span> dataSource<span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">EmpMapper</span> empMapper<span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">EmpService</span> empService<span class="token punctuation">;</span> <span class="token comment">//使用日志进行输出,需要在logback.xml中设置类的日志级别为DEBUG</span> <span class="token class-name">Logger</span> logger <span class="token operator">=</span> <span class="token class-name">LoggerFactory</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testTx</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Emp</span><span class="token punctuation">></span></span> empList <span class="token operator">=</span> empService<span class="token punctuation">.</span><span class="token function">getAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Emp</span> emp <span class="token operator">:</span> empList<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"emp = "</span> <span class="token operator">+</span> emp<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//打 soutv 字母即为快捷键,可以快速生成foreach循环</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
注意
需要使用 @Autowired注解装配service接口
5 最终bean图
二、SSM整合搭建环境表述层操作(spring和springMVC)
本质:
- ContextLoaderListener:读取 spring-persist.xml
- DispatcherServlet:读取 spring-mvc.xml
1 生成web.xml
在pom.xml中将当前module的打包方式修改为war。
<packaging>war</packaging>
2 配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<span class="token comment"><!-- ContextLoaderListener --></span> <span class="token comment"><!-- 通过 context-param 指定 spring 框架的配置文件位置 spring-persist.xml --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>context-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-name</span><span class="token punctuation">></span></span>contextConfigLocation<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-value</span><span class="token punctuation">></span></span>classpath:spring-persist.xml<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>context-param</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 配置 ContextLoaderListener 监听器 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>listener</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>listener-class</span><span class="token punctuation">></span></span>org.springframework.web.context.ContextLoaderListener<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>listener-class</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>listener</span><span class="token punctuation">></span></span> <span class="token comment"><!-- DispatcherServlet 读取spring-mvc.xml配置文件位置 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>servlet</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>servlet-name</span><span class="token punctuation">></span></span>dispatcherServlet<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>servlet-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>servlet-class</span><span class="token punctuation">></span></span>org.springframework.web.servlet.DispatcherServlet<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>servlet-class</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-name</span><span class="token punctuation">></span></span>contextConfigLocation<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-value</span><span class="token punctuation">></span></span>classpath:spring-mvc.xml<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>load-on-startup</span><span class="token punctuation">></span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>load-on-startup</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>servlet</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>servlet-mapping</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>servlet-name</span><span class="token punctuation">></span></span>dispatcherServlet<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>servlet-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>url-pattern</span><span class="token punctuation">></span></span>/<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>url-pattern</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>servlet-mapping</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 需要注意两个 Filter 的顺序:字符集过滤器在前,转换请求方式过滤器在后 --></span> <span class="token comment"><!-- CharacterEncodingFilter 设置字符集 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-name</span><span class="token punctuation">></span></span>characterEncodingFilter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-class</span><span class="token punctuation">></span></span>org.springframework.web.filter.CharacterEncodingFilter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-class</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-name</span><span class="token punctuation">></span></span>encoding<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-value</span><span class="token punctuation">></span></span>UTF-8<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-name</span><span class="token punctuation">></span></span>forceRequestEncoding<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-value</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-name</span><span class="token punctuation">></span></span>forceResponseEncoding<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>param-value</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>param-value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>init-param</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-mapping</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-name</span><span class="token punctuation">></span></span>characterEncodingFilter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>url-pattern</span><span class="token punctuation">></span></span>/*<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>url-pattern</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-mapping</span><span class="token punctuation">></span></span> <span class="token comment"><!-- HiddenHttpMethodFilter 把 POST 请求转换为 PUT 或 DELETE --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-name</span><span class="token punctuation">></span></span>hiddenHttpMethodFilter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-class</span><span class="token punctuation">></span></span>org.springframework.web.filter.HiddenHttpMethodFilter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-class</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-mapping</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>filter-name</span><span class="token punctuation">></span></span>hiddenHttpMethodFilter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>url-pattern</span><span class="token punctuation">></span></span>/*<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>url-pattern</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>filter-mapping</span><span class="token punctuation">></span></span>
</web-app>
3 配置SpringMVC
<?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">
<span class="token comment"><!-- springMVC 只扫描 handler 类所在的包--></span> <span class="token comment"><!-- spring 和 springMVC 扫描各自负责的组件,扫描的范围没有重合的部分,直接避免了重复创建的对象--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>component-scan</span> <span class="token attr-name">base-package</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.atguigu.ssm.handler<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment"><!--配置 Thymeleaf 的视图解析器--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>thymeleafViewResolver<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.thymeleaf.spring5.view.ThymeleafViewResolver<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>order<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>1<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>characterEncoding<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>templateEngine<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.thymeleaf.spring5.SpringTemplateEngine<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>templateResolver<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>prefix<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/WEB-INF/templates/<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>suffix<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>.html<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>characterEncoding<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>templateMode<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>HTML5<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span> <span class="token comment"><!--SpringMVC 注解驱动(标配)--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">mvc:</span>annotation-driven</span><span class="token punctuation">/></span></span> <span class="token comment"><!--对于没有映射的请求直接转发放行,主要是静态资源--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">mvc:</span>default-servlet-handler</span><span class="token punctuation">/></span></span>
</beans>
4 显示首页
① 创建首页对应的视图模板文件
- portal.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body style="text-align: center"> <h1>首页</h1> </body> </html>
② 在 spring-mvc.xml 中进行配置
<!--匹配请求路径直接前往视图,不经过 handler 方法
path :设置处理的请求地址
view-name:设置请求地址所对应的视图名称
-->
<mvc:view-controller path="/" view-name="portal"/>
5 最终bean 图
小结:
经过以上SSM整合的准备工作后,主要是对框架环境的搭建,下面开始进行分页显示数据以及基本的增删改查操作。
三、具体功能开发
下面的操作使用 restful 风格,具体功能清单
功能 | URL地址 | 请求方式 |
---|---|---|
访问首页 | / | view-controller |
查询全部数据 | /get/all | GET |
查询分页数据 | /get/page/{pageNo} | GET |
删除 | /emp/{empId}/{pageNo} | DELETE |
跳转到添加页面 | /emp/add | view-controller |
提交表单(执行保存) | /emp | POST |
跳转到更新页面 | /emp/{empId}/{pageNo} | GET |
执行更新 | /emp | PUT |
1 显示数据列表(全部数据)
1.1 创建 EmpHandler 类
@Controller public class EmpHandler {
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">EmpService</span> empService<span class="token punctuation">;</span>
为了让springMVC找到控制器,当前的控制器就必须是IOC容器中的组件,因此需要加上注解 @Controller
1.2 在 EmpHandler 类中声明处理请求的方法
前面我们在进行测试的时候就已经将mapper接口和mapper.xml以及empService接口及实现类中查询全部数据的代码已经完成了,现在我们只需要编写EmpHandler。
@Controller public class EmpHandler {
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">EmpService</span> empService<span class="token punctuation">;</span> <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/get/all"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getAll</span><span class="token punctuation">(</span><span class="token class-name">Model</span> model<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//1.查询数据</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Emp</span><span class="token punctuation">></span></span> empList <span class="token operator">=</span> empService<span class="token punctuation">.</span><span class="token function">getAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.存入模型</span> model<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"empList"</span><span class="token punctuation">,</span> empList<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token string">"emp-list"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
1.3 创建逻辑视图对应的视图模板页面
① 首先在首页先设置一个超链接能够跳转到显示全部数据的页面
portal.xml
<a th:href="@{/get/all}">显示全部数据</a><br>
② 创建 emp-list.html页面
③ 从请求域中读取要显示的数据
从请求域中获取请求参数:
${ }
内置对象:
#lists
④ 遍历数据
th:each
emp-list.html完整代码如下:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> table { border-collapse: collapse; margin: 0px auto 0px auto; }
<span class="token selector">table th, td</span> <span class="token punctuation">{<!-- --></span> <span class="token property">border</span><span class="token punctuation">:</span> 1px solid black<span class="token punctuation">;</span> <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span> <span class="token punctuation">}</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>style</span><span class="token punctuation">></span></span>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>SALARY</th>
</tr>
<tbody th:if="${#lists.isEmpty(empList)}">
<tr>
<td colspan="3">抱歉,没有查询到任何数据!</td>
</tr>
</tbody>
<tbody th:if="${not #lists.isEmpty(empList)}">
<tr th:each="emp : ${empList}">
<td th:text="${emp.empId}">这里显示员工ID</td>
<td th:text="${emp.empName}">这里显示员工Name</td>
<td th:text="${emp.empSalary}">这里显示员工SALARY</td>
</tr>
</tbody>
</table><a th:href="@{/}">回首页</a>
</body>
</html>
添加了一个回到首页的超链接
提出想法:
当数据很多的时候,我们适当分页并设置简单导航来使得查看数据更加便捷。
2 分页显示数据列表
注意:
这里不详细介绍,可以去相关链接查看,此处只做相关功能测试。
代码重工-分页相关知识
2.1 准备环境
① 导入依赖
在pom.xml配置文件中
<!-- MyBatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
② 在 SqlSessionFactoryBean 中配置 PageHelper 插件
<!-- 在 plugins 属性中配置 MyBatis插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <props> <!-- 设置 reasonable 为 true 表示将页码进行合理化修正。页码的有效范围:1~总页数 --> <prop key="reasonable">true</prop>
<span class="token comment"><!-- 数据库方言:同样都是 SQl 语句,拿到不同数据库中,在语法上会有差异 --></span> <span class="token comment"><!-- 默认情况下,按照 MySQL 作为数据库方言来运行 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>prop</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>helperDialect<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>prop</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>props</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>array</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span>
2.2 开发功能
① 在首页中添加一个超链接,需要考虑页码值传入
<a th:href="@{/get/page/1}">显示分页数据</a><br/>
rest风格: @{/get/page/{pageNo}}
这里我们默认设置点击超链接,跳转页面显示从第一页开始。
② 在 EmpHandler 声明 handler 方法
- EmpHandler 控制层
//显示分页数据 @RequestMapping("/get/page/{pageNo}") public String getPage( @PathVariable("pageNo") Integer pageNo, Model model) {
<span class="token comment">//1. PageInfo 对象封装了和分页相关的所有信息</span> <span class="token class-name">PageInfo</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Emp</span><span class="token punctuation">></span></span> pageInfo <span class="token operator">=</span> empService<span class="token punctuation">.</span><span class="token function">getPageInfo</span><span class="token punctuation">(</span>pageNo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.将 PageInfo 对象存入模型</span> model<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"pageInfo"</span><span class="token punctuation">,</span> pageInfo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3.返回页面</span> <span class="token keyword">return</span> <span class="token string">"emp-page"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
③在 Service 方法实现分页
- EmpService接口
PageInfo<Emp> getPageInfo(Integer pageNo);
- EmpServiceImpl实现类
@Override public PageInfo<Emp> getPageInfo(Integer pageNo) {
<span class="token comment">//1、确定每页显示数据的条数</span> <span class="token keyword">int</span> pageSize <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span> <span class="token comment">//2、设定分页数据:开启分页功能。开启后,后面执行的 SELECT 语句会自动被附加 LIMIT 子句,</span> <span class="token comment">//而且会自动查询总记录数</span> <span class="token class-name">PageHelper</span><span class="token punctuation">.</span><span class="token function">startPage</span><span class="token punctuation">(</span>pageNo<span class="token punctuation">,</span> pageSize<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3、正常执行查询</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Emp</span><span class="token punctuation">></span></span> empList <span class="token operator">=</span> empMapper<span class="token punctuation">.</span><span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4、封装为 PageInfo 对象返回</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">PageInfo</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span>empList<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
mapper接口和mapper.xml配置文件不用配置,利用查询数据的方法,只是做了分页显示。
④ 完成页面显示
创建 emp-page.html
- 显示主体数据
pageInfo.list
<tbody th:if="${#lists.isEmpty(pageInfo.list)}"> <tr> <td colspan="4">抱歉,没有查询到任何数据!</td> </tr> </tbody>
<tbody th:if="${not #lists.isEmpty(pageInfo.list)}">
<tr th:each="emp : ${pageInfo.list}">
<td th:text="${emp.empId}">这里显示员工ID</td>
<td th:text="${emp.empName}">这里显示员工Name</td>
<td th:text="${emp.empSalary}">这里显示员工SALARY</td>
</tr>
</tbody>
- 显示简单导航
pageInfo.hasPreviousPage
&pageInfo.hasNextPage
<span th:if="${pageInfo.hasPreviousPage}"> <a th:href="@{/get/page/1}">首页</a> <a th:href="@{/get/page/}+${pageInfo.prePage}">上一页</a> </span>
<span th:if="${pageInfo.hasNextPage}">
<a th:href="@{/get/page/}+${pageInfo.nextPage}">下一页</a>
<a th:href="@{/get/page/}+${pageInfo.pages}">最后一页</a>
</span>
<span th:text="${pageInfo.pageNum}+'/'+${pageInfo.pages}"></span>
- 显示导航数字
navigator : ${pageInfo.navigatepageNums}
<span th:each="navigator : ${pageInfo.navigatepageNums}"> <!-- th:if 判断当前标签是否是当前页 --> <!-- 如果不是当前页则显示超链接 --> <a th:if="${navigator != pageInfo.pageNum}" th:href="@{/get/page/}+${navigator}" th:text="'['+${navigator}+']'"></a> </span>
<span class="token comment"><!-- 如果是当前页则显示页码本身 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name"><span class="token namespace">th:</span>if</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${navigator == pageInfo.pageNum}<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">th:</span>text</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">'</span>[<span class="token punctuation">'</span>+${navigator}+<span class="token punctuation">'</span>]<span class="token punctuation">'</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
- 通过输入文本框跳转到指定页面
输入文本框
<tr>
<td colspan="4">
<!--绑定值改变事件响应函数-->
<!--placeholder属性:对用户做一个对输入内容的提醒或者指引-->
<input id="jumpToPageNumInput" @click="jumpToPageNumInput" type="text" name="jumpToPageNum" placeholder="请输入你想直接跳转的页码"/>
</td>
</tr>
配套js(使用了vue)
引入vue.js
<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
编辑点击响应函数
<script type="text/javascript"> var vue = new Vue({ el: "#pageTable", // el 指定Vue对象关联的HTML元素的id methods: { //配合 v-on 声明事件响应函数 jumpToPageNumInput:function (event) {//表示当前事件 //获取页码文本框的元素对象 var jumpToPageNum = document.getElementById("jumpToPageNumInput"); //给页码文本框绑定值改变响应函数 jumpToPageNum.onchange = function (){ //触发值改变响应函数后,获取当前文本框中用户输入的值 var targetNum = this.value;
<span class="token comment">//检测用户输入的数据是否是数字</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isNaN</span><span class="token punctuation">(</span>targetNum<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//如果不是数字则恢复文本框</span> <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token comment">//当前函数结束</span> <span class="token keyword">return</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//如果用户输入合法,则跳转页面</span> <span class="token comment">//[[@{/get/page/}]] 表示解析 thymeleaf 表达式</span> window<span class="token punctuation">.</span>location<span class="token punctuation">.</span>href <span class="token operator">=</span> <span class="token string">"[[@{/get/page/}]]"</span> <span class="token operator">+</span> targetNum<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span>
3 删除操作
3.1 在页面上给每一条记录编写超链接
<!-- v-on:click 表示绑定点击事件 -->
<!-- v-on 可以省略,变成 @click -->
<a class="deleteA" @click="deleteEmployee"
th:href="@{/emp/}+${emp.empId}+'/'+${pageInfo.pageNum}">delete</a>
3.2 转换请求方式:把点击超链接的 GET 请求转换为最终的 DELETE 请求
① 编写超链接对应的 handler 方法
<!-- 作用:通过超链接控制表单的提交,将post请求转换为delete请求 -->
<form id="delete_form" method="post">
<!-- HiddenHttpMethodFilter要求:必须传输_method请求参数,并且值为最终的请求方式 -->
<input type="hidden" name="_method" value="delete"/>
</form>
② 给每一个删除超链接绑定单击响应函数 @click
- 引入vue.js(第一种方式)
<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
- 配套Js
<script type="text/javascript">
var vue = new Vue({
el: "#pageTable", // el 指定Vue对象关联的HTML元素的id
methods: { //配合 v-on 声明事件响应函数
deleteEmp: function (event) { //event 表示当前事件
//通过document对象在整个文档范围内查找对象
//使用var 关键字接收document.getElementById("delete_form")方法的返回值
var delete_form = document.getElementById("delete_form");
//将触发事件的超链接的href属性为表单的action属性赋值
delete_form.action = event.target.href;
//提交表单
delete_form.submit();
//阻止超链接的默认跳转行为(点击超链接,会跳转页面,这是超链接的默认行为,需取消。)
event.preventDefault();
}
}
});
</script>
- 不使用vue(第二种方式:原生Js)
超链接
<!--/emp/{empId}/{pageNo} -->
<!--οnclick="convertMethod(this)" 表示点击这个超链接时,调用 convertMethod() 函数 -->
<!--this 代表当前超链接对象 -->
<!--event 是代表当前事件的事件对象 -->
<a onclick="convertMethod(this, event)" th:href="@{/emp/}+${emp.empId}+'/'+${pageInfo.pageNum}">删除</a>
配套js
<script type="text/javascript">
<span class="token keyword">function</span> <span class="token function">convertMethod</span><span class="token punctuation">(</span><span class="token parameter">anchorElement<span class="token punctuation">,</span> event</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">// 获取超链接原本要访问的目标地址</span> <span class="token keyword">var</span> targetURL <span class="token operator">=</span> anchorElement<span class="token punctuation">.</span>href<span class="token punctuation">;</span> <span class="token comment">// 获取表单对象</span> <span class="token keyword">var</span> delete_form <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"delete_form"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 把超链接原本要访问的地址设置给表单的 action 属性</span> delete_form<span class="token punctuation">.</span>action <span class="token operator">=</span> targetURL<span class="token punctuation">;</span> <span class="token comment">// 提交表单</span> delete_form<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 取消控件的默认行为:让超链接不会跳转</span> event<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span>
③编写超链接对应的 handler 方法
@RequestMapping(value = "/emp/{empId}/{pageNo}", method = RequestMethod.DELETE) // @DeleteMapping("/emp/{empId}/{pageNo}") public String deleteEmployee( @PathVariable("empId") Integer empId, @PathVariable("pageNo") Integer pageNo) {
<span class="token comment">// 调用 Service 方法执行删除</span> empService<span class="token punctuation">.</span><span class="token function">deleteEmployee</span><span class="token punctuation">(</span>empId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 以重定向方式回到分页页面,避免刷新浏览器重新执行删除操作</span> <span class="token keyword">return</span> <span class="token string">"redirect:/get/page/"</span> <span class="token operator">+</span> pageNo<span class="token punctuation">;</span> <span class="token punctuation">}</span>
④编写 handler 方法要调用的 Service 方法
- service接口 EmpService
void deleteEmployee(@Param("empId") Integer empId);
- service实现类 EmpServiceImpl
@Transactional( propagation = Propagation.REQUIRES_NEW, //设置事务传播行为 :当前方法必须启动新事务,并在它自己的事务内运行,如果有事务正在运行,应该将它挂起 rollbackFor = Exception.class //出现哪些异常可以回滚 ) @Override public void deleteEmployee(Integer empId) {
empMapper<span class="token punctuation">.</span><span class="token function">deleteEmployee</span><span class="token punctuation">(</span>empId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
④编写Mapper及其相关配置
- EmpMapper
void deleteEmployee(Integer empId);
- EmpMapper.xml
<!-- void deleteEmployee(Integer empId);--> <delete id="deleteEmployee"> delete from t_emp where emp_id = #{empId} </delete>
4 新增操作
4.1 跳转到表单页面(emp-add.html)
① 编写超链接
在表头处添加一个添加按钮
<tr>
<th>OPTIONS(<a th:href="@{/emp/add}">add</a>)</th>
</tr>
② 配置view-controller
<mvc:view-controller path="/emp/add" view-name="emp-add"/>
③ 创建emp-add.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/emp}" method="post">
姓名:<input type="text" name="empName"><br/>
工资:<input type="text" name="empSalary"><br/>
<button type="submit">保存</button>
</form>
</body>
</html>
4.2 提交表单
①handler方法
//提交添加的表单(保存数据),默认前往最后一页显示 @RequestMapping(value = "/emp",method = RequestMethod.POST) public String addEmp(){
empService<span class="token punctuation">.</span><span class="token function">addEmployee</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token string">"redirect:/get/page/"</span><span class="token operator">+</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span> <span class="token punctuation">}</span>
② service方法
service接口
void addEmp(Emp emp);
service实现类
@Transactional(
propagation = Propagation.REQUIRES_NEW, //设置事务传播行为 :当前方法必须启动新事务,并在它自己的事务内运行,如果有事务正在运行,应该将它挂起
rollbackFor = Exception.class //出现哪些异常可以回滚
)
@Override
public void addEmp(Emp emp) {
empMapper.addEmp(emp);
}
③ Mapper及其配置文件
EmpMapper
void addEmp(Emp emp);
EmpMapper.xml
<!--void addEmp(Emp emp);-->
<insert id="addEmp">
insert into t_emp(emp_name,emp_salary) values(#{empName},#{empSalary})
</insert>
5 更新操作
5.1 跳转到表单页面,回显数据
① 编写超链接
<a th:href="@{/emp/}+${emp.empId}+'/'+${pageInfo.pageNum}">update</a>
② handler方法
//更新操作一:回显数据(在表单中显示数据,相当于查询,所以是GET) @RequestMapping(value = "/emp/{empId}/{pageNo}",method = RequestMethod.GET) public String updateEmp( @PathVariable("empId") Integer empId, @PathVariable("pageNo") Integer pageNo, Model model){
<span class="token comment">//1.根据 empId 查询得到用来回显表单数据的实体类对象</span> <span class="token class-name">Emp</span> emp <span class="token operator">=</span> empService<span class="token punctuation">.</span><span class="token function">selectEmpById</span><span class="token punctuation">(</span>empId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.存入模型中,即向域对象中共享数据,方便获取</span> model<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"emp"</span><span class="token punctuation">,</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3.返回表单页面修改 emp-edit ,进行修改操作</span> <span class="token keyword">return</span> <span class="token string">"emp-edit"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
③ service方法
- service接口
Emp getEmpById(Integer empId);
- service接口实现类
@Transactional(readOnly = true) @Override public Emp getEmpById(Integer empId) { return empMapper.getEmpById(empId);
<span class="token punctuation">}</span>
④ Mapper及其配置文件
- EmpMapper
Emp getEmpById(Integer empId);
- 配置文件
<!--Emp getEmpById(Integer empId);-->
<select id="getEmpById" resultType="Emp">
select emp_id,emp_name,emp_salary from t_emp where emp_id = #{empId}
</select>
5.2 提交表单
①创建emp-edit 页面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form th:action="@{/emp}" method="post"> <input type="hidden" name="_method" value="put"> <input type="hidden" name="pageNo" th:value="${pageNo}"> <input type="hidden" name="empId" th:value="${emp.empId}">
姓名:<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>empName<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">th:</span>value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${emp.empName}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>br</span><span class="token punctuation">/></span></span> 工资:<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>empSalary<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">th:</span>value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${emp.empSalary}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>br</span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>button</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>submit<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>更新<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>button</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>form</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span>
</html>
② 编写handler 方法
//更新操作二:提交表单 @RequestMapping(value = "/emp",method = RequestMethod.PUT) public String update(Emp emp, /* 因为请求路径中没有携带pageNo(占位符中没有) 所以不能通过@PathVariable来给控制器方法的形参赋值,所以只能通过@RequestParam将请求参数和控制器的 形参创建映射关系,因此需要我们在提交表单的时候,将pageNo随表单一起提交给服务器 因此需要有一个隐藏域来传输pageNo */ @RequestParam("pageNo") Integer pageNo){ empService.update(emp);
<span class="token keyword">return</span> <span class="token string">"redirect:/get/page/"</span><span class="token operator">+</span>pageNo<span class="token punctuation">;</span> <span class="token punctuation">}</span>
③ 编写service方法
- service接口
void update(Emp emp);
- service实现类
@Transactional( propagation = Propagation.REQUIRES_NEW, //设置事务传播行为 :当前方法必须启动新事务,并在它自己的事务内运行,如果有事务正在运行,应该将它挂起 rollbackFor = Exception.class //出现哪些异常可以回滚 ) @Override public void update(Emp emp) {
empMapper<span class="token punctuation">.</span><span class="token function">updateByEmpId</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
④Mapper及其配置文件
- EmpMapper
void updateByEmpId(Emp emp);
- EmpMapper.xml 编写SQL语句
<!--void updateByEmpId(Emp emp);-->
<update id="updateByEmpId">
update t_emp set emp_name = #{empName},emp_Salary=#{empSalary} where emp_id = #{empId}
</update>
总结 相关知识合集
在完成此案例中可能用到的相关知识合集:
model.addattribute & request.setattribute区别
例如:以上就是今天要讲的内容,本文仅仅简单介绍了SSM整合及其ACRUD操作的使用。