SpringBoot学习笔记1 框架及基础配置

发布于:2023-01-13 ⋅ 阅读:(441) ⋅ 点赞:(0)

 

 

快速上手SpringBoot

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

package com.itheima.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//JavaWeb 能够基于Restful实现页面请求交互功能
//Rest模式,这是一个最简单的基于Restful的springmvc控制器
@RestController
@RequestMapping("/books")
public class BookController {

    @GetMapping
    public String getById(){
        System.out.println("springboot is running..");
        return "springboot is running";
    }

}

 

 

 当idea不能联网时,可以基于springboot官网创建 https://start.spring.io/

 如果官网被屏蔽了呢,通过阿里云官网访问

 

 隐藏不想看到的文件

 

springboot将各个jar包的坐标和版本分开管理,并提供了一系列的常用版本坐标,解决了不同技术所用jar包版本冲突的问题,因此我们只需要写jar包的名字就可以了,不用写版本了以后 

实现方式(充分利用了starter依赖传递的思想)

总之:

开发SpringBoot程序需要导入坐标时通常导入对应的starter
每个不同的starteri根据功能不同,通常包含多个依赖坐标
使用starter可以实现快速配置的效果,达到简化配置的目的

实际开发时:

使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot:提供,除非SpringBoot未提供对应版本V
如发生坐标错误,再指定Version(要小心版本冲突)

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

Restful知识回顾-隶属于SpringMVC

 

 根据Rest风格对资源进行访问称为Restful

要点一:无参方法在传统的@RequestMapping(/users)后面,继续添加一个method的参数,指定传输形式并遵循Rest风格

要点二:有参方法在要点一的基础上,在(/user后面添加一个动态路径{id}),并在方法的传参部分加前缀 @PathVariable

 

 

@RequestBody  @RequestParam  @PathVariable
·区别
        ■@RequestParam用于接收url地址传参或表单传参
        ■@RequestBody用于接收json数据
        ■@PathVariable用于接收路径参数,使用{参数名称)描述路径参数
●应用
        ■后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody)应用较广
        ■如果发送非json格式数据,选用@RequestParam接收请求参数
        ■采用RESTful:进行开发,当参数数量较少时,例如1个,可以采用@PathVariable接收请求路径变量,通常用于传递id值

进一步简化

1、 代替

2、头文件

 3、传输方式

 

SpringBoot基础配置

教你一招:复试工程

原则:保留工程基础结构、抹掉原始工程痕迹

属性配置

application.properties //boot的配置文件

1、修改端口

#服务器端口配置
server.port=80

2、修改banner

# 修改banner
# spring.main.banner-mode=off
# spring.banner.image.location=logo.png

3、修改日志级别

# 日志
logging.level.root=info
# logging.level.com.itheima=warn

SpringBoot所有配置的参数文档

配置文件分类

  

 配置.yaml 和 .yml 为配置文件 , 以获得idea的提示功能

 


yaml文件

 

 

 

country: china
province: beijing
city: beijing
area: haidian

port: 8080
#普通参数的写法
party: true

birthday: 1949-10-01
#对象的写法
user:
  name: itcast
  age: 16

user2:
  name: itheima
  age: 16

#用五级属性名来描述一个数据
a:
  b:
    c:
      d:
        e: 123

#描述一个人的多个信息 多个数据之间使用 - 进行区分,写数组
likes:
  - game
  - music
  - sleep

likes2: [game,music,sleep]

#数组的写法
users:
  - name: zhangsan
    age: 18
  - name: lisi
    age: 17

users3: [{name:zhangsan,age:18},{name:lisi,age:17}]

users2:
  -
    name: zhangsan
    age: 18
  -
    name: lisi
    age: 17


yaml数据读取

    //读取yaml数据中的单一数据
    @Value("${country}")//spring注入普通属性
    private String country1;

    // user:
    // name: itcast
    // age: 16
    @Value("${user.name}")
    private String name1;

    //读取数组中第[1]位
    @Value("${likes[1]}")
    private String likes1;
    //读取数组中第[1]位中的.age数据
    @Value("${users[1].age}")
    private String age1;

    @Value("${server.port}")
    private String port;

yaml文件中的变量引用

#使用${属性名}的方式引用数据
tempDir: ${baseDir}\temp
#使用引号包裹的字符串,其中的转义字符可以生效
tempDir2: "${baseDir}\temp \t1 \t2 \t3"
    @Value("${tempDir}")
    private String tempDir;

    //使用自动装配将所有的数据封装到一个对象Environment中(自动封装了全数据)
    //使用在字段上用于根据类型依赖注入
    //可以任意读取了
    //System.out.println(env.getProperty("server.port"));
    //System.out.println(env.getProperty("user.name"));
    @Autowired
    private Environment env;
    System.out.println(env.getProperty("server.port"));
    System.out.println(env.getProperty("user.name"));

 yaml封装对象数据(重中之重)用于boot内各种技术的配置信息,如sql数据库连接信息

1、在yaml中定义一组数据

datasource:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/springboot_db
  username: root
  password: root666123

2、编写实体对象封装这组数据 通过@ConfigurationProperties(perfix = "enterprise")连接关系

package com.itheima;

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

//1.定义数据模型封装yaml文件中对应的数据
//2.定义为spring管控的bean
@Component
//3.指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
    private String driver;
    private String url;
    private String username;
    private String password;

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

3、调用

    @Autowired
    private MyDataSource myDataSource;
    System.out.println(myDataSource);


网站公告

今日签到

点亮在社区的每一天
去签到