zoukankan      html  css  js  c++  java
  • SpringBoot入门

    新建工程:Spring Initializr
    SpringBoot就会帮我们建好SpringbootApplication启动类
    内容:

    package com.fitsoft.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringbootApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootApplication.class, args);
        }
    
    }
    

    这里举一个简单的例子来使用:
    在resources资源目录下新建application.yml配置文件(比application.properties好用,易维护)
    内容:

    spring:
      profiles:
        active: prd
    

    其中prd代表生产环境
    application-prd.yml内容:

    server:
      port: 80
    debug: false
    logging:
      level:
        root: info
      file: E:/demospringboot.log
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test
        data-username: root
        data-password: root
    mall:
      config:
        name: 优美商城
        description: 这是一家化妆品特卖网站
        hot-sales: 20
        show-advert: true
    

    dev代表测试环境
    application-dev.yml内容:

    debug: true
    logging:
      level:
        root: info
      file: E:/demospringboot.log
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test
        data-username: root
        data-password: root
    mall:
      config:
        name: 爱美商城
        description: 这是一家化妆品特卖网站
        hot-sales: 20
        show-advert: true
    

    这里我们使用的是生产环境举例,端口号为80
    新建MyController类

    package com.fitsoft.springboot.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;
    
    /**
     * @author Joker
     * @since 2019/9/14 0014 22:36
     */
    @Controller
    public class MyController {
    
        @Value("${mall.config.name}")
        private String name;
    
        @Value("${mall.config.description}")
        private String description;
    
        @Value("${mall.config.hot-sales}")
        private Integer hotSales;
    
        @Value("${mall.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);
        }
    }
    

    这里使用@Value注解来访问yml文件,并读取配置,运行一下:
    效果图:

    ![](https://i.loli.net/2019/09/15/oJsxgzR8CybXufP.png)

    真香...

  • 相关阅读:
    CAD开发中遇到的疑难问题整理与开发技巧
    使用jquery插件jquery.qrcode生成二维码
    小程序 跳转页面
    【已解决】Intel NUC10 拔插USB口/登录QQ/蓝牙连接等导致显示器黑屏
    element-ui表格el-table回显时默认全选数据
    设计模式
    react lib-flexible postcss-px2rem集成
    odoo 接口请求原理
    odoo 更改返回的json 格式
    git 合并两个仓库
  • 原文地址:https://www.cnblogs.com/zqm-sau/p/11521558.html
Copyright © 2011-2022 走看看