1. Spring Boot简介

初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好。所以很久以来我的Spring学习都是出于停滞状态的。

不过这种状态在我接触了Spring Boot之后,就发生了改变。Spring官方可能也觉得庞杂的配置是一件很不优雅的事情,虽然加入了包扫描,但是也没有触及灵魂。

Spring还有一个问题就是依赖的冲突问题,这个对我这种半道出家的程序员来说更是痛苦至极。

还好,所有的痛苦都被Spring Boot终结了。

Spring Boot有三个特点:

  • 自动配置
  • 起步依赖
  • Actuator对运行状态的监控

每一个特点都那么的美好。

其实开发人员的本职工作是什么?是完成业务代码的编写,实现业务功能,因此如果消耗大量时间在Spring本身的配置和依赖冲突解决上,那么等于是浪费了大量的时间。

Spring Boot的出现,可以说是对生产力的一次解放。

闲话少叙,看一个需求。

我现在想要写一个工具,连接数据库,实现增删改查功能,恐怕很多程序员会回忆起最初学习Java的时候,那堪称ugly的JDBC模板代码了吧。我本身是一个DBA,因为公司安排,写过很多JDBC代码,深刻的感觉到这种代码实在浪费时间,后来接触了JPA框架,感觉非常好。下面就用Spring Boot来实现一个数据库的增删改查功能。

2. Spring Boot实战JPA

数据库我会使用H2,这种嵌入式的数据库最适合在家学习的时候使用,很小,支持最基本的数据库操作,只要不涉及到太深刻的内容,感觉和MySQL差不多。至于如何在本机上启动一个H2 Server,就不在这里描述了。

首先呢,打开IDEA,遵循下面的顺序:

new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。

上图是我选择的需要的组件。

既然是JPA,那么我们首先要定义一个Entity,我的表叫做Demo,那么Entity也就是叫做Demo:

package com.example.springwithjdbc.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
@Data
public class Demo {
@Id
@GeneratedValue
private int id; private String uname;
}

注解解释:

  1. @Entity:表示这个类是个Entity;
  2. @Data:这是lombok提供的功能,这个注解加上之后,就不需要写getter和setter了,也不用写toString方法,都会自动生成;
  3. @Id:表示这个是主键;
  4. @GeneratedValue:表示采用自增

接下来,需要经典的DAO层出现了:

package com.example.springwithjdbc.dao;

import com.example.springwithjdbc.entity.Demo;
import org.springframework.data.jpa.repository.JpaRepository; public interface DemoDao extends JpaRepository<Demo, Integer> { }

DAO层只是定义了一个interface,没有进行任何实现,其实也不需要进行任何具体的实现,注意继承的JpaRepository,它帮我们做了很多需要我们原先手动编码的工作。

接下来就是经典的Service层了,Service即业务层:

package com.example.springwithjdbc.service;

import com.example.springwithjdbc.entity.Demo;

import java.util.List;

public interface IDemoService {
Demo add(Demo demo); Demo findById(int id); List<Demo> findAll();
}

以上代码是service的接口,接下来编写具体的实现:

package com.example.springwithjdbc.service;

import com.example.springwithjdbc.dao.DemoDao;
import com.example.springwithjdbc.entity.Demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class DemoService implements IDemoService {
@Autowired
private DemoDao demoDao; @Override
public Demo add(Demo demo) {
return demoDao.save(demo);
} @Override
public Demo findById(int id) {
return demoDao.findById(id).get();
} @Override
public List<Demo> findAll() {
return demoDao.findAll();
}
}

注解解释:

  1. @Service:表示这是一个Service;

  2. @Autowired:将DemoDao注入。

接下来,我们需要一个Controller来实现REST接口:

package com.example.springwithjdbc.controller;

import com.example.springwithjdbc.entity.Demo;
import com.example.springwithjdbc.service.IDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
@RequestMapping("/")
public class DemoRestController {
@Autowired
IDemoService demoService; @PostMapping("/save")
public String save(@RequestParam(name = "name")String name) {
Demo demo = new Demo();
demo.setUname(name);
return demoService.add(demo).toString();
} @GetMapping("/find")
public String findById(@RequestParam(name = "id")int id) {
return demoService.findById(id).toString();
} @GetMapping("/list")
public String findAll() {
List<Demo> demoList = demoService.findAll();
return demoList.toString();
}
}

最后,配置application.yml,配置数据源:

