SpringBoot(一):入门

1.SpringBoot是什么?

  
  Spring Boot它本身并不提供Spring框架的核心特性以及扩展功能,
		  只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。
	     也就是说,它并不是用来替代Spring的解决方案,
		而是和Spring框架紧密结合用于提升Spring开发者体验的工具。
  
  同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等)
  		,Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box)
  		,大部分的Spring Boot应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑

说明:

  ① 敏捷式开发
  ② spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,
        就像maven整合了所有的jar包,spring boot整合了所有的框架
  ③ spring cloud/dubbo:>=jdk1.8

2.使用Idea配置SpringBoot项目

(1) 创建SpringBoot项目
右键new——>model
SpringBoot(一):入门
点击next
出现以下页面
SpringBoot(一):入门
SpringBoot(一):入门
最后直接点击finish

(2) 目录结构

src/main/java:主程序入口 Application,可以通过直接运行该类来启动Spring Boot应用

src/main/resources:配置目录,该目录用来存放应用的一些配置信息,比如应用名、服务端口、数据库配置等。由于我们应用了Web模块,因此产生了 static目录与templates目录,前者用于存放静态资源,如图片、CSS、JavaScript等;后者用于存放Web页面的模板文件。
src/test:单元测试目录,生成的 ApplicationTests 通过 JUnit4实现,可以直接用运行 Spring Boot应用的测试。

application.properties/application.yml 用于存放程序的各种依赖模块的配置信息,比如 服务端口,数据库连接配置等。。。

注1:包和类不能乱改,只能在com.zking.springboot01建子包,
因为程序只加载Application.java所在包及其子包下的内容
com.zking.springboot01
controller
service
mapper
model

(3) springboot的配置修改

application.properties:

server.port=8081
server.servlet.context-path=/springboot

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8
mysql.username=root
mysql.password=123

改成:application.yml

server:
  port: 8081
  servlet:
    context-path: /springboot

mysql:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8
  username: root
  password: 123

注1:application.yml,yml是什么格式?

3、

① 自定义属性
src—>main—>java—com.zking.springboot01—>controller—>HelloController

package com.zking.springboot01.controller;

import com.zking.springboot01.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小李飞刀
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-02-16 16:56
 */
@Controller
public class HelloController {
    @Value("${mysql.driver}")
    private String driver;
    @Value("${mysql.url}")
    private String url;
    @Value("${mysql.username}")
    private String username;
    @Value("${mysql.password}")
    private  String password;
 
    @ResponseBody
    @RequestMapping("/mysql1")
    public String mysql1(){
        return this.driver+"</br>"+this.url+"</br>"+this.username+"</br>"+this.password+"</br>";
    }
 

② 属性封装类
src—>main—>java—com.zking.springboot01—>MysqlEntity

package com.zking.springboot01;

import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author小李飞刀
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-02-16 19:03
 */
@ToString
@Component
@ConfigurationProperties(prefix = "mysql")
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;
    }
}

src—>main—>java—com.zking.springboot01—>controller—>HelloController

package com.zking.springboot01.controller;

import com.zking.springboot01.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小李飞刀
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-02-16 16:56
 */
@Controller
public class HelloController {

    @Resource
    private MysqlEntity mysqlEntity;
 
    @ResponseBody
    @RequestMapping("/mysql2")
    public MysqlEntity mysql2(){
        return this.mysqlEntity;
    }
}