• SpringBoot学习笔记(2)——B站动力节点


    003- springboot和web组件

    3.1 springboot中使用 拦截器

    拦截器是SpringMVC中一种对象,能拦截器对Controller的请求。
    
    拦截器框架中有系统的拦截器, 还可以自定义拦截器。 实现对请求预先处理。
    
    实现自定义拦截器:
    
    1.创建实现类 实现SpringMVC框架的HandlerInterceptor接口
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    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 {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.需在SpringMVC的配置文件中,声明拦截器

    <mvc:interceptors>
    	<mvc:interceptor>
        	
            <bean class="拦截器类全限定名称"/>
        mvc:interceptor>
    mvc:interceptors>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述


    springboot中如何使用拦截器
    
    • 1

    在这里插入图片描述在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    3.2 springboot中使用 servlet

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

    在这里插入图片描述在这里插入图片描述在这里插入图片描述

    3.3 springboot中使用 过滤器Filter

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

    在框架中使用过滤器:

    1. 创建自定义过滤器类,实现Filter接口
    2. 注册Filter过滤器对象

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

    3.4 字符集过滤器

    CharacterEncodingFilter: 解决post请求中乱码的问题
    
    在SpringMVC框架, 在web.xml 注册过滤器。 配置他的属性。
    
    在springboot中使用:
    
    第一种方式:
    	自己创建字符集过滤器,太麻烦了,直接第二种方式。
    第二种方式:
    	修改application.properties文件
    	springboot默认有字符集过滤器CharacterEncdoingFilter 不需要自己创建
    	编码方式 默认是ISO-8859-1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    004- ORM 操作 MySQL(springboot集成mybatis)

    4.1 基本操作

    (1)第一种方式:@Mapper

    使用MyBatis框架操作数据, 在SpringBoot框架集成MyBatis
    
    • 1

    使用步骤:

    1. mybatis起步依赖 : 完成mybatis对象自动配置, 对象放在容器中
    2. pom.xml 指定把src/main/java目录中的xml文件包含到classpath中
    3. 创建实体类Student
    4. 创建Dao接口 StudentDao , 创建一个查询学生的方法
    5. 创建Dao接口对应的Mapper文件, 即xml文件, 写sql语句
    6. 创建Service层对象, 创建StudentService接口和他的实现类。 去dao对象的方法。完成数据库的操作
    7. 创建Controller对象,访问Service。
    8. 写application.properties文件。配置数据库的连接信息。

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述在这里插入图片描述


    pom.xml:
    
    • 1
    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    在这里插入图片描述


    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述


    第一种方式:@Mapper(放在dao接口的上面, 每个接口都需要使用这个注解)

    /**
     * @Mapper:告诉MyBatis这是dao接口,创建此接口的代理对象。
     *     位置:在类的上面
     */
    @Mapper
    public interface StudentDao {
    
        Student selectById(@Param("stuId") Integer id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    弊端:如果有50个接口 就要写50次@Mapper
    
    • 1

    (2)第二种方式:@MapperScan

    不在dao接口上写@Mapper
    在主类上写 @MapperScan(basePackages = "com.bjpowernode.dao")
    	basePackages:指定 dao 接口所在的包名。
    
    • 1
    • 2
    • 3
    /**
    * @MapperScan: 扫描所有的 mybatis 的 dao 接口
    * 位置:在主类的上面
    * 属性:basePackages:指定 dao 接口的所在的包名。
    * dao 接口和 mapper 文件依然在同一目录
     */
    //dao包和mapper包下都有dao接口
    @SpringBootApplication
    @MapperScan(basePackages = {"com.bjpowernode.dao","com.bjpowernode.mapper"})
    public class Application {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (3)第三种方式:Mapper文件与Dao接口分开管理

    推荐使用第三种方式

    步骤:现在把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
    
    • 1
    • 2
    • 3
    • 4

    4)在pom.xml中指定 把resources目录中的文件,编译到目标目录中

    
    <resources>
       <resource>
          <directory>src/main/resourcesdirectory>
          <includes>
             <include>**/*.*include>
          includes>
       resource>
    resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    其他和第二种方式一致
    注意要在主类中加 @MapperScan=(basePackages=dao接口所在包)


    在这里插入图片描述在这里插入图片描述在这里插入图片描述
    在这里插入图片描述

    4.2 事务

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    在这里插入图片描述在这里插入图片描述

    
    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    在这里插入图片描述
    在这里插入图片描述


    在这里插入图片描述


    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

    005- 接口架构风格——RESTful

    5.1 认识RESTful

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

    在这里插入图片描述


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

    在这里插入图片描述


    在这里插入图片描述在这里插入图片描述


    在这里插入图片描述


    在这里插入图片描述

    一句话说明REST:使用url表示资源,使用http动作操作资源。

    5.2 RESTful的注解

    (1)基本操作

    Spring Boot 开发 RESTful 
    主要是由几个注解实现
    
    • 1
    • 2

    在这里插入图片描述


    在这里插入图片描述在这里插入图片描述在这里插入图片描述

    在这里插入图片描述


    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述


    在这里插入图片描述

    使用postman
    
    • 1

    在这里插入图片描述在这里插入图片描述

    在这里插入图片描述


    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述


    (2)在页面/ajax中支持put/delete请求

    在这里插入图片描述

    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    (3)注意

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

    在这里插入图片描述

  • 相关阅读:
    WuThreat身份安全云-TVD每日漏洞情报-2023-09-27
    B. Decode String
    【Rust】字符串String类型学习
    [数据结构初阶]一文轻松学链表
    用docker搭建简易ctf题目
    电脑指示灯闪烁,但是无法开机的解决方案
    java连接mysql数据库结构表批量生产word文档
    分布式与微服务区别?
    【小白刷leetcode】第15题
    免杀对抗-反沙盒+反调试
  • 原文地址:https://blog.csdn.net/m0_52041525/article/details/125873385