SprinBoot-黑马-笔记

  • Post author:
  • Post category:其他



SpringBoot—-黑马视频地址

网盘链接:https://pan.baidu.com/s/1z8I4IXEuYBepDYbg4ijzUw

提取码:1234



1. 基础篇



1.快速上手SpringBoot



1.SpringBoot入门程序开发

image-20220601155555169

image-20220601155619770

image-20220601155632264

image-20220601155657703

image-20220601155727581

image-20220601155749767

image-20220601155822958

image-20220601155954551

image-20220601160006110

image-20220601160112650

image-20220601160046931

image-20220601202706790

image-20220601202756569

image-20220601203031507

image-20220601203049764

image-20220601203105532



隐藏指定文件/文件夹

image-20220601203250693



2.浅谈入门程序工作原理

image-20220601204103130

image-20220601204614957


parent

image-20220601204641638


starter

image-20220601204854211

image-20220601204931344

引导类

image-20220601210722297

image-20220601210748904

image-20220601211209100

springboot内置三款服务器

image-20220601211335721

image-20220601211637146

image-20220601212914197



2.Rest风格

image-20220502005643958

image-20220601213737224

image-20220601213753181

image-20220601213812246

@RequestBody @RequestParam @PathVariable

image-20220601214955530

@RestController = @Controller + @ResponseBody



RESTful快速开发

image-20220601215354718

image-20220601215412053

image-20220601215441459



3.基础配置

复制工程时,模板module文件样式:

image-20220502012124841

image-20220601215707695

image-20220601220011342

image-20220601220130886

image-20220601220146882


属性配置方式:

image-20220601221715833

image-20220601221726353

image-20220601221809680

image-20220601221822659

image-20220601221934732



4.Yaml



yaml数据读取

@Value(“${一级属性名.二级属性名}”)



封装整个yaml文件中的属性

//使用自动装配将所有的数据封装到一个对象Environment中
    @Autowired
    private Environment env;
// 使用方式
System.out.println("-----------------------------");
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("user.name"));



封装yaml文件中的单个对象


//1.定义数据模型封装yaml文件中对应的数据
//2.定义为spring管控的bean
@Component
//3.指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
    private String driver;
    private String url;
    private String username;
    private String password;
}
// 省略 toString()、setXxx()、getXxx()
	
//使用
    @Autowired
    private MyDataSource myDataSource;

image-20220601222155666
注意yml文件中的数字信息,eg:

0127 会被当做八进制数 转换为 十进制的 87

.

image-20220508001302020

image-20220601222310552

image-20220601222333159

image-20220601222429862

image-20220601222448450

image-20220601222544298

image-20220508001322611

image-20220601222636619

image-20220601222703188

image-20220601223537489

image-20220601223618842



5.整合第三方技术



整合JUint

image-20220601223732888

image-20220502114708115

image-20220601223756208

image-20220601224139281



整合MyBatis

image-20220601224244699

image-20220601224302775

image-20220601224320481

image-20220601224341631

image-20220601224402458

  1. MySQL 8.X驱动强制要求设置时区
  • 修改url,添加serverTimezone设定
  • 修改MySQL数据库配置
  1. 驱动类过时,提醒更换为com.mysql.cj.jdbc.Driver

image-20220601224443539



整合MyBatis-Plus

MyBatis-Plus是中国人开发的

image-20220601224724223

使用改框架可以节省Dao接口的编写,直接让接口继承

BaseMapper<>

image-20220601224745862

image-20220601224805832



整合Druid

image-20220602100737000

image-20220602100844615

image-20220602100923645

image-20220602100935871



6.基于SpringBoot的SSMP整合案例

image-20220602101949111

image-20220602102043638

使用Lombok简化POJO开发

  1. 导入lombok
  2. @Data注解

image-20220602102815382

image-20220602102830831



数据层开发

image-20220602102905127

image-20220602103102336

image-20220602103149331

image-20220602103216792

image-20220602103230033

image-20220602103350638

image-20220602103408930



分页功能

image-20220502152909729

image-20220602103946315

需要用拦截器实现

@Configuration
public class  MPConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

image-20220602104107621

image-20220602104922837

image-20220602104942399

image-20220602104958538



业务层开发

image-20220602105211092

image-20220602105229658

image-20220602105300108

image-20220602105327571

image-20220602105346947

image-20220502161225467

image-20220602112219937

image-20220602112259389

image-20220602112618597

image-20220602112639665



表现层开发

image-20220602112821883

image-20220602112922363

image-20220602113045708

image-20220602113104019



表现层数据一致性

image-20220602114656268

image-20220602114533348

image-20220602114713670

