springboot复习(黑马)

springboot复习(黑马)_第1张图片

学习目标


基于SpringBoot框架的程序开发步骤

熟练使用SpringBoot配置信息修改服务器配置

基于SpringBoot的完成SSM整合项目开发

一、SpringBoot简介


1. 入门案例

问题导入

SpringMVC的HelloWord程序大家还记得吗?

  • SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

  • 原生开发SpringMVC程序过程

springboot复习(黑马)_第2张图片

1.1 入门案例开发步骤

环境准备

springboot复习(黑马)_第3张图片
springboot复习(黑马)_第4张图片
springboot复习(黑马)_第5张图片
springboot复习(黑马)_第6张图片
①:创建新模块,选择Spring初始化,并配置模块相关基础信息
springboot复习(黑马)_第7张图片
springboot复习(黑马)_第8张图片
②:选择当前模块需要使用的技术集
springboot复习(黑马)_第9张图片
springboot复习(黑马)_第10张图片
springboot复习(黑马)_第11张图片
springboot复习(黑马)_第12张图片
③:开发控制器类
@RestController
public class Controller01 {

    @RequestMapping("/sayHi")
    public String sayHi(){
        System.out.println("hi...");
        return "hi ... springboot...";
    }
}
springboot复习(黑马)_第13张图片

④:运行自动生成的Application类

springboot复习(黑马)_第14张图片
springboot复习(黑马)_第15张图片

访问页面

springboot复习(黑马)_第16张图片

重新启动

springboot复习(黑马)_第17张图片
springboot复习(黑马)_第18张图片

最简SpringBoot程序所包含的基础文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.1
         
    
    com.itheima
    demo1_helloworld
    0.0.1-SNAPSHOT
    demo1_helloworld
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


package com.itheima;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/*/*
    @SpringBootApplication:
        1. 表示这个类是一个springboot应用程序的入口类。
        2. 要想让程序启动,只需要在main方法里面写上这样的一句话:
              SpringApplication.run(当前类的字节码对象, args);
        3. 拓展:
            3.1 springboot项目启动的时候,默认会扫描启动类所在的位置,以及它后续的所有子包。
            3.2 查找到类里面打的注解 @Controller , @Service , @RequestMapping.
            3.3 springApplication.run 传递进去当前类的字节码对象,也是可以确定当前
                这个启动器它的包是哪个!
 */
@SpringBootApplication
public class Demo1HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo1HelloworldApplication.class, args);
    }

}

Spring程序与SpringBoot程序对比

springboot复习(黑马)_第19张图片

注意事项:

基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构

1.2 基于SpringBoot官网创建项目

springboot复习(黑马)_第20张图片

保存到桌面

springboot复习(黑马)_第21张图片
springboot复习(黑马)_第22张图片

解压完之后 用idea打开他

springboot复习(黑马)_第23张图片
springboot复习(黑马)_第24张图片
springboot复习(黑马)_第25张图片

因为用到了Mysql 所以得配置数据库

springboot复习(黑马)_第26张图片

已经启动

springboot复习(黑马)_第27张图片

1.3 SpringBoot项目快速启动

① 对SpringBoot项目打包(执行Maven构建指令package)

② 执行启动指令

springboot复习(黑马)_第28张图片

java -jar ava -jar demo1_helloworld-0.0.1-SNAPSHOT.jar    # 项目的名称根据实际情况修改

例子

springboot复习(黑马)_第29张图片
springboot复习(黑马)_第30张图片
springboot复习(黑马)_第31张图片
springboot复习(黑马)_第32张图片
springboot复习(黑马)_第33张图片
springboot复习(黑马)_第34张图片
springboot复习(黑马)_第35张图片
springboot复习(黑马)_第36张图片
springboot复习(黑马)_第37张图片
springboot复习(黑马)_第38张图片
springboot复习(黑马)_第39张图片

springboot复习(黑马)_第40张图片

注意事项:

jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件。

springboot复习(黑马)_第41张图片

 
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

如果没有这个依赖maven插件 打包就只有4k左右

springboot复习(黑马)_第42张图片

这个时候跟本运行不了

springboot复习(黑马)_第43张图片

2. SpringBoot概述

问题导入

