SpringBoot入门

时间:2019-02-16
本文章向大家介绍SpringBoot入门,主要包括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整合了所有的框架

2.使用Idea配置SpringBoot项目

2.1 创建SpringBoot项目
File–>New–>Project…
Spring Initializr
Maven Project
Web
目录结构
java源文件夹中的Springboot01Application.java是整个项目的启动类

static:存放的是静态资源的文件

templetes:存放的项目所需的页面

application.properties里面存放的是项目的全局配置信息

成功界面:

2.3 springboot的配置修改
application.properties–>application.yml

注意格式

server:
  port: 8080
  servlet:
    context-path: /springboot1
jdbc:
  driver: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306/db_ssm?serverTimezone=UTC&characterEncoding=utf-8
  username: root
  password: zr1001323Xzhang

controller代码

package com.zhang.springboot.Controller;

import com.zhang.springboot.entity.MysqlEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author张瑞
 * @company 个人
 * @create 2019-02-16 15:36
 */
@Controller
public class HelloController {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Resource
    private MysqlEntity mysqlEntity;


    @ResponseBody
    @RequestMapping("/")
    public String hello(){
        return driver+"<br/>"+url+"<br/>"+driver+"<br/>"+password+"<br/>";
    }

    @ResponseBody
    @RequestMapping("/mysql1")
    public MysqlEntity mysql1(){
        return mysqlEntity;
    }
}

实体类代码

package com.zhang.springboot.entity;

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

/**
 * @author张瑞
 * @company 个人
 * @create 2019-02-16 15:48
 */
@Component
@ConfigurationProperties(prefix = "jdbc")
public class MysqlEntity {
    private String driver;
    private String url;
    private String username;
    private String 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;
    }
}