遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

SpringBoot简易教程(01):SpringBoot基础入门

 

教程汇总请查看这里https://www.cnblogs.com/uncleyong/p/17984075

简介

官网:https://spring.io/projects/spring-boot

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

SpringBoot不是Spring官方的框架模式,而是一个团队在Spring4.0版本上二次开发并开源公布出来的。简而言之,SpringBoot就是一个轻量级,简化配置和开发流程的web整合框架。

SpringBoot是Spring开发的脚手架。

 

创建一个springboot项目:方式1-1,一个项目下可以创建多个Module

创建项目

 

 空项目

 

输入项目名称

 

选择sdk等

 

配置maven

 

新建模块(这里演示只创建一个Module)

 

选择“Spring Initializr”,然后选择jdk版本,另外,默认是从https://start.spring.io拉取代码模板

 

Group和Package改为一样

 

选择starter(SpringBoot提供了许多Starter,也就是启动器,分别对应不同中的应用场景,只要在项目中引入这些starter,相应场景的依赖就会被导入)

web项目,选择"Spring Web"

 

如果要操作数据库,可以选择数据库依赖

 

模块名

 

下面四个可以删除

 

生成的项目

resources下:

  static:保存所有的静态资源,js css images;

  templates:保存所有的模板页面,可以使用模板引擎(freemarker、thymeleaf);

  application.properties:Spring Boot应用的配置文件,可以修改一些默认设置;

 

pom中父工程版本找不到???

 

我们先把父工程的版本换成2.4.5吧

说明:Spring Boot提供了一个名为spring-boot-starter-parent的工程,这个父工程的父工程(spring-boot-dependencies)里面已经对各种常用依赖的版本进行了管理

 

自动导入了很多依赖

 

编写一个控制器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.qzcsbj.demo.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@RestController
public class HelloController {
    @RequestMapping(value="hello", method = RequestMethod.GET)
    public String hello(){
        return "hello springboot";
    }
}

 

@RestController = @controller = @ResponseBody

 

 

运行启动类

 

下面日志中可以看到,端口是8080

 

请求:localhost:8080/hello

与之前springmvc相比,少了很多配置,用起来更方便。

返回json

添加依赖

1
2
3
4
5
6
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
    <scope>provided</scope>
</dependency>

  

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.qzcsbj.demo.bean;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer 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
package com.qzcsbj.demo.controller;
 
import com.qzcsbj.demo.bean.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@RestController
public class HelloController {
    @RequestMapping(value="hello", method = RequestMethod.GET)
    public String hello(){
        return "hello springboot";
    }
 
    @RequestMapping("/users")
    public List<User> findUsers(){
        List<User> users = new ArrayList<>();
        users.add(new User("jack",18));
        users.add(new User("lucy",19));
        return users;
    }
}

  

请求:127.0.0.1:8080/users

 

创建一个springboot项目:方式1-2,可以直接创建项目和Module

 

 

 

 

 

删除:.mvn、.gitignore、mvnw、mvnw.cmd

 

pom中,java.version修改为1.8

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qzcsbj.demo</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

 

选择jdk等

 

配置maven

说明:上面配置仅仅对本项目生效

idea每次新建项目都要重新配置maven的解决方案:https://www.cnblogs.com/uncleyong/p/16199194.html

 

创建一个springboot项目:方式2,在Spring Boot官方Initializer页面在线构建工程再导入到idea中

https://spring.io/projects/spring-boot

 

 

https://start.spring.io/

 

会自动下载生成的项目代码,然后导入idea。

 

【bak】

原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17064293.html

 

posted @   全栈测试笔记  阅读(189)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2021-01-22 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte
2021-01-22 宿主机ping不通虚拟机,虚拟机能ping通宿主机
2020-01-22 【V】自动化必备:自动化持续集成环境搭建(下):git + maven + jenkins
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示