学习了SpringBoot入门案例之后,感觉对比SpringMVC哪一个更加方便简洁?

  • SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

  • Spring程序缺点

  • 配置繁琐

  • 依赖设置繁琐

  • SpringBoot程序优点

  • 自动配置

  • 起步依赖(简化依赖配置)

  • 辅助功能(内置服务器,……)

springboot复习(黑马)_第44张图片
springboot复习(黑马)_第45张图片

28行到225行

springboot复习(黑马)_第46张图片

226行 到2737行

springboot复习(黑马)_第47张图片
springboot复习(黑马)_第48张图片

2.1 起步依赖

  • starter

  • SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的

  • 可以认为这个起步依赖相当于一个开关 我们主要用了这个东西 就相当用了他的全套功能

   
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
springboot复习(黑马)_第49张图片

parent

  • 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的

  • spring-boot-starter-parent(2.5.0)与 spring-boot-starter-parent(2.4.6)共计57处坐标版本不同



    4.0.0
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.1
         
    
    
    com.itheima
    demo1_helloworld
    0.0.1-SNAPSHOT
    demo1_helloworld
    Demo project for Spring Boot


    4.0.0
    
        org.springframework.boot
        spring-boot-dependencies
        2.5.0
    
    spring-boot-starter-parent
    pom    
    ...

实际开发

  • 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供

  • 如发生坐标错误,再指定version(要小心版本冲突)

如我们要用到Mysql

     
            mysql
            mysql-connector-java
        
springboot复习(黑马)_第50张图片
springboot复习(黑马)_第51张图片

当然也不是什么都有管理

比如druid就不被管理

 
            com.alibaba
            druid
            1.2.8
        

如果不写版本号的话 就会出现unknown 这个时候 我们就知道在springboot的父亲的父亲里面并没有管理这个依赖 这个时候 我们就能自己添加版本号

springboot复习(黑马)_第52张图片

自己添加版本号

springboot复习(黑马)_第53张图片

2.2 辅助功能

  • SpringBoot程序启动

package com.itheima;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/*/*
    @SpringBootApplication:
        1. 表示这个类是一个springboot应用程序的入口类。
        2. 要想让程序启动,只需要在main方法里面写上这样的一句话:
              SpringApplication.run(当前类的字节码对象, args);
        3. 拓展:
            3.1 springboot项目启动的时候,默认会扫描启动类所在的位置,以及它后续的所有子包。
            3.2 查找到类里面打的注解 @Controller , @Service , @RequestMapping.
            3.3 springApplication.run 传递进去当前类的字节码对象,也是可以确定当前
                这个启动器它的包是哪个!
 */
@SpringBootApplication
public class Demo1HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo1HelloworldApplication.class, args);
    }

}
  • SpringBoot在创建项目时,采用jar的打包方式

  • SpringBoot的引导类是项目的入口,运行main方法就可以启动项目

  • 使用maven依赖管理变更起步依赖项

  • Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty


        
            org.springframework.boot
            spring-boot-starter-web
            
            
        
        
        
            org.springframework.boot
            spring-boot-starter-jetty
        

        
            mysql
            mysql-connector-java
        

        
            com.alibaba
            druid
            1.2.8
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
springboot复习(黑马)_第54张图片

全部pom.xml

