栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot教程

1.概述

spring官网 spring.io
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

2.springboot入门案例 2.1 springboot的创建

先创建一个空工程

2.1.1 方式一:用IDEA initializr来创建



2.1.1.1 一个最简单的springboot

1:创建一个controller

2.执行main方法
3.浏览器访问地址 localhost:8080/book

2.1.2 方式二:spring官网创建






将下载的压缩包解压到工作目录即可



2.1.3 方式三:阿里云创建

与方式一相同,改ip即可


2.1.4 方式四:Maven的方式手动创建



编辑pom.xml



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.2
         
    

    com.peppacatt
    springboot_01_04_quickstart
    1.0-SNAPSHOT

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    



添加启动类:

2.2 parent依赖

所有spring官方的starter都是以spring-boot-starter开头spring-boot-starter-xxx格式.例如:

 
     org.springframework.boot
     spring-boot-starter-web
 

第三方技术的starter以spring-boot-starter结尾xxx-spring-boot-starter格式,例如:

 
     org.mybatis.spring.boot
     mybatis-spring-boot-starter
     2.2.0
 


查看springboot的parent依赖


找到parent标签,ctrl+鼠标左键 进入

继续点击进入

查看properties中定义的版本

展开properties标签

展开 标签

2.3 启动类


查看启动类

package com.peppacatt.springboot_01_01_quickstart;

import com.peppacatt.springboot_01_01_quickstart.controller.BookController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Springboot0101QuickstartApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext cac = SpringApplication.run(Springboot0101QuickstartApplication.class, args);
        BookController bookController = cac.getBean(BookController.class);
        System.out.println(bookController);
    }

}

查看运行结果:

2.4 内置服务器

2.4.1springboot内置Tomcat服务器

2.4.2 更换内置服务器

2.5 属性配置application.yml

springboot三种配置文件类型,名称都必须为application.xxx

  • application.properties
  • application.yml
  • application.yaml

在开发中只会有一个配置文件,如果配置文件共存,则:

spring官网属性配置文档



idea属性提示消失解决方案



yml数据格式


yml语法规则:

字面值的表示方式

数组的表示方式

数组格式:

①和②两者格式都可以,②是①的缩略版

对象数组格式:

总结

读取yml配置文件数据


yml:

spring读取:

结果:

总结

yml属性引用


总结

封装对象全部属性Environment

自定义类封装yml属性

1.新建

新建封装类:

package com.peppacatt.springboot_03_yml.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


//定义为spring管理的bean
@Component
//指定加载的数据
@ConfigurationProperties(prefix = "cat")
public class PropertiesCat {
    private String name;
    private Integer age;
    private Integer gender;

    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 Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "YmlCat{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", gender=" + gender +
                '}';
    }
}

使用:

3 springboot整合 3.1 springboot整合Junit

springboot已整合
使用案例:



classes属性

如果测试类不在启动类的包或者子包下面,则需要添加classes

3.2 springboot整合MyBatis

添加依赖:

或者手动添加:

 
     org.mybatis.spring.boot
     mybatis-spring-boot-starter
     2.2.0
 

 
     mysql
     mysql-connector-java
     runtime
 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684901.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号