springboot(1)

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

1.新建
在这里插入图片描述在这里插入图片描述

勾选我们需要的内容,这里我们勾选最基本的:


在这里插入图片描述
然后等待仓库下载完成!

下载完成之后我们就可以看到springboot中的层级结构了
在这里插入图片描述
注意!:这里它自动创建的包和类不要删除,并且后面所写代码要在其包下,不能和Springboot01Application同级,不然无法扫描到你所写的代码。

由于springboot整合了tomcat,所以就相当于自带了一个tomcat,直接启动它就行了。
测试代码:
在这里插入图片描述
启动成功:在这里插入图片描述

package com.swx.springboot01.controller;

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

import java.util.HashMap;
import java.util.Map;

/**
 * @author Songwanxi
 * @site www.lentter.club
 * @company
 * @create  2019-12-26 19:49
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/say1")
    public String say1(){
        return "springboot 自带tomcat";
    }

    //测试带参
    @RequestMapping("/sayname")
    public String say1(String name ){
        return name+"测试带参数";
    }

    //测试带参2
    @RequestMapping("/sayname2/{name}")
    public String sayname(@PathVariable("name") String name){
        return name+"测试带参数";
    }

    //测试json
    @RequestMapping("/say2")
    public Map say2(){
        Map map = new HashMap();
        map.put("code",200);
        map.put("msg","测试json");
        return map;
    }


}

http://localhost:8080/hello/say1
在这里插入图片描述
http://localhost:8080/hello/say2
在这里插入图片描述
http://localhost:8080/hello/sayname?name=ls
在这里插入图片描述
http://localhost:8080/hello/sayname2/zs
在这里插入图片描述

Springboot配置文件

内置属性

内置属性有很多,这里就举例两个:
一般来说,项目实际开发时,Port=80,Context-path=/
在这里插入图片描述

自定义属性

Application.properties中配置

server.port=80
server.servlet.context-path=/

user.uname=swx
user.upwd=123

swx.name=swx
swx.age=18
swx.sex=男
swx.phone=110
swx.address=湖南


接着在controller层写:

package com.swx.springboot01.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Songwanxi
 * @site www.lentter.club
 * @company
 * @create  2019-12-26 19:49
 */
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Value("${user.uname}")
    private String uname;

    @Value("${user.upwd}")
    private String upwd;

    @RequestMapping("/say3")
    public Map say3(){
        Map map = new HashMap();
        map.put("uname",uname);
        map.put("upwd",upwd);

        return map;
    }

    @RequestMapping("/say1")
    public String say1(){
        return "springboot 自带tomcat";
    }

    //测试带参
    @RequestMapping("/sayname")
    public String say1(String name ){
        return name+"测试带参数";
    }

    //测试带参2
    @RequestMapping("/sayname2/{name}")
    public String sayname(@PathVariable("name") String name){
        return name+"测试带参数";
    }

    //测试json
    @RequestMapping("/say2")
    public Map say2(){
        Map map = new HashMap();
        map.put("code",200);
        map.put("msg","测试json");
        return map;
    }


}

测试一下:
在这里插入图片描述

属性封装类

在我们自定义属性只有几个时,上面的写法无可厚非,但是当我们需要得到一大串属性时怎么办?
写一个属性封装类:
首先导入属性封装类的jar包

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

接着在新建一个entity包,包里写SwxEntity属性封装类,
这里要特别注意它的注解!

package com.swx.springboot01.entity;

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

/**
 * @author Songwanxi
 * @site www.lentter.club
 * @company
 * @create  2019-12-26 20:05
 */
@Component //标志为spring管理
@ConfigurationProperties(prefix = "swx")//是自定义属性中的哪个前缀的属性
@Data//自动get、set、tostring
public class SwxEntity {
    private String name;
    private String age;
    private String sex;
    private String phone;
    private String address;
}

接着写controller层:

package com.swx.springboot01.controller;

import com.swx.springboot01.entity.SwxEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Songwanxi
 * @site www.lentter.club
 * @company
 * @create  2019-12-26 19:49
 */
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private SwxEntity swxEntity;

    @RequestMapping("/sayswx")
    public SwxEntity sayswx(){
        return swxEntity;
    }

    @Value("${user.uname}")
    private String uname;

    @Value("${user.upwd}")
    private String upwd;

    @RequestMapping("/say3")
    public Map say3(){
        Map map = new HashMap();
        map.put("uname",uname);
        map.put("upwd",upwd);

        return map;
    }

    @RequestMapping("/say1")
    public String say1(){
        return "springboot 自带tomcat";
    }

    //测试带参
    @RequestMapping("/sayname")
    public String say1(String name ){
        return name+"测试带参数";
    }

    //测试带参2
    @RequestMapping("/sayname2/{name}")
    public String sayname(@PathVariable("name") String name){
        return name+"测试带参数";
    }

    //测试json
    @RequestMapping("/say2")
    public Map say2(){
        Map map = new HashMap();
        map.put("code",200);
        map.put("msg","测试json");
        return map;
    }


}

测试一下:
在这里插入图片描述
成功拿到了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值