`

    4.0.0
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.1
         
    
    
    com.itheima
    demo1_helloworld
    0.0.1-SNAPSHOT
    demo1_helloworld
    Demo project for Spring Boot
    
    
        1.8
    
    
    
    
        
            org.springframework.boot
            spring-boot-starter-web
            
            
        

        
        
            org.springframework.boot
            spring-boot-starter-jetty
        






        
            mysql
            mysql-connector-java
        

        
            com.alibaba
            druid
            1.2.8
        



        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


二、基础配置

环境准备 demo2_properties

前面的创建操作和demo1类似 不在展示

springboot复习(黑马)_第55张图片
springboot复习(黑马)_第56张图片

springboot复习(黑马)_第57张图片
springboot复习(黑马)_第58张图片
springboot复习(黑马)_第59张图片
springboot复习(黑马)_第60张图片

1. 配置文件格式

问题导入

框架常见的配置文件有哪几种形式?

1.1 修改服务器端口

SpringBoot提供了多种属性配置方式

  • application.properties

server.port=8081
springboot复习(黑马)_第61张图片

重启

springboot复习(黑马)_第62张图片
springboot复习(黑马)_第63张图片

小备注:如果发现启动报错

springboot复习(黑马)_第64张图片

解决办法

查看进程 看看有没的被人家使用了

springboot复习(黑马)_第65张图片

发现被14452使用了

打开任务管理器

springboot复习(黑马)_第66张图片

找到14452

springboot复习(黑马)_第67张图片

springboot复习(黑马)_第68张图片

在重新启动就可以

application.yml

server:
  port: 8082
springboot复习(黑马)_第69张图片
springboot复习(黑马)_第70张图片

重新启动

springboot复习(黑马)_第71张图片
springboot复习(黑马)_第72张图片

application.yaml

server:
  port: 8083

发现不提示 解决办法 1.2 自动提示功能消失解决方案

springboot复习(黑马)_第73张图片

解决完小叶子问题之 后 就会有提示了

springboot复习(黑马)_第74张图片

重启

springboot复习(黑马)_第75张图片
springboot复习(黑马)_第76张图片

1.2 自动提示功能消失解决方案

操作步骤:

第一种:

springboot复习(黑马)_第77张图片
springboot复习(黑马)_第78张图片
springboot复习(黑马)_第79张图片
springboot复习(黑马)_第80张图片

如果发现上面的ok点不了 可能是没有application.properties 我们要先把这个给创建出来

springboot复习(黑马)_第81张图片

第二种

springboot复习(黑马)_第82张图片
springboot复习(黑马)_第83张图片
springboot复习(黑马)_第84张图片
springboot复习(黑马)_第85张图片

1.3 SpringBoot配置文件加载顺序(了解)

  • application.properties > application.yml > application.yaml

注意事项:

  1. SpringBoot核心配置文件名为application

  1. SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性

2. yaml

问题导入

什么是yaml,和properties有什么区别?

  • YAML(YAML Ain't Markup Language),一种数据序列化格式

  • 优点:

  • 容易阅读

  • 容易与脚本语言交互

  • 以数据为核心,重数据轻格式

  • YAML文件扩展名

  • .yml(主流)

  • .yaml

2.1 yaml语法规则

  • 大小写敏感

  • 属性层级关系使用多行描述,每行结尾使用冒号结束

  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)

  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)

  • #表示注释

  • 核心规则:数据前面要加空格与冒号隔开

2.2 yaml数组数据

  • 数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔

数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔

springboot复习(黑马)_第86张图片

2.3 yaml数据读取

  • 2.3.1 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名……}
package com.itheima.web;

/*
    在这个类里面,使用@Value来读取yml文件中的数据。
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller01 {
    @Value("${lesson}")
    private String lesson;

    //@Value("${第一层.第二层.第三层.属性名}")
    @Value("${server.port}")
    private String port;

    @Value("${enterprise.name}")
    private String name;

    @Value("${enterprise.subject[0]}")
    private String subject;

    @RequestMapping("/readYml")
    public String readYml(){

        System.out.println("lesson = " + lesson);
        System.out.println("port = " + port);
        System.out.println("name = " + name);
        System.out.println("subject = " + subject);

        return "hi springboot...";
    }
}
lesson: SpringBoot

server:
  port: 8082

enterprise:
  name: 传智播客
  age: 16
  tel: 4006184000
  subject:
    - Java
    - 前端
    - 大数据
springboot复习(黑马)_第87张图片
springboot复习(黑马)_第88张图片
springboot复习(黑马)_第89张图片
springboot复习(黑马)_第90张图片

注:如果报错的话 修改一下编码

springboot复习(黑马)_第91张图片
springboot复习(黑马)_第92张图片
springboot复习(黑马)_第93张图片
springboot复习(黑马)_第94张图片

如果还报错的话 就clean一下

springboot复习(黑马)_第95张图片

如果还是报错的话

springboot复习(黑马)_第96张图片

如果还是报错 我们就把前面没有用到的application.properties和application.yaml的端口号给注释了或把这二个文件删除

端口号给注释了

springboot复习(黑马)_第97张图片
springboot复习(黑马)_第98张图片

或删除

springboot复习(黑马)_第99张图片
  • 2.3.2 封装全部数据到Environment对象
package com.itheima.web;

/*
    在这个类里面,使用Environment来读取yml文件中的数据。
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller02 {

    @Autowired
    private Environment env;

    @RequestMapping("/readYml02")
    public String readYml(){

        System.out.println("lesson = " + env.getProperty("lesson"));
        System.out.println("port = " + env.getProperty("server.port"));
        System.out.println("name = " + env.getProperty("enterprise.name"));
        System.out.println("subject = " + env.getProperty("enterprise.subject[0]"));

        return "hi springboot.0222..";
    }
}
lesson: SpringBoot

server:
  port: 8082

enterprise:
  name: 传智播客
  age: 16
  tel: 4006184000
  subject:
    - Java
    - 前端
    - 大数据
springboot复习(黑马)_第100张图片
springboot复习(黑马)_第101张图片
springboot复习(黑马)_第102张图片
  • 2.3.3 自定义对象封装指定数据【常用】==

因为封装 要得到属性 用到@Data注解 所以要加入以下依赖

      
            org.projectlombok
            lombok
        
springboot复习(黑马)_第103张图片

package com.itheima.bean;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private int age ;
    private String tel;
    private String [] subject;
}
springboot复习(黑马)_第104张图片
package com.itheima.web;

/*
    在这个类里面,使用Environment来读取yml文件中的数据。
 */

import com.itheima.bean.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
public class Controller03 {

    @Autowired
    private Enterprise enterprise;

    @RequestMapping("/readYml03")
    public String readYml(){

        System.out.println("enterprise = " + enterprise);


        return "hi springboot.0333..";
    }
}
springboot复习(黑马)_第105张图片
lesson: SpringBoot

server:
  port: 8082

enterprise:
  name: 传智播客
  age: 16
  tel: 4006184000
  subject:
    - Java
    - 前端
    - 大数据
springboot复习(黑马)_第106张图片
springboot复习(黑马)_第107张图片
springboot复习(黑马)_第108张图片
springboot复习(黑马)_第109张图片
springboot复习(黑马)_第110张图片

自定义对象封装数据警告解决方案

问题:

解决

加入依赖

       
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
springboot复习(黑马)_第111张图片

3. 多环境开发配置

环境准备

springboot复习(黑马)_第112张图片
springboot复习(黑马)_第113张图片
springboot复习(黑马)_第114张图片

问题导入

在实际开发中,项目的开发环境、测试环境、生产环境的配置信息是否会一致?如何快速切换?

3.1 多环境启动配置

  • yaml文件多环境启动

springboot复习(黑马)_第115张图片
3.1.1 在YML文件多环境启动
# 激活具体的环境
spring:
  profiles:
    active: dev

---
# 定义开发环境
server:
  port: 80

spring:
  config:
    activate:
      on-profile: dev
---

# 定义生产环境
server:
  port: 81

spring:
  config:
    activate:
      on-profile: prod

---
# 定义测试环境
server:
  port: 82
spring:
  config:
    activate:
      on-profile: test
springboot复习(黑马)_第116张图片

springboot复习(黑马)_第117张图片
springboot复习(黑马)_第118张图片
springboot复习(黑马)_第119张图片
springboot复习(黑马)_第120张图片
3.1.2 properties文件多环境启动

application.properties

# 使用properties的文件格式来定义多环境
# 1. 一套环境就一个properties文件,命名格式: application-环境的id.properries
# 2. 在application.properties 里面激活具体的环境。
spring.profiles.active=dev
springboot复习(黑马)_第121张图片

application-dev.properties

server.port=8081
springboot复习(黑马)_第122张图片

application-prod.properties

server.port=8082
springboot复习(黑马)_第123张图片

application-test.properties

server.port=8083
springboot复习(黑马)_第124张图片

启动

springboot复习(黑马)_第125张图片

3.2 多环境启动命令格式

把application.properties放到properties-bak

springboot复习(黑马)_第126张图片
  • 带参数启动SpringBoot

springboot复习(黑马)_第127张图片

打包

springboot复习(黑马)_第128张图片
springboot复习(黑马)_第129张图片
springboot复习(黑马)_第130张图片
springboot复习(黑马)_第131张图片
springboot复习(黑马)_第132张图片

重新cmd 修改端口

springboot复习(黑马)_第133张图片

参数加载优先顺序

  • 参看文档:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

springboot复习(黑马)_第134张图片

3.3 多环境开发控制

Maven与SpringBoot多环境兼容(步骤)
springboot复习(黑马)_第135张图片

环境准备

springboot复习(黑马)_第136张图片
springboot复习(黑马)_第137张图片
springboot复习(黑马)_第138张图片
①:Maven中设置多环境属性

        
        
            dev_mvn
            
                dev
            

            
                true
            
        

        
        
            prod_mvn
            
                prod
            
        

        
        
            test_mvn
            
                test
            
        
    
springboot复习(黑马)_第139张图片
②:SpringBoot中引用Maven属性
# 激活具体的环境
spring:
  profiles:
    active: ${a} # 这里的值是动态变化的,由maven来控制它的变化

---
# 声明开发环境
server:
  port: 80
spring:
  config:
    activate:
      on-profile: dev
---
# 声明生产环境
server:
  port: 81
spring:
  config:
    activate:
      on-profile: prod
---
# 声明测试环境
server:
  port: 82
spring:
  config:
    activate:
      on-profile: test
springboot复习(黑马)_第140张图片
springboot复习(黑马)_第141张图片

③:执行Maven编译指令

  • Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中

springboot复习(黑马)_第142张图片

解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符

④:对资源文件开启对默认占位符的解析
    
            
                maven-resources-plugin
                
                    utf-8
                    true
                
            
springboot复习(黑马)_第143张图片

重新编译

springboot复习(黑马)_第144张图片

Maven编译加载到属性,编译顺利通过

springboot复习(黑马)_第145张图片

如果想要换其他的环境

springboot复习(黑马)_第146张图片

也可以用命令的方式

springboot复习(黑马)_第147张图片
springboot复习(黑马)_第148张图片

打包

springboot复习(黑马)_第149张图片
springboot复习(黑马)_第150张图片
springboot复习(黑马)_第151张图片

4. 配置文件分类

问题导入

SpringBoot的配置文件可以放在项目的哪些地方?

springboot复习(黑马)_第152张图片
java –jar springboot.jar --spring.profiles.active=test --server.port=85 --server.servlet.context-path=/heima --server.tomcat.connection-timeout=-1 ... ...
  • SpringBoot中4级配置文件

1级: file :config/application.yml 【最高】

2级: file :application.yml

3级:classpath:config/application.yml

4级:classpath:application.yml 【最低】

  • 作用:

1级与2级留做系统打包后设置通用属性

3级与4级用于系统开发阶段设置通用属性

springboot复习(黑马)_第153张图片
  • 演示3级和4级

springboot复习(黑马)_第154张图片

优先使用8888端口 因为config/application.yml的优先级高于application.yml 【最低】

springboot复习(黑马)_第155张图片
springboot复习(黑马)_第156张图片
springboot复习(黑马)_第157张图片
springboot复习(黑马)_第158张图片
springboot复习(黑马)_第159张图片
springboot复习(黑马)_第160张图片
springboot复习(黑马)_第161张图片
springboot复习(黑马)_第162张图片
springboot复习(黑马)_第163张图片
springboot复习(黑马)_第164张图片
  • 演示第二级

springboot复习(黑马)_第165张图片
springboot复习(黑马)_第166张图片
springboot复习(黑马)_第167张图片
springboot复习(黑马)_第168张图片
  • 演示第一级

springboot复习(黑马)_第169张图片
springboot复习(黑马)_第170张图片
springboot复习(黑马)_第171张图片
springboot复习(黑马)_第172张图片

Ctrl+C 结束前面的

重新启动

springboot复习(黑马)_第173张图片

三、整合第三方技术


1. 整合JUnit

环境准备

springboot复习(黑马)_第174张图片
springboot复习(黑马)_第175张图片
springboot复习(黑马)_第176张图片
springboot复习(黑马)_第177张图片

有web


            org.springframework.boot
            spring-boot-starter-web
        

无web


            org.springframework.boot
            spring-boot-starter
        

不导入web也是可以自动扫描包 也是可以自动创建工程

springboot复习(黑马)_第178张图片

问题导入

回忆一下Spring整合JUnit的步骤?

1.1 Spring整合JUnit(复习)

springboot复习(黑马)_第179张图片

1.2 SpringBoot整合JUnit

先跑一下自己写的测试类

@SpringBootTest
public class MyTest {


    @Test
    public void test01(){
        System.out.println("test01....");

    }
}

springboot复习(黑马)_第180张图片
springboot复习(黑马)_第181张图片

MyTest里面测试service的方法

测试类

package com.itheima;
import com.itheima.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class MyTest {
    @Autowired
    private UserService us;

    @Test
    public void test01(){

        System.out.println("test01....");
        us.add();

    }
}
springboot复习(黑马)_第182张图片

接口

package com.itheima.service;

public interface UserService {
    void add();
}
springboot复习(黑马)_第183张图片

实现类

package com.itheima.service.impl;

import com.itheima.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("调用了UserServiceImpl的add方法~!!~");
    }
}
springboot复习(黑马)_第184张图片

@SpringBootTest解释

springboot复习(黑马)_第185张图片

@SpringBootTest(classes = Demo5JunitApplication.class)

什么时候加classes

1. 如果测试类所在的包 是 位于 启动类所在的包或者它的子包里面, 或者大家的包名一致,那么可以省略启动类不写!

启动类: com.itheima

测试: com.itheima

springboot复习(黑马)_第186张图片

2. 如果测试类所在的包,并不是位于启动类所在包的下面,那么一定要指定启动类是谁, 否则有可能出错!

比如我们把test包放到com下面 不写classes就会出错

springboot复习(黑马)_第187张图片
springboot复习(黑马)_第188张图片

如果写上classes

springboot复习(黑马)_第189张图片
springboot复习(黑马)_第190张图片

2. 基于SpringBoot实现SSM整合

问题导入

回忆一下Spring整合MyBatis的核心思想?

2.1 Spring整合MyBatis(复习)

2.1 Spring整合MyBatis(复习)

  • SpringConfig

  • 导入JdbcConfig

  • 导入MyBatisConfig

`@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
public class SpringConfig {

}