spring:
datasource:
url: jdbc:h2:tcp://localhost/~/code/h2/bin/demo
username: admin
password: admin
jpa:
show-sql: true

启动这个工程即可,接下来就可以用浏览器或者postman进行测试了,这是我用postman进行的测试,首先发送一个POST请求,写入一条数据:

下面我们查询这条数据:

多写几条数据以后,调用list接口:

到这里,我们已经成功的编写了一段基于Spring Boot的JPA代码,实现了简单的新增和查询功能。比我之前写的JDBC代码不知道简单到哪里去了,而且也更加的优雅了,再也没有那么多复杂的让人难以看懂的xml配置了。

Spring甚至贴心到做了一个网站专门生成工程骨架。

3. 小结

我最近在学习微服务,要学习微服务绕不开的就是Spring Cloud,而Spring Cloud离不开Spring Boot。因此首先了解Spring Boot是很有必要的。

这是我的笔记整理出来的第一篇,希望能够帮助到其他和我一样在学习微服务,学习Spring Cloud,学习Spring Boot的人。

Spring Cloud学习笔记--Spring Boot初次搭建的更多相关文章

  1. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  2. Spring Cloud 学习笔记 (一)-- Eureka 服务器

    开局一张图,截取了本人学习资料中的一张图,很好地展示了Eureka的架构. Eureka服务器 管理服务的作用.细分为服务注册,服务发现. 所有的客户端在Eureka服务器上注册服务,再从Eureka ...

  3. Spring Cloud学习笔记-010

    分布式配置中心:Spring Cloud Config Spring Cloud Config是Spring Cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外 ...

  4. Spring Cloud 学习笔记(一)——入门、特征、配置

    [TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...

  5. Spring Cloud学习笔记-006

    服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...

  6. Spring Cloud学习笔记【九】配置中心Spring Cloud Config

    Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...

  7. Spring Cloud学习笔记【一】Eureka服务注册与发现

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  8. Spring Cloud学习笔记-007

    声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...

  9. Spring Cloud学习笔记之微服务架构

    目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务     微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的.     其中每个小型服务都运行在自己的进程中,并经常采 ...

随机推荐

  1. .Net Core Web应用发布至IIS后报“An error occurred while starting the application”错误

    An error occurred while starting the application. .NET Core X64 v4.1.1.0    |   Microsoft.AspNetCore ...

  2. SpringCloud实现集群和负载均衡

    Spring cloud是一个基于Spring Boot实现的服务治理工具包,在微服务架构中用于管理和协调服务的. 组成部分 spingcloud的五大神兽 服务发现——Netflix Eureka ...

  3. Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))

    Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...

  4. 23_pikle/shevel/json

    一.序列化       存储数据或者传输数据时,需要把对象进行处理,把对象处理成方便存储和传输的数据格式.不同的序列化,结果也不同.     序列化方式:         (1) pickle 可以将 ...

  5. mongodb批量更新某个字段

    查询出hospitalName是xx医院和openId以2开头的所有记录,并且更新my_booking表中的payType为1. db.getCollection('my_booking').find ...

  6. Swift5 语言参考(五) 语句

    在Swift中,有三种语句:简单语句,编译器控制语句和控制流语句.简单语句是最常见的,由表达式或声明组成.编译器控制语句允许程序更改编译器行为的各个方面,并包括条件编译块和行控制语句. 控制流语句用于 ...

  7. 【hyperscan】示例解读 pcapscan

    示例位置: <hyperscan source>/examples/pcapscan.cc参考:http://01org.github.io/hyperscan/dev-reference ...

  8. iOS-xcconfig环境变量那些事(配置环境的配置)

    前言 在配置宏定义参数时,会发现一个问题,在需要临时修改或者测试一些数据时,修改宏,如果不修改,就多写一个,注释掉原来的,然后测试后,再换回来,当然了,如果一两个宏,可以这样,但是,如果每次改的比较多 ...

  9. 【原创】关于程序卸载的一个Bug

    今天解决了一个问题,程序安装目录下的某个文件不能被卸载,干净环境下不能重现,某些计算机可以重现. 解决: 这个问题里有两个文件不能被卸载 1.由程序生成的文件,如日志,即不是通过安装包安装的文件在卸载 ...

  10. oc中类的实例化及方法调用

    上一篇我们讲了oop和类的创建,上一篇的重点我们回顾一下 类 对象 实例 方法 接口 这一篇我们来实现类的实例化,调用类中的公共参数和方法:类的实现在.m文件中,以下是实现代码: // // HuiT ...