Spring Boot 快速入门

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 大纲下面您将会看到:Spring Boot 快速入门案例Spring Boot 整合持久层开发Spring Boot 整合控制层开发Spring Boot 整合视图层开发Spring Boot 整合安全框架Spring Boot 整合定时任务Spring Boot 测试Spring Boot 自动装配原理导图如下Spring Boot 快速入门案例介绍spring boot 是用来简化原先的应用开发,属于一种轻量级框架,程序员不需要编写大量的配置文件,即可以运行起来应用。

大纲

下面您将会看到:

  1. Spring Boot 快速入门案例
  2. Spring Boot 整合持久层开发
  3. Spring Boot 整合控制层开发
  4. Spring Boot 整合视图层开发
  5. Spring Boot 整合安全框架
  6. Spring Boot 整合定时任务
  7. Spring Boot 测试
  8. Spring Boot 自动装配原理

导图如下

在这里插入图片描述

Spring Boot 快速入门案例

介绍

spring boot 是用来简化原先的应用开发,属于一种轻量级框架,程序员不需要编写大量的配置文件,即可以运行起来应用。

spring boot 有如下特性
1、 spring boot 简单易上手
2、 spring boot 内嵌 tomcat 部署方便
3、 spring boot 可以零配置

安装

这里使用 idea 向导构建 spring boot 应用

选择新建应用,选择 spring Initializr

在这里插入图片描述

选择坐标

在这里插入图片描述
选择 Developer Tools 选项中的 Spring boot DevTools , lombook, Spring Configuration Processor 其中 Spring boot DevTools 为spring boot 额外提供的开发工具,lombook为简化POJO 类的 get set 方法,Spring Configuration Processor 用于进行 Spring boot 应用配置的读取。

选择如下

在这里插入图片描述

由于是 web 应用,需要选择 web 选项,选择 Spring Web Starter 用于为该web应用添加启动类。

选择如下 如图 1 - 4

在这里插入图片描述

再次选择 sql 选项。选择 MyBatis Framework 这里持久层框架使用 MyBatis

选择 Project name 和 Project location 如图 1 - 5

在这里插入图片描述

选择自动加载 maven 即 Enable Auto-Import 如图 1 - 6

在这里插入图片描述

此时新建目录如下 如图 1 - 7

在这里插入图片描述

各目录详细解释

.idea 目录: idea 编译器的配置目录,保存Idea的配置文件
.mvn 目录: 用于对 maven 的相关配置
src 目录 保存 项目源代码
.gitignore 文件 用于 git 文件文件上传过滤文件
HELP.md 帮助文档
mvnw 文件 Java 项目的脚本启动文件 linux 直接运行 mvnw 即可启动,windows 运行 mvnw.cmd 文件 即可启动
pom.xml 文件 用于进行 maven 的依赖
.imi 文件 用于进行该项目的 idea 配置
连接数据库
创建一张测试表,sql 语句如下 如 代码 1 - 8

/*
 Navicat Premium Data Transfer


 Source Server         : t
 Source Server Type    : MySQL
 Source Server Version : 50718
 Source Host           : cdb-1yfd1mlm.cd.tencentcdb.com:10056
 Source Schema         : test


 Target Server Type    : MySQL
 Target Server Version : 50718
 File Encoding         : 65001


 Date: 25/08/2019 02:50:49
*/


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;


-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test`  (
  `id` int(11) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;


SET FOREIGN_KEY_CHECKS = 1;

将 resources 目录下的 application.properties 文件名更改为 application.yml

Spring boot 配置相关数据源如下

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///test
    username: root
    password: 

编写持久层
创建 POJO Test 类

package com.example.ming.POJO;

import lombok.Data;

@Data
public class Test {
    private String id;
    private String name;
}

创建 Mapper 如代码 1 - 10

package com.example.ming.mapper;

import com.example.ming.POJO.Test;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TestMapper {

    @Select("SELECT * FROM  test")
    public List<Test> findAll() throws Exception;
}

该 Mapper 需要添加 @Mapper 注解,表明是 Mybatis 的 mapper

启动类添加扫描注解 如 代码 1 - 11

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
@MapperScan("com.example.ming.mapper")
public class MingApplication {

    public static void main(String[] args) {
        SpringApplication.run(MingApplication.class, args);
    }

}

编写 service 层

package com.example.ming.controller;

import com.example.ming.POJO.Test;
import com.example.ming.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Service("TestService")
public class TestController {
    @Autowired
    private TestMapper testMapper;
    
    public List<Test> findAll()throws Exception{
        return testMapper.findAll();
    } 
}

添加 Service 注解 表明该 Service 名称为 TestService 其对象管理交由 Spring 容器进行处理。添加 Autowired 注解 , 表明 Spring 容器进行自动装配 TestMapper 对象 。

编写 Controller 层

package com.example.ming.controller;

import com.example.ming.POJO.Test;
import com.example.ming.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Test")
public class TestController {
    @Autowired
    private TestService testService;

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public List<Test> findAll()throws Exception{
        return testService.findAll();
    }
}

添加注解 RestController 表明是一个返回 Json 数据格式的 Controller
添加注解 RequestMapper 表明此类是在 Test 链接下
类方法添加注解 RequestMapper 表明此类是在 Test 链接下的 findAll 链接, 接收的方法为 GET 方法。
运行应用
运行应用 main 方法

访问 链接 http://localhost:8080/Test/findAll

结果如图

在这里插入图片描述

此时,若能显示出 json 数据,表明整个应用已经启动完成。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
26天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
40 0
|
6月前
|
存储 缓存 NoSQL
快速入门:Spring Cache
快速入门:Spring Cache
53 0
|
5天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
19 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
7天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
30 0
【Spring系列】Sping VS Sping Boot区别与联系
|
2月前
|
XML 监控 druid
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
|
3月前
|
Java
springboot项目打包瘦身
springboot项目打包瘦身
|
5月前
|
Java 测试技术
Springboot集成JUnit5优雅进行单元测试
Springboot集成JUnit5优雅进行单元测试
|
7月前
|
安全 Java 数据安全/隐私保护
Spring Security快速入门
Spring Security快速入门
116 0
|
7月前
|
存储 Java 数据库
Spring快速入门
Spring快速入门
|
7月前
|
JSON Java API
Spring Boot之Jackson快速入门,你必须得会!
在上一期《SpringBoot之Jackson配置全局时间日期格式》文中提到Jackson,了解到有很多小伙伴对它很感兴趣;顾这一期,我就重点带着大家以最基础的教学方式领大家入门,废话不多说,咱们这就开始。