快速构建Spring Cloud工程
spring cloud简介
spring cloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解
在之前的所有Spring Boot相关博文中,都会涉及Spring Boot工程的创建。而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也可以通过SPRING INITIALIZR页面工具来创建,相信每位读者都有自己最喜欢和最为熟练的创建方式。
本文我们将介绍嵌入的Intellij中的Spring Initializr工具,它同Web提供的创建功能一样,可以帮助我们快速的构建出一个基础的Spring Cloud工程。
创建工程
第一步:菜单栏中选择File=New=Project..,我们可以看到如下图所示的创建功能窗口。其中Initial Service Url指向的地址就是Spring官方提供的Spring Initializr工具地址,所以这里创建的工程实际上也是基于它的Web工具来实现的。




项目结构:

我测试了连接数据库查询数据,不多说先配置依赖:

pom.xml:文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies> <!-- 这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <!--支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--S支持全栈式Web开发,包括Tomcat和spring-webmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--数据库连接 -->
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.4.1</version>
</dependency> <!--支持JPA(Java Persistence API. ,包括spring-data-jpa、spring-orm、Hibernate。-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.3.RELEASE</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
application.properties:配置
server.port=3333 # 数据源配置
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/stu
spring.datasource.username=root
spring.datasource.password=666666 spring.jpa.hibernate.ddl-auto=update # 如果是mariadb,需要配置这个
spring.database-platform=org.hibernate.dialect.MariaDB10Dialect
controller层:
package com.example.demo.controller; import com.example.demo.entity.News;
import com.example.demo.service.Impl.NewsServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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
public class NewsController{ @Autowired
private NewsServiceImp newsServiceImp; //查询
@RequestMapping(value = "/listNews", method = RequestMethod.GET)
public ResponseEntity getNews() {
List<News> news = newsServiceImp.listAll();
return ResponseEntity.ok(news);
} }
dao层:
package com.example.demo.dao; import com.example.demo.entity.News;
import org.springframework.data.jpa.repository.JpaRepository; public interface NewsMapper extends JpaRepository<News,Integer> {
}
entity层:
package com.example.demo.entity; import javax.persistence.*; @Entity
@Table(name = "news") //表名
public class News {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //标明该字段是自动增长
private int id;
private String title;
private String body; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getBody() {
return body;
} public void setBody(String body) {
this.body = body;
}
}
service层 里面包含的一个Impl 的包,下面放的是实现接口类:
package com.example.demo.service;
import com.example.demo.entity.News;
import java.util.List;
public interface NewsService {
List<News> listAll();
void add (News news);
void del (int id);
void update(News news);
}
Impl下:
package com.example.demo.service.Impl; import com.example.demo.dao.NewsMapper;
import com.example.demo.entity.News;
import com.example.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class NewsServiceImp implements NewsService { @Autowired
private NewsMapper newsMapper; @Override
public List<News> listAll() {
return newsMapper.findAll();
} @Override
public void add(News news) {
newsMapper.save(news); } @Override
public void del(int id) {
newsMapper.deleteById(id);
} @Override
public void update(News news) {
newsMapper.save(news);
}
}
下面启动看效果:

数据库已经查询出来

源码地址: https://github.com/nongzihong/spring_cloud
快速构建Spring Cloud工程的更多相关文章
- SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- Springboot(一):使用Intellij中的Spring Initializr来快速构建Spring Boot工程
使用Intellij中的Spring Initializr来快速构建Spring Boot工程 New---Project 可以看到图所示的创建功能窗口.其中Initial Service Url指向 ...
- 使用Intellij中的Spring Initializr来快速构建Spring Boot工程
本文将介绍嵌入的Intellij中的Spring Initializr工具,它同Web提供的创建功能一样,可以帮助我们快速的构建出一个基础的Spring Boot/Cloud工程. 1.菜单栏中选择F ...
- 只需五分钟-用Maven快速搭建Spring Cloud微服务
Maven安装手册 1.准备安装包 安装包: apache-maven-3.5.4-bin.zip (最好JDK 1.7及以上版本) 集成包: eclipse-maven3-plugin.zip 2 ...
- 十分钟快速创建 Spring Cloud 项目
一般来说,Intelij IDEA 可以通过 Maven Archetype 来快速生成Maven项目,其实 IDEA 集成了 Spring 官方提供的 Spring Initializr,可以非常方 ...
- 在eclipse中使用maven构建spring cloud微服务
使用eclipse中使用maven构建spring cloud微服务,springcloud通过maven构建项目.springcloud项目搭建. 工具/原料 eclipse maven spr ...
- 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程(十五)
在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...
- 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...
随机推荐
- Linux /dev/shm
/dev/shm是Linux下的一个目录,/dev/shm目录不在磁盘上,而是在内存中,因此使用Linux /dev/shm的效率非常高,直接写进内存 可以通过两个脚本验证Linux /dev/shm ...
- Java——LinkedList使用Demo
package list; import java.util.Iterator; import java.util.LinkedList; public class LinkedListDemo { ...
- SQL Server之索引解析(二)
1.堆表 堆表通过IAM连接一起,查询时全表扫描. 1.1 非聚集索引 结构 叶子节点数据结构:行数据结构+Rid(8字节) 中间节点数据结构: (非聚集非唯一索引)行数据结构+Page(4)+2+ ...
- swtich和case语句中,定义变量要加花括号
转自: http://blog.chinaunix.net/uid-27103408-id-3340702.html http://www.xuebuyuan.com/2070170.html swi ...
- Python 入门之 Python三大器 之 迭代器
Python 入门之 Python三大器 之 迭代器 1.迭代器 (1)可迭代对象: <1> 只要具有__ iter __()方法就是一个可迭代对象 (我们可以通过dir()方法去判断一个 ...
- Rtools的安装
long long ago,我在http://cran.r-project.org/bin/windows/Rtools/ 下载到了Rtools30.exe……这是一个神奇的工具…… 我Rto ...
- kNN分类算法实现
kNN算法就是计算每个点到其他所有点的距离,选出距离最小的k个点.在这k个点里,哪个类别的最多,就把待分类的点归到哪类. kNN.py: from numpy import * import oper ...
- CPU与GPU,我们应该使用哪个?
CPU与GPU,我们应该使用哪个? CPU与GPU CPU即中央处理器,GPU即图形处理器. 两者的相同之处:两者都有总线和外界联系,有自己的缓存体系,以及数字和逻辑运算单元 两者的区别之处:在于存在 ...
- Mongo--02 命令介绍
目录 Mongo工具 1. 查看指令 2.插入命令 3.查询命令 4.更新数据 5.索引 5.删除 6.mongo命令介绍 7.创建用户和角色 Mongo工具 1. 查看指令 test:登录时默认存在 ...
- 【转】/etc/inittab文件详解
转自:https://www.cnblogs.com/ricks/p/10020886.html Linux在完成核内引导(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就 ...