Spring Boot是Spring 全家桶非常重要的一个模块,通过 Spring Boot 可以快速搭建一个基于 Spring 的 Java 应用程序,Spring Boot 对常用的第三方库提供了配置方案,可以很好地和 Spring 进行整合,MyBatis、Spring Data JPA 等,可以一键式搭建功能完备的 Java 企业级应用。

  Spring Boot 的优势

    - 不需要任何 XML 配置文件。
    - 内嵌 Web 服务器,可以直接启动。
    - 默认支持 JSON 数据,不需要做额外配置。
    - 支持 RESTful 风格
    - 使用一个配置文件(非 XML、propertis、YAML)可以配置所有的个性化信息

  Spring Boot 就是一个可以用很少的配置快速搭建 Spring 应用的框架,并且可以自动集成主流的 Java 技术栈。

  Spring Boot有两种创建方式

    - 在线创建工程

    - 手动创建工程

  这里演示一下在线创建

1、启动idea,点击Create New Project

  

2、选择Spring Initializr--Default: https://start.spring.io--next

  

3、输入Group、Artifact等--点击next

  

4、选择web--勾选Spring Web--点击next

  

5、选择路径--点击finish

  

6、OK,spring boot项目创建成功了,如果是第一次创建spring boot项目的话,需要等待一会,下载pom依赖

  

7、添加pom.xml依赖

<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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- 数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.14</version>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
    <!-- mybaits -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
</dependencies>

8、配置application.properties文件

注意:Spring Boot支持 .properties / .yml两种格式的配置文件,如果两种都存在时,以第一种优先。这里我修改为了.yml后缀的(便于书写,建议使用此格式的)

为了演示例子,这里只做最简单配置

server:
  port: 7777

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/sunjian2?&useSSL=false&serverTimezone=UTC

9、创建实体类

package com.sunjian.demo.entity;

import lombok.Data;

/**
 * @author sunjian
 * @date 2020/3/24 23:20
 */
@Data
public class Person {
    private Integer id;
    private String name;
    private String age;
    private String gender;
    private String email;
    private String city;
}

10、创建dao层接口

package com.sunjian.demo.dao;

import com.sunjian.demo.entity.Person;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * @author sunjian
 * @date 2020/3/24 23:25
 */
@Mapper
public interface UserDao {
    @Select("select * from person where id = #{id}")
    Person findById(Integer id);
}

11、创建service层接口及实现类

package com.sunjian.demo.service;

import com.sunjian.demo.entity.Person;

/**
 * @author sunjian
 * @date 2020/3/24 23:29
 */
public interface PersonService {
    public Person findById(Integer id);
}
package com.sunjian.demo.service.impl;

import com.sunjian.demo.dao.UserDao;
import com.sunjian.demo.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sunjian
 * @date 2020/3/24 23:30
 */
@Service
public class PersonServiceImpl {
    @Autowired
    private UserDao userDao;

    public Person findById(Integer id){
        return userDao.findById(id);
    }
}

12、创建controller视图层类

package com.sunjian.demo.controller;

import com.sunjian.demo.entity.Person;
import com.sunjian.demo.service.PersonService;
import com.sunjian.demo.service.impl.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sunjian
 * @date 2020/3/24 23:32
 */
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonServiceImpl personServiceImpl;

    @GetMapping("/findById/{id}")
    public Person findById(@PathVariable("id") Integer id){
        Person person = personServiceImpl.findById(id);
        System.out.println(person);
        return person;
    }
}

13、启动项目(Spring Boot内置了Tomcat web服务器,直接运行DemoApplication启动类文件即可启动项目),访问

  

  

  

OK.

Spring Boot框架——快速入门的更多相关文章

  1. (Spring Boot框架)快速入门

    Spring Boot 系列文章推荐 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Boot 静态资源处理 今天介绍一 ...

  2. Spring Boot:快速入门教程

    什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...

  3. Spring Boot【快速入门】简单案例

    Spring Boot[快速入门]   Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point ...

  4. Spring Boot【快速入门】

    Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for building all Sp ...

  5. 【转载】Spring Boot【快速入门】2019.05.19

    原文出处:https://www.cnblogs.com/wmyskxz/p/9010832.html   Spring Boot 概述 Build Anything with Spring Boot ...

  6. Spring Boot WebFlux 快速入门实践

    02:WebFlux 快速入门实践 Spring Boot 2.0 spring.io 官网有句醒目的话是: BUILD ANYTHING WITH SPRING BOOT Spring Boot ( ...

  7. Spring Boot (十四): 响应式编程以及 Spring Boot Webflux 快速入门

    1. 什么是响应式编程 在计算机中,响应式编程或反应式编程(英语:Reactive programming)是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流 ...

  8. Spring Boot JWT 快速入门

    本章节讨论 jwt 在 spring boot 中的应用.意在快速入门 jwt. java jdk1.8 maven 3.2+ spring boot 2.0+ JSON Web Token(JWT) ...

  9. Spring Boot:快速入门

    上一篇讲述什么是Spring Boot,这一篇讲解怎么使用IDE工具快速搭建起来独立项目. 一.构建方式 快速搭建项目有三种方式,官方也有答案给到我们: 二.构建前准备 想要使用IDE运行起来自己的S ...

随机推荐

  1. 吴裕雄--天生自然 R语言开发学习:时间序列(续三)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  2. Docker学习笔记_04 Rancher的部署安装(编排选用K8S)

    原文地址:http://dbase.cc/2018/01/12/docker/04_rancher的部署安装/ 为什么要使用Rancher Rancher是一个开源的企业级容器管理平台.通过Ranch ...

  3. 在linux中自动向设备中安装apk包

    环境:华为手机 linux centos64 为了锻炼自己,我把脚本文件和APK文件放到了不同的路径下. 需求:将虚拟机中的100个apk包安装到手机中. import os,time os.chdi ...

  4. python登陆接口编写

    #coding:utf-8 import getpass,sys i=0 j=0 while i<3: username=raw_input('username:') #输入用户名 life_1 ...

  5. numpy的索引

    import numpy as np A =np.arange(3,15).reshape(3,4) print(A) #第一行 print(A[2]) #返回元素 print(A[1][2]) pr ...

  6. Kubelet

    Kubelet 相关博客 Kubelet组件深度解析 Kubelet组件解析 Kubelet运行机制分析 Kubelet与apiserver通信 ___ Kubelet组件运行在Node节点上,维持运 ...

  7. qt creator源码全方面分析(3-1)

    目录 qtcreator.pro 包含qtcreator.pri include(filename) Qt版本判断 message(string) $$运算符 error(string) 包含doc. ...

  8. MySQL多表查询、事务、DCL:内含mysql如果忘记密码解决方案

    MySQL多表查询.事务.DCL 多表查询 * 查询语法: select 列名列表 from 表名列表 where.... * 准备sql # 创建部门表 CREATE TABLE dept( id ...

  9. 设计模式-11享元模式(Flyweight Pattern)

    1.模式动机 在面向对象程序设计过程中,有时会面临要创建大量相同或相似对象实例的问题.创建那么多的对象将会耗费很多的系统资源,它是系统性能提高的一个瓶颈. 享元模式就是把相同或相似对象的公共部分提取出 ...

  10. java反序列化-ysoserial-调试分析总结篇(4)

    1.前言 这篇文章继续分析commoncollections4利用链,这篇文章是对cc2的改造,和cc3一样,cc3是对cc1的改造,cc4则是对cc2的改造,里面chained的invoke变成了i ...