JDBCConfig

  • 定义数据源(加载properties配置项:driver、url、username、password)

#jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=itheima
public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource getDataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

MyBatisConfig

  • 定义SqlSessionFactoryBean

  • 定义映射配置

@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) {
    SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
    ssfb.setTypeAliasesPackage("com.itheima.domain");
    ssfb.setDataSource(dataSource);
    return ssfb;
}
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer() {
    MapperScannerConfigurer msc = new MapperScannerConfigurer();
    msc.setBasePackage("com.itheima.dao");
    return msc;
}

2.2 SpringBoot整合MyBatis

  • SpringBoot整合Spring(不存在)

  • SpringBoot整合SpringMVC(不存在)

  • SpringBoot整合MyBatis(主要)

①:创建新模块,选择Spring初始化,并配置模块相关基础信息
springboot复习(黑马)_第191张图片
②:选择当前模块需要使用的技术集(MyBatis、MySQL)
springboot复习(黑马)_第192张图片
springboot复习(黑马)_第193张图片
springboot复习(黑马)_第194张图片
③:设置数据源参数
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot_test
    username: root
    password: 123456
springboot复习(黑马)_第195张图片

注意事项:

  1. SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题

jdbc:mysql://localhost:3306/springboot_test?serverTimezone=UTC

