• 【Spring Boot】Day01


    一、Spring Boot的引入

    创建

    在这里插入图片描述
    在这里插入图片描述

    总结

    # 项目中集成spring和springmvc
    1. 新建项目
    2. 拷贝jar包(maven)
    (场景启动器)
    3. 配置spring.xml springmvc.xml
    4. 配置web.xml
    5. 配置tomcat
    
    springboot 就是优化了这些配置
    
    # springboot使用
    1. 创建maven项目
    2. 继承父模块
     
        <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.1.2.RELEASE</version>
       </parent>
     
      
    3. 引入web场景启动器
       
       <!--引入场景启动器-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       
    4. 添加Controller
       
       @Controller
       public class HelloController {
    
           @RequestMapping("/hello")
           @ResponseBody
           public String hello(){
                return "hello";
       }
    
    }
    5. 配置启动器类
       
       @SpringBootApplication
       public class App
       {
          public static void main( String[] args ){
             SpringApplication.run(App.class);
          }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    演示

    pom.xml
    在这里插入图片描述
    在这里插入图片描述
    ##在这里插入图片描述

    运行

    在这里插入图片描述
    在这里插入图片描述
    成功!

    可能出现的错误

    启动失败,端口号占用
    添加配置文件application.properties
    在这里插入图片描述

    二、使用idea创建Spring Boot

    创建

    在这里插入图片描述
    在这里插入图片描述
    创建完成之后
    在这里插入图片描述
    正确的结构
    在这里插入图片描述

    之后pom.xml自动创建
    在这里插入图片描述

    如果没有包,先创建包,也没有标记,再记得标记,然后再创建HelloCtroller,再运行。
    在这里插入图片描述

    运行http://localhost:8080/hello

    在这里插入图片描述

    三、介绍配置文件

    两种同种效果
    在这里插入图片描述

    介绍application.yml

    需要导入一个依赖

    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-configuration-processorartifactId>
    			<optional>trueoptional>
    		dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编写配置文件
    在这里插入图片描述

    server:
      port: 8080
    
    # 配置人的信息
    person:
      # 数据使用键值对形式
      #使用空格缩进来表示数据层级关系
      #字符串类型
      name: lisi
    
      #整数
      age: 18
    
      #日期
      birth: 2020//1/1
    
      #boolean
      b: false
    
      #list类型
      lists:
        - lisi
        - wangwu
    
      #map 类型
      maps: {k1: v1, k2: v2}
    
      dog:
        name: xiaogou
        age: 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    然后创建一个Person类,生成get,set,tostring

    • 读取配置文件中的数据,映射到此类的同名属性
      @ConfigurationProperties
      prefix = “person”: 读取person节点的数据
      @Component:让spring容器管理此类
      注意:类中的变量要与配置文件中的属性名最好要一致,比较规范,但是如java中的lastName属性,在yml中使用lastName或 last-name都可正确映射!【参考:https://blog.csdn.net/ThinkWon/article/details/100642870】
      并且类中全部的类型都是大写!!!!

    在这里插入图片描述

    package com.zy.springboot.bean;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    /***
     *
     * 读取配置文件中的数据,映射到此类的同名属性
     * @ConfigurationProperties
     *  prefix = "person": 读取person节点的数据
     *  @Component:让spring容器管理此类
     *
     *
     */
    @Component
    @ConfigurationProperties(prefix = "person")
    public class Person {
        private String name;
        private Integer age;
        private Date birth;
        private Boolean b;
        private List<String> lists;
        private Map<String,String> maps;
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Date getBirth() {
            return birth;
        }
    
        public void setBirth(Date birth) {
            this.birth = birth;
        }
    
        public Boolean getB() {
            return b;
        }
    
        public void setB(Boolean b) {
            this.b = b;
        }
    
        public List<String> getLists() {
            return lists;
        }
    
        public void setLists(List<String> lists) {
            this.lists = lists;
        }
    
        public Map<String, String> getMaps() {
            return maps;
        }
    
        public void setMaps(Map<String, String> maps) {
            this.maps = maps;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", birth=" + birth +
                    ", b=" + b +
                    ", lists=" + lists +
                    ", maps=" + maps +
                    '}';
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    测试
    在这里插入图片描述
    在这里插入图片描述
    再创建一个Dog类:
    在这里插入图片描述

    package com.zy.springboot.bean;
    
    public class Dog {
        private String name;
        private Integer age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Dog{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    然后在person类中添加private Dog dog;生成get\set方法,并重新tostring(),输出检测:
    在这里插入图片描述

  • 相关阅读:
    springMVC视图、视图解析器、国际化笔记
    从零开始用 Axios 请求后端接口
    Linux系统安全:安全技术和防火墙
    计算机组成原理【2022-10-24】
    算法训练营第三天 | 203.移除链表元素、707.设计链表 、206.反转链表
    springboot实战(四)之整合mybatis-plus
    华为 EVC兼容性
    代码随想录算法训练营第五十九天丨 单调栈02
    动画圆圈文字标志效果
    [免费专栏] Android安全之动态代码注入技术(利用JDB调试APK)
  • 原文地址:https://blog.csdn.net/weixin_44926962/article/details/127836820