Spring Boot 速学

一. Spring Boot入门

1. Spring Boot 的概述

        Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程,Spring boot有着自动配置、起步依赖、内置服务器等优点。更多可以参考官方文档

 

2. 快速入门

        2.1 按步骤创建Spring boot 项目

         2.2 选择当前模块需要使用的技术集

         2.3 创建启动类

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
              
@SpringBootApplication
@RestController
public class DemoApplication {
                
                  
public static void main(String[] args) {
     SpringApplication.run(DemoApplication.class, args);
 }
                  
@GetMapping("/hello")
  public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
      }
                
 }
            

        2.4 运行应用程序

 二. 技术整合

1. Spring程序与SpringBoot程序对比

        基于idea开发Boot程序需要确保联网且能够加载到程序框架结构,Boot提供快速和简化的操作,Boot提供了 Spring 运行的默认配置,Boot为通用 Spring项目提供很多非功能性特性。

 2. 基础配置

        2.1 修改服务器端口的配置文件格式

# application.properties
server.port=80

# application.yml
server:
  port: 81

# application.yaml
server:
  port: 82

        2.2 配置文件加载顺序

        SpringBoot核心配置文件名为application,SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性。

        application.properties ---> application.yml ---> application.yaml

        2.3 多环境开发配置

# properties文件多环境启动

#主启动配置文件 application.properties
spring.profiles.active=pro

#环境分类配置文件 application-pro.properties
server.port=80

#环境分类配置文件 application-dev.properties
server.port=81

#环境分类配置文件application-test.properties
server.port=82

        2.4 多环境启动命令格式

# 带参数启动SpringBoot
java –jar springboot.jar --spring.profiles.active=test
java –jar springboot.jar --server.port=88
java –jar springboot.jar --server.port=88 --spring.profiles.active=test

           2.5 SpringBoot中4级配置文件

                其作用为1级与2级留做系统打包后设置通用属性,3级与4级用于系统开发阶段设置通用属性。

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

                2级: file :application.yml

                3级:classpath:config/application.yml

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

3. Spring Boot整合第三方技术

        3.1 整合JUnit

                【第一步】添加整合junit起步依赖(可以直接勾选)

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

                【第二步】编写测试类,默认自动生成了一个

@SpringBootTest
class Springboot07JunitApplicationTests {
    @Autowired
    private BookService bookService;

    @Test
    public void testSave() {
        bookService.save();
    }
}

        3.2 SpringBoot整合MyBatis

                【第一步】创建新模块,选择Spring初始化,并配置模块相关基础信息

                【第二步】选择当前模块需要使用的技术集(MyBatis、MySQL)

                【第三步】设置数据源参数

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_test?serverTimezone=UTC
    username: root
    password: 1234
    type: com.alibaba.druid.pool.DruidDataSource

                【第四步】定义数据层接口与映射配置

@Mapper
public interface UserDao {
    @Select("select * from tbl_book where id=#{id}")
    Book getById(Integer id);
}

                【第五步】测试类中注入dao接口,测试功能

@SpringBootTest
class Springboot08MybatisApplicationTests {
    @Autowired
    private BookDao bookDao;

    @Test
    public void testGetById() {
        Book book = bookDao.getById(1);
        System.out.println(book);
    }
}

        3.3 SpringBoot实现ssm整合

                【第一步】创建SpringBoot工程,添加druid依赖

<!--添加druid连接池依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>

                【第二步】删除config包中的所有配置,在BookDao接口上加@Mapper注解

// 在BookDao接口上加@Mapper注解,让SpringBoot给接口创建代理对象
@Mapper
public interface BookDao {
    //...
}

                【第三步】将application.properties修改成application.yml,配置端口号和连接参数

server:
  port: 80
# 配置数据库连接参数
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_test
    username: root
    password: 1234
    type: com.alibaba.druid.pool.DruidDataSource

                【第四步】修改BookServiceTest配置类,进行配置

// 修改单元测试类,添加@SpringBootTest主键,修复@Test注解导包
@SpringBootTest
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Test
    public void testGetById(){
        Book book = bookService.getById(2); //传递参数1会抛出异常
        System.out.println(book);
    }
    @Test
    public void testGetAll(){
        List<Book> all = bookService.getAll();
        System.out.println(all);
    }
}

                【第五步】在static目录中提供index.html页面,跳转到"pages/books.html"

<script>
    location.href="pages/books.html"
</script>

 4. springboot生命周期

        初始化环境变量 --->初始化环境变量完成 --->应用启动 --->应用已启动完成 --->应用刷新 ---> 应用停止 --->应用关闭

springboot 生命周期_普通网友的博客-CSDN博客_springboot生命周期

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10JQK炸

如果对您有所帮助,请给点鼓励吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值