• springboot入门


    Springboot入门

    Spring Boot它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具

    同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的Spring Boot应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑

     1:敏捷式开发

      注2:spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,

            就像maven整合了所有的jar包,spring boot整合了所有的框架

    Idea配置SpringBoot项目

     

     

    java源文件夹中的Springboot01Application.java是整个项目的启动类

    static:存放的是静态资源的文件

    templetes:存放的项目所需的页面

    application.properties里面存放的是项目的全局配置信息

    启动Springboot

    浏览器访问

    测试springboot(案例)

     1 package com.javaxl.springboot01.controller;
     2 
     3 import com.javaxl.springboot01.configurationProperties.MysqlEntity;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.beans.factory.annotation.Value;
     6 import org.springframework.web.bind.annotation.PathVariable;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RestController;
     9 
    10 import java.util.HashMap;
    11 import java.util.Map;
    12 
    13 /**
    14  * @author XuFanQi
    15  * @site
    16  * @company
    17  * @create 2019-11-24 15:50
    18  * RestController = responsebady + conterllor
    19  */
    20 @RestController
    21 public class HelloController {
    22 
    23 
    24     @RequestMapping("/hello01")
    25     public String hello01(){
    26         return "Hello springboot ! ! !";
    27     }
    28 
    29     @RequestMapping("/say1")
    30     public String say1(String name){
    31         return name + "say hello springboot 你大爷";
    32     }
    33 
    34     @RequestMapping("/say2/{name}")
    35     public String say2(@PathVariable("name") String name){
    36         return name + "say hello springboot 你大爷";
    37     }
    38 
    39     @RequestMapping("/json")
    40     public Map returnJson(){
    41         Map map = new HashMap();
    42         map.put("success",true);
    43         map.put("msg","恭喜你中奖了!!!");
    44         return map;
    45     }
    46 
    47 
    48 }

    Springboot配置文件

    内置属性

    注意:实际项目开发的时候Port=80,Context-path=/,以下配置只为讲解相关知识点

     

    自定义属性

    application.yml

     1 server:
     2   servlet:
     3     context-path: /
     4   port: 80
     5 
     6 user:
     7   uname: zs
     8   pwd: 123456
    

    HelloController相关代码

     1 /**
     2  * @author XuFanQi
     3  * @site
     4  * @company
     5  * @create 2019-11-24 15:50
     6  * RestController = responsebady + conterllor
     7  */
     8 @RestController
     9 public class HelloController {
    10 
    11     @Value("${user.uname}")
    12     private String uname;
    13     @Value("${user.pwd}")
    14     private String pwd;
    15 
    16     @Autowired
    17     private MysqlEntity mysqlEntity;
    18 
    19     @RequestMapping("/hello01")
    20     public String hello01(){
    21         return "Hello springboot ! ! !";
    22     }
    23 
    24     @RequestMapping("/say1")
    25     public String say1(String name){
    26         return name + "say hello springboot 你大爷";
    27     }
    28 
    29     @RequestMapping("/say2/{name}")
    30     public String say2(@PathVariable("name") String name){
    31         return name + "say hello springboot 你大爷";
    32     }
    33 
    34     @RequestMapping("/json")
    35     public Map returnJson(){
    36         Map map = new HashMap();
    37         map.put("success",true);
    38         map.put("msg","恭喜你中奖了!!!");
    39         return map;
    40     }
    41 
    42     @RequestMapping("/say3")
    43     public Map say3(){
    44         Map map = new HashMap();
    45         map.put("uname",uname);
    46         map.put("pwd",pwd);
    47         return map;
    48     }
    49 }

    浏览器访问结果

    属性封装类

    定义属性封装类

    application.yml

     1 server:
     2   servlet:
     3     context-path: /
     4   port: 80
     5 
     6 user:
     7   uname: zs
     8   pwd: 123456
     9   age: 18
    10   sex: "男"
    11   addr: "北京"
    MysqlEntity 
     1 package com.javaxl.springboot01.configurationProperties;
     2 
     3 import lombok.Data;
     4 import org.springframework.boot.context.properties.ConfigurationProperties;
     5 import org.springframework.stereotype.Component;
     6 
     7 /**
     8  * @author XuFanQi
     9  * @site
    10  * @company
    11  * @create 2019-11-24 16:52
    12  */
    13 @Component
    14 @Data
    15 @ConfigurationProperties(prefix = "user")
    16 public class MysqlEntity {
    17 
    18     private  String uname;
    19     private  String pwd;
    20     private  Integer age;
    21     private  String sex;
    22     private  String addr;
    23 }

    添加pom依赖,解决报红问题

    1 <dependency>
    2     <groupId>org.springframework.boot</groupId>
    3     <artifactId>spring-boot-configuration-processor</artifactId>
    4     <optional>true</optional>
    5 </dependency>
    HelloController 
     1 package com.javaxl.springboot01.controller;
     2 
     3 import com.javaxl.springboot01.configurationProperties.MysqlEntity;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.beans.factory.annotation.Value;
     6 import org.springframework.web.bind.annotation.PathVariable;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RestController;
     9 
    10 import java.util.HashMap;
    11 import java.util.Map;
    12 
    13 /**
    14  * @author XuFanQi
    15  * @site
    16  * @company
    17  * @create 2019-11-24 15:50
    18  * RestController = responsebady + conterllor
    19  */
    20 @RestController
    21 public class HelloController {
    22 
    23     @Value("${user.uname}")
    24     private String uname;
    25     @Value("${user.pwd}")
    26     private String pwd;
    27 
    28     @Autowired
    29     private MysqlEntity mysqlEntity;
    30 
    31     @RequestMapping("/hello01")
    32     public String hello01(){
    33         return "Hello springboot ! ! !";
    34     }
    35 
    36     @RequestMapping("/say1")
    37     public String say1(String name){
    38         return name + "say hello springboot 你大爷";
    39     }
    40 
    41     @RequestMapping("/say2/{name}")
    42     public String say2(@PathVariable("name") String name){
    43         return name + "say hello springboot 你大爷";
    44     }
    45 
    46     @RequestMapping("/json")
    47     public Map returnJson(){
    48         Map map = new HashMap();
    49         map.put("success",true);
    50         map.put("msg","恭喜你中奖了!!!");
    51         return map;
    52     }
    53 
    54     @RequestMapping("/say3")
    55     public Map say3(){
    56         Map map = new HashMap();
    57         map.put("uname",uname);
    58         map.put("pwd",pwd);
    59         return map;
    60     }
    61 
    62     @RequestMapping("/say4")
    63     public MysqlEntity say4(){
    64 
    65         return mysqlEntity;
    66     }
    67 }

     

  • 相关阅读:
    javascript封装自定义滚动条方法,可自定义四个边框滚动条
    前端页面优化:javascript图片延迟加载
    VPS用LNMP安装WordPress
    结缘PDO
    Laravel踩坑笔记——illuminate/html被抛弃
    Android定时器实现方法[转]
    关于安装Android Studio的一些问题的解决方法
    nginx 多站点配置方法
    PHP5.6.x的新鲜事
    ThinkPHP学习手记——环境搭建
  • 原文地址:https://www.cnblogs.com/xcn123/p/11923779.html
Copyright © 2020-2023  润新知