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=666666spring.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{@Autowiredprivate 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 {@Autowiredprivate NewsMapper newsMapper;@Overridepublic List<News> listAll() {return newsMapper.findAll();}@Overridepublic void add(News news) {newsMapper.save(news);}@Overridepublic void del(int id) {newsMapper.deleteById(id);}@Overridepublic void update(News news) {newsMapper.save(news);}
}

下面启动看效果:

数据库已经查询出来

源码地址: https://github.com/nongzihong/spring_cloud

转载于:https://www.cnblogs.com/nongzihong/p/10576132.html

快速构建Spring Cloud工程相关推荐

  1. SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

  2. 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程(十五)

    在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...

  3. 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

    在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...

  4. Spring Boot教程(十五)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程...

    在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...

  5. start.aliyun.com 正式上线!极速构建 Spring Cloud 应用

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 Photo @ Alibaba Initializr 官网 文  ...

  6. 使用 Spring Boot 快速构建 Spring 框架应用

    https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/index.html Spring 框架对于很多 Java 开发人员来说都不陌生 ...

  7. 使用 Spring Boot 快速构建 Spring 框架应用--转

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...

  8. 快速了解Spring Cloud

    Spring Cloud 是什么? Spring Cloud 虽然带有Cloud字样, 但其不是云解决方案.而是基于Spring Boot 的微服务开发框架,是用于构建分布式系统的工具集. Sprin ...

  9. 【开源项目】SpringCloud 快速构建项目脚手架工程(持续更新)

    一.项目地址 https://gitee.com/smile-coding/springcloud-quick-start 二.项目说明 本项目旨在提供一个快速构建微服务的脚手架工程,不掺杂任何的复杂 ...

最新文章

  1. sql 纵向求和_sql列统计求和
  2. 服务器保存所有用户的操作指令(history)
  3. powerbi 线性回归_Power BI二月新增图表及课程福利
  4. 【深度学习】基于web端和C++的两种深度学习模型部署方式
  5. 四十、Linux和ViM的使用
  6. attr和prop的区别
  7. 防御CSRF、XSS和SQL注入***
  8. Unity渲染管线-百人计划笔记
  9. windows中配置ant环境变量
  10. 电脑朋友圈,PC玩朋友圈,真的来了
  11. 一个女留学生在美国的七年
  12. 多帧图像增强 matlab,MATLAB中图像增强技术的实现
  13. prometheus 阿里云短信告警
  14. 浏览器出现无法访问此页面的提示的解决办法
  15. 软件设计师-设计模式
  16. 计算机网络题库---第三章数据链路层
  17. 【新技研】高通全新骁龙Krait 300/400处理器架构浅析
  18. 计算机专业及软件开发推荐书籍
  19. android端蓝牙控制单片机,android 蓝牙控制继电器——单片机控制端
  20. 中国地图 echarts china.js 无乱码

热门文章

  1. 男人最不该做的7件事
  2. C++自动生成的成员函数
  3. CssGaga 快速上手指南
  4. 开源图形库 FreeImage
  5. HDU2673-shǎ崽(水题)
  6. C# Windows CE使用小技巧实例
  7. Intellij IDEA单元测试提示Test events were not received
  8. 软件体系架构模式之三微内核体系架构
  9. Gridview][UpdateCommand的写法要点]
  10. OpenCV代码提取:cvtColor函数的实现