Spring Cloud学习笔记--Spring Boot初次搭建
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;
}
注解解释:
- @Entity:表示这个类是个Entity;
- @Data:这是lombok提供的功能,这个注解加上之后,就不需要写getter和setter了,也不用写toString方法,都会自动生成;
- @Id:表示这个是主键;
- @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();
}
}
注解解释:
@Service:表示这是一个Service;
@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初次搭建的更多相关文章
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- Spring Cloud 学习笔记 (一)-- Eureka 服务器
开局一张图,截取了本人学习资料中的一张图,很好地展示了Eureka的架构. Eureka服务器 管理服务的作用.细分为服务注册,服务发现. 所有的客户端在Eureka服务器上注册服务,再从Eureka ...
- Spring Cloud学习笔记-010
分布式配置中心:Spring Cloud Config Spring Cloud Config是Spring Cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外 ...
- Spring Cloud 学习笔记(一)——入门、特征、配置
[TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...
- Spring Cloud学习笔记-006
服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...
- Spring Cloud学习笔记【九】配置中心Spring Cloud Config
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...
- Spring Cloud学习笔记【一】Eureka服务注册与发现
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
- Spring Cloud学习笔记-007
声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...
- Spring Cloud学习笔记之微服务架构
目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务 微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的. 其中每个小型服务都运行在自己的进程中,并经常采 ...
随机推荐
- this与$(this)对象
this与$(this)对象.前者是Javascript对象,而后者是jQuery是对象.两者分清楚,它们只能使用自己的方法.Javascript对象使用Javascript的方法,jQuery对象使 ...
- Spring Framework 4.3.22.RELEASE Reference文档目录
<Spring Framework Reference Documentation 4.3.22.RELEASE> https://docs.spring.io/spring/docs/4 ...
- 01-Python的基础知识3
- 数字 - 数字常量: - 整型: - 概念: - 指代平常数学上的整数常量.Python中整型指代int类型. - 基本运算: - 可以执行平常的+,-,*,/ ,%以及其他操作 假设a=15,b ...
- 修改git全局初始化的用户名
今天我把文件上传到码云的时候出现 翻译的话就是 用户名不能多个值 这样的错误 所以想要解决这个错误的话非常简单 $ git config --global --replace-all user ...
- elasticsearch+logstash+redis+kibana 实时分析nginx日志
1. 部署环境 2. 架构拓扑 3. nginx安装 安装在192.168.176.128服务器上 这里安装就简单粗暴了直接yum安装nginx [root@manager ~]# yum -y in ...
- 刚破了潘金莲的身份信息(图片文字识别),win7、win10实测可用(免费下载)
刚破了潘金莲的身份信息(图片文字识别),win7.win10实测可用 效果如下: 证照,车牌.身份证.名片.营业执照 等图片文字均可识别 电脑版 本人出品 大小1.3MB 下载地址:https://p ...
- JDK8 - Function介绍
注:写这个文档只是为了方便加深记忆,加强理解,重点关注两个default方法中泛型[V]. JDK8作为一个还在维护阶段的长期版本,势必会在企业应用中占据相当大的市场份额,所以还是以JDK8作为例子的 ...
- django中url,静态文件,POST请求的配置 分类: Python 2015-06-01 17:00 789人阅读 评论(0) 收藏
平时使用的是pycharm,所以这篇文章主要也是使用pycharm默认创建的django项目为基础进行讲解.项目目录如下图: 1.URL的配置 当创建好项目后,运行项目就可以看到django默认的页面 ...
- How to set background image to a LinearLayout using Android-Universal-Image-Loader ? #594
You can do it by 2 ways: use loadImage(...) and set layout background in listener (ImageLoadingListe ...
- 走进javascript——被忽视的DOM方法和属性
isEqualNode() isEqualNode方法可以用来判断两个DOM节点是否相同,给我的第一感觉是没用,因为两个DOM的比较很容易让人想成是字符串的比较,心想直接用两个等号不就可以了吗,但马上 ...