Spring-boot——新手入门详解

包结构

在这里插入图片描述

使用Maven构建SpringBoot项目:

按照包结构创建包和文件:

在这里插入图片描述

引入jar包
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.imooc</groupId>
    <artifactId>myspringboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--引入springboot最基础组件-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
    </parent>

    <!--引入springboot依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!--定义springboot打包方式-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
创建控制类
package com.aaa.myspringboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    @RequestMapping("/out")
    @ResponseBody
    public String out() {
        return "success";
    }
}
创建Spring Boot入口类

Spring Boot入口类:

  • 入口类命名通常以*Application结尾
  • 入口类增加@SpringBootApplication注解
  • 利用SpringApplication.run()方法启动应用
package com.aaa.myspringboot;

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

//说明这是一个springboot应用的入口类
@SpringBootApplication
public class MySpringBootApplication {
    public static void main(String[] args) {
        //启动SpringBoot应用
        SpringApplication.run(MySpringBootApplication.class);
    
验证
  • 运行MySpringBootApplication。并访问localhost:8080/out

Spring Initializr构建Spring Boot应用

在这里插入图片描述
2. 在这里插入图片描述
3. 在这里插入图片描述

启动流程

在这里插入图片描述

@Repository 	对应DAO类,用于数据处理
@Service		对应业务逻辑类
@Controller 	对应MVC的控制器类
@Component	 	对于某些不太好区分的类

Spring Boot常用配置

在这里插入图片描述

配置application.properties

server.port=80
server.servlet.context-path=/工程名

若不加server.servlet.context-path=/工程名。访问localhost/out。无需添加端口号

配置日志

server.port=80
server.servlet.context-path=/myspringboot

#指定日志保存路径
logging.file=D:/myspringboot.log

#debug-->info-->warn-->error-->fatal
#程序的调试日志-->程序一般性的输出信息-->警告信息,并不会引起程序的失败-->程序异常-->灾难性(服务器宕机)
logging.level.root=info

运行后会在D盘生成一个myspringboot.log日志文件

连接数据库


server.port=80
server.servlet.context-path=/myspringboot

#指定日志保存路径
logging.file=D:/myspringboot.log

#debug-->info-->warn-->error-->fatal
#程序的调试日志-->程序一般性的输出信息-->警告信息,并不会引起程序的失败-->程序异常-->灾难性(服务器宕机)
logging.level.root=info
debug=true

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/database
database
spring.datasource.username=aaa
spring.datasource.password=123

Spring Boot支持两种配置文件

  1. 属性文件:application.properties
  2. YAML格式: application.yml

YAML的语法

  • YAML 是一种简洁的非标记语言。YAML以数据
    为中心,使用空白,缩进,分行组织数据,从而使 得表示更加简洁易读。
    YAML语法格式
  • 标准格式:key:(空格)value
  • 使用空格代表层级关系,以“:”结束

配置application.yml

  • 创建src/resources/application.yml

server:
  port: 80

logging:
  level:
    root: info
  file:
    name: "D:/myspringboot.log"

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost.com:3306/database
    username: aaa
    password: 123

Spring Boot自定义配置项

Spring Boot允许我们自定义应用配置项,在程序运
行时允许动态加载,这为程序提供了良好的可维护性。
在实际项目开发中,我们通常将项目的自定义信息放
在配置文件中。

  • 在application.yml配置文件中自定义。eg:
mail:
  config:
    name: 冠兴
    description: 商城
    hot-sales: 10
    show-advert: true
  • 控制类:
package com.aaa.myspringboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    @Value("${mail.config.name}")
    private String name;

    @Value("${mail.config.description}")
    private String description;

    @Value("${mail.config.hot-sales}")
    private Integer hotSales;

    @Value("${mail.config.show-advert}")
    private Boolean showAdvert;

    @RequestMapping("/out")
    @ResponseBody
    public String out() {
        return "success";
    }

    @RequestMapping("/info")
    @ResponseBody
    public String info() {
        return String.format("name:%s,description:%s,hot-sales:%s,show-advert:%s,", name, description, hotSales, showAdvert);
    }
}
  • 验证:localhost:8080/info
name:冠兴,description:商城,hot-sales:10,show-advert:true,

环境配置及切换

  • Spring Boot可针对不同的环境提供不同的Profile文件。
  • Profile文件的默认命名格式为application-{env}.yml
  • 使用spring.profiles.active选项来指定不同的profile
  • eg:
    创建如下三个文件
    在这里插入图片描述
  • dev(开发环境)
debug: true
logging:
  level:
    root: info
  file:
    name: "D:/myspringboot.log"

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/database
    username: aaa
    password: 123

mail:
  config:
    name: 冠兴(开发环境)
    description: 商城
    hot-sales: 10
    show-advert: true
  • prd(生产环境)
debug: false
logging:
  level:
    root: info
  file:
    name: "/local/user/app-prd.log"

server:
  port: 80

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.12.13.45:3306/database
    username: aaa
    password: 123

mail:
  config:
    name: 冠兴(生产环境)
    description: 商城
    hot-sales: 100
    show-advert: true
  • yml(切换环境)
spring:
  profiles:
    active: prd

这个时候访问localhost:8080/info

name:冠兴(生产环境),description:商城,hot-sales:10,show-advert:true,

打包与运行

  1. 利用Maven的package命令,生成可独立运行的Jar包。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  2. 利用 java -jar xxx.jar 命令启动Spring Boot应用。
    在这里插入图片描述
    在这里插入图片描述

  3. Jar包可自动加载同目录的application配置文件。

  • 通过修改application切换环境
    在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
课程的实战源码是我在 GitHub 上开源项目 spring-boot-projects 中的其中一个项目代码,目前已有 2300 多个 star,项目截图如下: 由于项目比较受大家欢迎,因此心中就出现了将它做成教学视频的想法,也就是你现在了解的这个课程《SpringBoot入门及前后端分离项目开发》,本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 项目实践开发。Spring Boot 介绍、前后端分离、API 规范等内容旨在让读者更加熟悉 SpringBoot 及企业开发中需要注意的事项并具有使用 SpringBoot 技术进行基本功能开发的能力;这最后的项目实战为课程的主要部分,我会带着大家实际的开发一个前后端分离的 Spring Boot 实践项目,让大家实际操作并从无到有开发一个线上项目,并学习到一定的开发经验以及其中的开发技巧,旨在让读者具有将 Spring Boot 真正应用于项目开发的能力; 以下为实践项目的页面和功能展示,分别为:登录页面 列表页面(分页功能) 图片上传功能 富文本编辑器整合使用 实践项目的主要功能和页面就是这些,通过项目展示大家也能够感受到,在实际应用开发中的高频次功能都已经实现,稍加修改就可以运用到企业开发中,整个项目的开发模式为前后端分离的模式,即 Spring Boot 提供后端接口,前端页面通过 Ajax 异步调用接口的方式与后端服务器进行交互并将数据填充至页面中,这也是目前企业开发中比较重用的开发模式,希望大家能够了解并且能够实际的上手开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

〆`杨陆原じ₯㎕

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值