image-20220602114807102



前后端协议联调

image-20220602115020900

image-20220602115036597

image-20220602115108678

image-20220602115122774

image-20220602115229332

image-20220602115247982

image-20220602115324816

image-20220602115344134

image-20220602115357393

image-20220602115504528

image-20220602115624590

image-20220602115639581

image-20220602115803008

image-20220602115820070

image-20220602115842459

image-20220602115909755

image-20220602115922415



业务消息一致性处理

image-20220602120224405

image-20220602120250992

image-20220602120327885

image-20220602120345534

image-20220602120419985

image-20220602120437414

image-20220602172413312

image-20220602172437005

image-20220602172508093

image-20220602172537487

image-20220602172557483

image-20220602172610970

image-20220602172627053

image-20220602172725245

image-20220602173629206

image-20220602173643282

image-20220602173700973

image-20220602174244602

image-20220602174338651

image-20220602174353426

image-20220602174409001



总结

image-20220602174503650

image-20220602174523876



2. 运维实用篇



端口冲突

使用命令行杀死进程

image-20220506135712598



临时属性设置

  • 带属性数启动SpringBoot

​ java -jar springboot

–server.port=8080

  • 携带多个属性启动SpringBoot,属性使用空格分隔



1. 配置文件的等级

image-20220506141555822



2. 自定义配置文件

  1. 在idea中设置启动参数Program arguments : –spring.config.name=

    +配置文件名称

image-20220506143715509

  1. 在idea中设置Program arguments : –spring.config.location=

    classpath:/ebank.yml
  2. Program arguments 中可以写多个配置信息,用 “,” 隔开



多环境开发

1.同一个文件内用 —分隔开

# 应用环境
spring:
  profiles:
    active: pro
# 设置环境

# 生产环境
---
spring:
  config:
    activate:
      on-profile: pro
server:
  port: 80

---
# 开发环境
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 81

---
# 测试环境
spring:
  config:
    activate:
      on-profile: test
server:
  port: 82
  1. 多个环境文件

yml文件和properties文件操作方式相同

spring:
  profiles:
    active: dev
    inclue: 
    #现在用group
    group:
    	"dev": devDB,devMVC
    	"pro": proDB,proMVC

include: 中的文件先加载最后加载 dev

group: 先加载dev,最后加载devMVC

image-20220506204247706



日志

日志基础

  1. 日志作用

    • 日志(log)作用
    • 运营记录信息

      1. 记录日常运营重要信息(峰值流量、平均响应时长…)
      2. 记录应用报错信息(错误堆栈)
      3. 记录运维过程数据(扩容、宕机、报警…)

日志级别

image-20220506213159246

日志输出格式控制

  • 设置日志输出格式

image-20220506220100982

日志文件



3. 实用开发篇



热部署

重启

image-20220507105101413

ctrl+alt+shift+/

image-20220507105539247

默认不触发重启的目录列表

image-20220507181118944

关闭热部署功能

image-20220507181957688



配置高级



@ConfigurationProperties

image-20220507205113622

在使用了@EnableConfigurationProperties()后需要关闭@Component

image-20220507205511959



宽松绑定/松散绑定

image-20220507205728693

宽松绑定不支持注解@Value引用单个属性的方式

prefix 中用小写字符

image-20220507210030193



常用计量单位绑定
image-20220507211527928



数据校验

image-20220508000423797

image-20220508000437914

image-20220508000455763



测试



1. 加载测试专用属性

image-20220508004751647

image-20220508004808165



2. 加载测试专用配置

image-20220508114249709



3.Web环境模拟测试

image-20220508115901710

image-20220508121242753

image-20220508140433995

image-20220508140702865



Json

image-20220508140848490



4.数据层测试回滚

避免测试的数据影响数据库

image-20220508144016777



5.测试用例数据设定

随机测试数据

image-20220508145114130



4. 数据层解决方案



1.SQL

现有数据层解决方案技术选型

Druid + MyBatis-Plus + MySQL

  1. 数据源: DruidDataSource
  2. 持久化技术: MyBatis-Plus / MyBatis
  3. 数据库: MySQL




数据源:

image-20220508165702778

image-20220508165801390


应用场景:

image-20220508165848795




内置持久化解决方案—jdbcTemplate

@SpringBootTest
class Springboot15SqlApplicationTests{
    @Test
    void testJdbcTemplate(@Autowired JdbcTemplate jdbcTemplate){

        String sql = "select * from tbl_book";
        RowMapper<Book> rm = new RowMapper<Book>() {
            @Override
            public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
                Book temp = new Book();
                temp.setId(rs.getInt("id"));
                temp.setName(rs.getString("name"));
                temp.setType(rs.getString("type"));
                temp.setDescription(rs.getString("description"));
                return temp;
            }
        };
        List<Book> list = jdbcTemplate.query(sql, rm);
        System.out.println(list);
    }
}