加入druid连接池 和lombok依赖

   
            com.alibaba
            druid
            1.2.8
        
 
            org.projectlombok
            lombok
        
springboot复习(黑马)_第196张图片
④:定义数据层接口与映射配置

实体类bean

package com.itheima.bean;

import lombok.Data;

@Data
public class Book {
    private int id;
    private String type;
    private String name;
    private String description;
}
springboot复习(黑马)_第197张图片
⑤:测试类中注入dao接口,测试功能
package com.itheima.dao;

import com.itheima.bean.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/*
    要想创建出来dao的代理对象,那么需要告诉springboot,这个dao在哪里?
        1. 第一种做法: 就是直接在dao的接口上打上注解 @Mapper

        2. 第二种做法: 在启动类上打上注解: @MapperScan("com.itheima.dao")
 */
@Mapper
public interface BookDao {

    @Select("select * from tbl_book")
    List findAll();
}
springboot复习(黑马)_第198张图片

package com.itheima;

import com.itheima.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Demo6MybatisApplicationTests {

    @Autowired
    private BookDao dao;

    @Test
    void testFindAll() {
        System.out.println("dao.findAll() = " + dao.findAll());
    }
}

测试结果:

springboot复习(黑马)_第199张图片

如果不用druid连接池 就用光连接池

