zoukankan      html  css  js  c++  java
  • spring boot入门

    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项目

    更改项目名。不能包含大写字母

    勾选项目需要的jar包。

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

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

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

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

    初次启动

    案例代码

     1 package com.yuan.springboot01.controller;
     2 
     3 import org.springframework.web.bind.annotation.PathVariable;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RestController;
     6 
     7 import java.util.HashMap;
     8 import java.util.Map;
     9 
    10 @RestController
    11 public class HelloController {
    12 
    13     @RequestMapping("/hello1")
    14     public String hello1(){
    15         return "hello springboot!!!";
    16     }
    17 
    18     @RequestMapping("/say1")
    19     public String say1(String name){
    20         return name + "。。say hello springboot 谢谢观看!!!";
    21     }
    22 
    23     @RequestMapping("/say2/{name}")
    24     public String say2(@PathVariable("name") String name){
    25         return name + "。。say hello springboot 谢谢观看!!!";
    26     }
    27 
    28     @RequestMapping("/json")
    29     public Map returnJson(){
    30         Map map = new HashMap();
    31         map.put("success",true);
    32         map.put("msg","恭喜你中奖了!!!");
    33         return map;
    34     }
    35 
    36 
    37 }

    测试结果

    Springboot配置文件

    修改端口号

    当配置较多的时候或者在公司上班的时候一般使用的是.yml后缀。层次感清晰,方便找到配置信息

    在Controller层获取yml文件的配置信息

    直接获取

    controller层代码

    当某一个配置信息较多时可以选择用实体类的方式进行获取

    看。。代码

    application.yml

     1 server:
     2   servlet:
     3     context-path: /
     4   port: 8081
     5 
     6 user:
     7   uname: zs
     8   pwd: 123456
     9   age: 23
    10   sex: nv
    11   adder: shang hai

    MysqlEntity实体类

     1 package com.yuan.springboot01.configurationProperties;
     2 
     3 import lombok.Data;
     4 import org.springframework.boot.context.properties.ConfigurationProperties;
     5 import org.springframework.stereotype.Component;
     6 
     7 @Component
     8 @Data
     9 @ConfigurationProperties(prefix = "user")
    10 public class MysqlEntity {
    11     private String uname;
    12     private String pwd;
    13     private Integer age;
    14     private String sex;
    15     private String adder;
    16 
    17 
    18 
    19 }

    在编写实体的时候会出现报红的现象,此时需要导入一个pom依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    Controller层

     1 package com.yuan.springboot01.controller;
     2 
     3 import com.yuan.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 javax.xml.ws.soap.Addressing;
    11 import java.util.HashMap;
    12 import java.util.Map;
    13 
    14 @RestController
    15 public class HelloController {
    16 
    17     @Value("${user.uname}")
    18     private String uname;
    19     @Value("${user.pwd}")
    20     private String pwd;
    21 
    22     @Autowired
    23     private MysqlEntity mysqlEntity;
    24 
    25 
    26     @RequestMapping("/say4")
    27     public MysqlEntity say4(){
    28         return mysqlEntity;
    29     }
    30 
    31 
    32 }

    谢谢观看!!!

  • 相关阅读:
    Python -- Redis List
    Python --Redis Hash操作
    Python使用redis介绍
    缓存服务器
    linux python3获取ip地址
    Rabbitmq -- rpc
    Rabbitmq--topic
    Rabbitmq -- direct
    删除rabbitmq中持久化的队列和数据
    Exchange-fanout 广播模式
  • 原文地址:https://www.cnblogs.com/ly-0919/p/11924168.html
Copyright © 2011-2022 走看看