需要导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

yml中的jdbcTemplate配置

image-20220508175801120



内嵌数据库

image-20220508180141981

H2数据库:线上运行时请务必关闭

image-20220508180205398

image-20220508191128289

image-20220508215037781



2.NoSQL

主流NoSQL

image-20220508215548331



redis (key-value存储结构)

image-20220508221225551

image-20220508221416911

启动Windows端redis时,一个小bug的解决方式

image-20220509005936521


redis整合流程

  1. 导入redis坐标

image-20220509013402104

  1. 配置redis

image-20220509013607352

  1. RedisTemplate的API

image-20220509013701546

  1. 实例

image-20220509013734970

RedisTemplate是以对象为基本操作单元

StringRedisTemplate以字符串为基本操作单元

image-20220509014930891

image-20220509014946097



连接Redis服务器的方式

  1. lettcus (springboot 默认)
  2. jedis

image-20220509015432372



Mongodb 结构化数据

image-20220509015851565

应用场景:

image-20220509020211217

Mongodb数据库下载

image-20220509020237121

阉割版操作系统可能出现的问题

image-20220510002723115



Mongodb可视化客户端 Robo 3T

image-20220510002827654

image-20220510005857350

image-20220510005919477

image-20220510010853267

image-20220510010903138

image-20220510010927171



ES(Elasticsearch)

Elasticsearch是一个分布式全文搜索引擎

image-20220511011413205

image-20220511011536500

image-20220511011609042

all是虚拟字段,内容由name和description内容组成.

{
    "mappings":{
        "properties":{
            "id":{
                "type":"keyword"
                // 用indext来设置是否用于查询
                // "index":true
            },
            "name":{
                // 设置参与配置的字段
                "type":"text",
                "analyzer":"ik_max_word",
                "copy_to":"all"
            },
            "type":{
                "type":"keyword"
            },
            "description":{
                // 设置参与配置的字段
                "type":"text",
                "analyzer":"ik_max_word",
                "copy_to":"all"
            },
            "all":{
                "type":"text",
                "analyzer":"ik_max_word"
            }
        }
    }
}

image-20220511014441473

image-20220511014532094

用put请求修改数据,需要把只改一个字段数据时,更新后的该数据只保留一个字段,其他字段数据删除

用update请求更改数据,只改一个字段数据时,其他字段数据不会改动

image-20220511014546144


es整合

:

image-20220511020326897

image-20220511020403853

image-20220511020432568

image-20220511021744657

其中使用了fastjson 依赖

image-20220511021804768

image-20220511021840737

image-20220511022435560

image-20220511022632839



5.整合第三方技术



1. 缓存

image-20220513124301768

image-20220515005613988

配置中添加 spring.cache.type=simple

image-20220515005705231

image-20220515005731420

image-20220516104210824

image-20220515010438894



1.Ehcache

image-20220515212407778

image-20220515212421368

image-20220515212457950

image-20220515212508599



2.Redis

image-20220515220946322

image-20220515221001140

image-20220515221020241



3.memcached

springboot没有整合memcacahed,所以需要自己定义版本号和提供属性

image-20220516020535726

image-20220516020747844

image-20220516020902827



整合流程

image-20220516102533884

image-20220516102555825

image-20220516102611325

image-20220516102649806

image-20220516102718844

image-20220516103923881



4.jetcache

image-20220516104510518



整合流程

image-20220516163114472

image-20220516163208788

image-20220516163231340

image-20220516163421519

image-20220516163436457



5.j2cache

j2cache-spring-boot2-starter 中包含有redis

image-20220517164215069

image-20220517164314488

image-20220517164455012

image-20220517164533235



2.任务

image-20220517173620196



1.Quartz

image-20220517173813725

image-20220517174850445

定义任务

image-20220517174911363

image-20220517174935014



2.spring task

image-20220517175651086

image-20220517175706326

image-20220517175721850



3.邮件

3个邮件相关协议

image-20220517215408333

image-20220519003547969

image-20220519204335712

image-20220520005052295



4.消息



1.消息概念与作用

image-20220521004437067

image-20220521004515153

image-20220521004705798

image-20220521004745672



2.JMS

一个规范,与JDBC相同

image-20220521004808662



3.AMQP

image-20220521004840012



4.MQTT

image-20220521005415107



5.Kafka简介

image-20220521005510912

image-20220521005533320

image-20220521010414280



ActiveMQ

