拦截器是SpringMVC中一种对象,能拦截器对Controller的请求。
拦截器框架中有系统的拦截器, 还可以自定义拦截器。 实现对请求预先处理。
实现自定义拦截器:
1.创建实现类 实现SpringMVC框架的HandlerInterceptor接口
public interface HandlerInterceptor {
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
2.需在SpringMVC的配置文件中,声明拦截器
<mvc:interceptors>
<mvc:interceptor>
<bean class="拦截器类全限定名称"/>
mvc:interceptor>
mvc:interceptors>

springboot中如何使用拦截器





在SpringBoot框架中使用Servlet对象。
使用步骤:
1.创建Servlet类。 创建类继承HttpServlet
2.注册Servlet。 让框架能找到Servlet



Filter是Servlet规范中的过滤器,可以处理请求, 对请求的参数, 属性进行调整。
常常在过滤器中处理字符编码
在框架中使用过滤器:





CharacterEncodingFilter: 解决post请求中乱码的问题
在SpringMVC框架, 在web.xml 注册过滤器。 配置他的属性。
在springboot中使用:
第一种方式:
自己创建字符集过滤器,太麻烦了,直接第二种方式。
第二种方式:
修改application.properties文件
springboot默认有字符集过滤器CharacterEncdoingFilter 不需要自己创建
编码方式 默认是ISO-8859-1
server.port=9001
server.servlet.context-path=/myboot
#让系统的CharacterEncdoingFilter生效 默认就是true 可以不写
server.servlet.encoding.enabled=true
#指定使用的编码方式
server.servlet.encoding.charset=utf-8
#强制request,response都使用charset属性的值
server.servlet.encoding.force=true
使用MyBatis框架操作数据, 在SpringBoot框架集成MyBatis
使用步骤:




pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.2version>
<relativePath/>
parent>
<groupId>com.bjpowernodegroupId>
<artifactId>017-springboot-mapperartifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
resources>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>











第一种方式:@Mapper(放在dao接口的上面, 每个接口都需要使用这个注解)
/**
* @Mapper:告诉MyBatis这是dao接口,创建此接口的代理对象。
* 位置:在类的上面
*/
@Mapper
public interface StudentDao {
Student selectById(@Param("stuId") Integer id);
}
弊端:如果有50个接口 就要写50次@Mapper
不在dao接口上写@Mapper
在主类上写 @MapperScan(basePackages = "com.bjpowernode.dao")
basePackages:指定 dao 接口所在的包名。
/**
* @MapperScan: 扫描所有的 mybatis 的 dao 接口
* 位置:在主类的上面
* 属性:basePackages:指定 dao 接口的所在的包名。
* dao 接口和 mapper 文件依然在同一目录
*/
//dao包和mapper包下都有dao接口
@SpringBootApplication
@MapperScan(basePackages = {"com.bjpowernode.dao","com.bjpowernode.mapper"})
public class Application {
}
推荐使用第三种方式
步骤:现在把Mapper文件放在resources目录下
1)在resources目录中创建子目录 (自定义的)例如mapper
2)把mapper文件放到 mapper目录中
3)在application.properties文件中,指定mapper文件的目录
#指定mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#指定mybatis的日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4)在pom.xml中指定 把resources目录中的文件,编译到目标目录中
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.*include>
includes>
resource>
resources>
其他和第二种方式一致
注意要在主类中加 @MapperScan=(basePackages=dao接口所在包)




Spring框架中的事务:
1) 管理事务的对象: 事务管理器(接口, 接口有很多的实现类)
例如:使用Jdbc或mybatis访问数据库,使用的事务管理器:DataSourceTransactionManager
2 ) 声明式事务: 在xml配置文件或者使用注解说明事务控制的内容
控制事务: 隔离级别,传播行为, 超时时间
3)事务处理方式:
1) Spring框架中的@Transactional
2) aspectj框架可以在xml配置文件中,声明事务控制的内容
SpringBoot中使用事务: 上面的两种方式都可以。
1)在业务方法的上面加入@Transactional , 加入注解后,方法有事务功能了。
2)明确的在 主启动类的上面 ,加入@EnableTransactionManager




<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.2version>
<relativePath/>
parent>
<groupId>com.bjpowernodegroupId>
<artifactId>019-springboot-transactionalartifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.*include>
includes>
resource>
resources>
<plugins>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.6version>
<configuration>
<configurationFile>GeneratorMapper.xmlconfigurationFile>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>


DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="D:\tools\mysql-connector-java-8.0.22.jar"/>
<context id="tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
userId="root"
password="123">
jdbcConnection>
<javaModelGenerator targetPackage="com.bjpowernode.model"
targetProject="D:\course\25-SpringBoot\springboot-prj\019-springboot-transactional\src\main\java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.bjpowernode.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
javaClientGenerator>
<table tableName="student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
context>
generatorConfiguration>








接口: API(Application Programming Interface,应用程序接口)
是一些预先定义的接口(如函数、HTTP接口)或指软件系统不同组成部分衔接的约定。
用来提供应用程序与开发人员基于某软件或硬件得以访问的一组例程,而又无需访问源码,或理解内部工作机制的细节。
接口(API):
广义上来说,可以指访问servlet,controller的url,或者调用其他程序的 函数
架构风格: api组织方式(样子)(接口怎么写 格式)
这是一个传统的写法: http://localhost:9002/mytrans/addStudent?name=lisi&age=26
在地址上提供了 访问的资源名称addStudent, 在其后使用了get方式传递参数。

RESTful架构风格
1)REST:
(英文:Representational State Transfer,中文:表现层状态转移)。
REST:是一种接口的架构风格和设计的理念,不是标准。
优点: 更简洁,更有层次
表现层状态转移:
表现层就是视图层,显示资源的,通过视图页面,jsp等等显示操作资源的结果。
状态:资源变化
转移:资源可以变化的。资源能创建,new状态,资源创建后可以查询资源,能看到资源的内容。
这个资源内容,可以被修改,修改后资源 和之前的不一样。
2)REST中的要素:
用REST表示资源和对资源的操作。 在互联网中,表示一个资源或者一个操作。
资源使用url表示的,在互联网,使用的图片,视频,文本,网页等等都是资源。
资源是用名词表示。
对资源的操作:
查询资源: 看,通过url找到资源。
创建资源: 添加资源
更新资源: 更新资源,编辑
删除资源: 去除





一句话说明REST:使用url表示资源,使用http动作操作资源。
Spring Boot 开发 RESTful
主要是由几个注解实现










使用postman



















要保证:请求方式+url 唯一
