首页 > 编程语言 > 详细

SpringBoot入门

时间:2020-06-17 12:04:53      阅读:51      评论:0      收藏:0      [点我收藏+]

SpringBoot概述

什么是SpringBoot

技术分享图片

传统SSM应用开发流程

技术分享图片

SpringBoot应用开发流程

技术分享图片

核心特性

技术分享图片

SpringBoot应用开发

项目准备

技术分享图片

技术分享图片

使用Maven构建SpringBoot项目

  • 引入依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • 创建目录结构

技术分享图片

  • 设置Controller
@Controller
public class MyController {

    @RequestMapping("/out")
    @ResponseBody
    public String out() {
        return "success";
    }
}
  • 设置Application,启动SpringBoot应用
//说明这是一个SpringBoot应用的入口类
@SpringBootApplication
public class MySpringBootApplication {
    public static void main(String[] args) {
        //启动SpringBoot应用
        SpringApplication.run(MySpringBootApplication.class);
    }
}

使用SpringInitializr创建SpringBoot项目

技术分享图片

技术分享图片

  • 设置Controller
@Controller
public class MyController {

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

SpringBoot配置详情

SpringBoot启动流程与常用配置

技术分享图片

技术分享图片

技术分享图片

SpringBoot配置文件

技术分享图片

技术分享图片

  • 对比
server.port=80
# debug->info->warn->error->fatal
logging.level.root=info
logging.file.name=e:/myspringboot.log
debug=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123
server:
  port: 80

logging:
  level:
    root: info
  file:
    name: e:/myspringboot.log

debug: true

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456

SpringBoot自定义配置项

技术分享图片

mall:
  config:
    name: 爱美商城
    description: 这是一家化妆品特卖网
    host-sales: 20
    show-advert: true
@Controller
public class MyController {

    @Value("${mall.config.name}")
    private String name;
    @Value("${mall.config.description}")
    private String description;
    @Value("${mall.config.host-sales}")
    private Integer hotSales;
    @Value("${mall.config.show-advert}")
    private Boolean showAdvert;

    @RequestMapping("/info")
    @ResponseBody
    public String info() {
        return String.format("name:%s,description:%s,hot-sales=%s,show-advert:%s",
                name,description,hotSales,showAdvert);
    }
}

环境配置文件

技术分享图片

spring:
  profiles:
    active: prd
server:
  port: 8080

logging:
  level:
    root: info
  file:
    name: e:/myspringboot.log

debug: true

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456

mall:
  config:
    name: 爱美商城
    description: 这是一家化妆品特卖网
    host-sales: 20
    show-advert: true
server:
  port: 80

logging:
  level:
    root: info
  file:
    name: /local/user/app-prd.log

debug: false

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://155.32.35.88:3306/test
    username: root
    password: adfas23!

mall:
  config:
    name: 优美商城
    description: 这是一家化妆品特卖网
    host-sales: 20
    show-advert: true

打包与运行

  • 点击package生成jar包
  • 运行时默认优先读取同级目录下的配置文件

SpringBoot入门

原文:https://www.cnblogs.com/jessekkk/p/13151460.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!