下载地址

image-20220521184621571

启动服务

image-20220521185354158

整合流程

image-20220522223824852

image-20220522223843622

image-20220522223855891

image-20220522223950370

image-20220522224008919



RabbitMQ

image-20220522224100377

image-20220522232838344

image-20220523131623504


建议使用Chrome访问,edge打不开

image-20220523131644062

image-20220523190523832

direct exchange 整合流程

image-20220523224526387

image-20220523224605429

image-20220523224630887

image-20220523224704865

image-20220523224811567

两个listener轮询处理

image-20220523224827309

image-20220523224849166

topic exchange 整合流程

image-20220524004351245

image-20220524004415231

image-20220524004504983

image-20220524004528516



RocketMQ

image-20220524005325339

image-20220524213720702

启动RocketMQ

image-20220524225455629

image-20220524225525002

整合流程

image-20220525002017931

image-20220525002045057

image-20220525002100060

image-20220525002116798

image-20220525002135116



Kafka

image-20220525091331608

image-20220525091234776

image-20220525091257291

整合流程

image-20220525110629196

image-20220525110640591

image-20220525110702203

image-20220525110717945



6.监控

image-20220525190204258



1. 监控的意义

监控的意义

image-20220525204752492

监控的方式

image-20220525205101303



2.可视化监控平台

image-20220525221503474

image-20220525233214168

二者简化

image-20220525233235644

配置通过web端读取监控信息

adming服务端一定要做成一个web项目把功能开放出来,这样才能监控到这个应用

image-20220525233322320

image-20220525233612106



3.监控原理

image-20220526001144770

management:
  endpoint:
    health:
      show-details: always
    info:
      enable: false
  endpoints:
    web:
      exposure:
      # 端点功能暴露
        include: "*"
    #  端点功能开启与关闭
    enabled-by-default: true



4.自定义监控指标

image-20220526005401678

image-20220526005436304

image-20220526011918533

为Metrics端头添加自定义指标

image-20220526160158975

image-20220526161911523



4.原理篇



1.自动配置



1.bean的加载方式

加载方式一:

image-20220527211915366

加载方式二:

指定加载bean的位置, @component, @configuration

image-20220527215242689

image-20220527214059176

image-20220527215151387

image-20220527221614992

实现对bean加载到容器之前的批处理操作

image-20220528001542020

加载配置类并加载配置文件

image-20220528002410514

image-20220528004423255

image-20220528145221986

@import导入的bean,在容器中的名称为全路径名称

image-20220528153514771

image-20220529004549797

image-20220529011137087

image-20220529012219005

image-20220529020413818

image-20220529020431956



2.bean的加载控制

image-20220529021215393

image-20220529132652636

image-20220530004232913

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oYL1IHZL-1654171453493)(https://typora—pic.oss-cn-hangzhou.aliyuncs.com/img/image-20220530004320491.png)]

image-20220530004401696

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aI70pvpB-1654171453495)(https://typora—pic.oss-cn-hangzhou.aliyuncs.com/img/image-20220530004413927.png)]

image-20220530005316162

image-20220530005330449

image-20220530005736149

小结:

image-20220530005753655



3.bean依赖属性

image-20220530173017227

image-20220530173055299

image-20220530173125973

image-20220530173302885

image-20220530173338071



4.自动配置原理

image-20220530173826602

image-20220530230958338

image-20220530231229412

image-20220530231244572

image-20220530185629159



5.变更自动配置

image-20220530191116939

image-20220530191248590

image-20220530191318876

image-20220530191421019



2.自定义starter



1.案例:统计独立IP访问次数

image-20220530232333930

image-20220530232354619



2.自定义starter

判断key是否存在

image-20220531002112914

image-20220531002125185

image-20220531002143254

image-20220531002256723

image-20220531002315313

image-20220531160502548

image-20220531160749910

image-20220531160823725

image-20220531180540263

image-20220531180634018

image-20220531180854599

image-20220531181033158

image-20220531181053202

image-20220531181104407

image-20220531181152105

image-20220531231615724

image-20220531231627398

采用@Import注解 生成bean, 因为@configurationproperties生成的bean名称与EL表达式冲突

image-20220531231702523

image-20220531231824672

image-20220531231854810

image-20220601131336943

image-20220601131405684

image-20220601131422051



3.辅助功能开发

image-20220601132703359

image-20220601132719463

image-20220601132731967

image-20220601132743880



3.核心原理



1.SpringBoot启动流程

image-20220601135738127

image-20220601135748606

image-20220601153751814

image-20220601153807101



2.容器类型选择



3.监听器



版权声明:本文为weixin_45395760原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。