spring:
  datasource:
    #type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot_test
    username: root
    password: 123456
springboot复习(黑马)_第200张图片
/*
    要想创建出来dao的代理对象,那么需要告诉springboot,这个dao在哪里?
        1. 第一种做法: 就是直接在dao的接口上打上注解 @Mapper

        2. 第二种做法: 在启动类上打上注解: @MapperScan("com.itheima.dao")
 */

第二种做法

package com.itheima;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.itheima.dao")
@SpringBootApplication
public class Demo6MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo6MybatisApplication.class, args);
    }

}
springboot复习(黑马)_第201张图片

如果二种都不打 会报以下错误

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.itheima.dao.BookDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
springboot复习(黑马)_第202张图片

2.3 案例-SpringBoot实现ssm整合

① 环境准备
springboot复习(黑马)_第203张图片
springboot复习(黑马)_第204张图片
springboot复习(黑马)_第205张图片
springboot复习(黑马)_第206张图片
springboot复习(黑马)_第207张图片
② 导入静态页面
springboot复习(黑马)_第208张图片
③ yml
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot_test
    username: root
    password: 123456
springboot复习(黑马)_第209张图片

启动

springboot复习(黑马)_第210张图片

访问

http://localhost:8080/pages/books.html
springboot复习(黑马)_第211张图片
④ 前端
         methods: {
                //列表
                getAll() {

                    axios.get("/books").then(resp=>{
                        console.log(resp);
                        //this.dataList = resp.data.data;

                    });
springboot复习(黑马)_第212张图片
⑤ bean实体类
`package com.itheima.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/*
    这个类是用来包装返回给前端的数据
 */

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Result {
    private int code; //代表这次请求的代号: 表示成功或者表示失败
    private String msg; //代表这次请求的代号的一个简短说明: 添加品牌成功 或者  添加品牌失败
    private Object data; //代表这次请求要返回给客户端的数据,一般是针对查询操作。
}
springboot复习(黑马)_第213张图片
⑥ dao接口
package com.itheima.dao;

import com.itheima.bean.Book;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BookDao {

    @Select("select * from tbl_book")
    List  findAll();
}
springboot复习(黑马)_第214张图片
⑦ service接口
`package com.itheima.service;

import com.itheima.bean.Book;

import java.util.List;

public interface BookService {
    List findAll();
}
springboot复习(黑马)_第215张图片
⑧ BookServiceImpl实现类
`package com.itheima.service.impl;

import com.itheima.bean.Book;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao dao;

    @Override
    public List findAll() {
        return dao.findAll();
    }
}
springboot复习(黑马)_第216张图片
⑨ web 【Controller】
`package com.itheima.web;

import com.itheima.bean.Book;
import com.itheima.bean.Result;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/books")
@RestController
public class BookController {

    @Autowired
    private BookService bs;

    @GetMapping
    public Result findAll(){
        List list = bs.findAll();
        return new Result(1, "查询成功" , list);
    }
}
springboot复习(黑马)_第217张图片

启动类打上@MapperScan("com.itheima.dao")

package com.itheima;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.itheima.dao")
@SpringBootApplication
public class Demo7SsmApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo7SsmApplication.class, args);
    }

}
springboot复习(黑马)_第218张图片

启动

springboot复习(黑马)_第219张图片
springboot复习(黑马)_第220张图片
⑩ 前端
 methods: {
                //列表
                getAll() {

                    axios.get("/books").then(resp=>{
                        console.log(resp);
                        this.dataList = resp.data.data;

                    });

重启

springboot复习(黑马)_第221张图片

你可能感兴趣的:(spring,boot,java,spring)