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工程的更多相关文章

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

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

  2. SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程

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

  3. Springboot(一):使用Intellij中的Spring Initializr来快速构建Spring Boot工程

    使用Intellij中的Spring Initializr来快速构建Spring Boot工程 New---Project 可以看到图所示的创建功能窗口.其中Initial Service Url指向 ...

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

    本文将介绍嵌入的Intellij中的Spring Initializr工具,它同Web提供的创建功能一样,可以帮助我们快速的构建出一个基础的Spring Boot/Cloud工程. 1.菜单栏中选择F ...

  5. 只需五分钟-用Maven快速搭建Spring Cloud微服务

    Maven安装手册 1.准备安装包 安装包: apache-maven-3.5.4-bin.zip  (最好JDK 1.7及以上版本) 集成包: eclipse-maven3-plugin.zip 2 ...

  6. 十分钟快速创建 Spring Cloud 项目

    一般来说,Intelij IDEA 可以通过 Maven Archetype 来快速生成Maven项目,其实 IDEA 集成了 Spring 官方提供的 Spring Initializr,可以非常方 ...

  7. 在eclipse中使用maven构建spring cloud微服务

    使用eclipse中使用maven构建spring cloud微服务,springcloud通过maven构建项目.springcloud项目搭建. 工具/原料   eclipse maven spr ...

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

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

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

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

随机推荐

  1. 多线程16-SpinWait

        );             isCompleted = ));             isCompleted = );             isCompleted = true;    ...

  2. 教你在 IntelliJ IDEA 中使用 VIM!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 IdeaVim(下载)插件可以让你在IntelliJ IDEA中键盘敲的飞起. 安装 打开IDEA的设置,在Plugins里 ...

  3. sudo pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple ipython

    sudo pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple ipython

  4. neo4j 初探

    neo4j 初探 参考 转载:http://shomy.top/2018/06/08/neo4j-start/ 近期需要处理图数据,考察后打算使用neo4j, 相比其他一些图数据库,neo4j开源,跨 ...

  5. Tomcat启动慢的原因及解决方法

    Tomcat启动慢的原因及解决方法 在CentOS启动Tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是session引起的随机数问题导致的.Tocmat的Session ID ...

  6. CSS初识

    CSS:层叠样式表,控制网页数据样式显示,使得数据的表现和内容分离 CSS的引入方式 使用元素内嵌样式表:例<a style=”font-size:40px”></a>表示在a ...

  7. 计蒜客 蓝桥模拟 B.素数个数

    用 0,1,2,3⋯70,1,2,3 \cdots 70,1,2,3⋯7 这 888 个数组成的所有整数中,质数有多少个(每个数字必须用到且只能用一次). 提示:以 000 开始的数字是非法数字. 代 ...

  8. java 分页对象以及数据库分页查询

    import java.util.List; public class Pager<T> { private Integer pageSize; private Integer total ...

  9. hash详细介绍

    转发http://www.cnblogs.com/maybe2030/p/4719267.html (请转步这个网站,写得非常好)

  10. Linux学习-FTP服务

    一.FTP相关介绍 1.文本传输协议FTP FTP (File Transfer Protocol) 文件传输协议,是因特网中使用最广泛的文件传输协议: 基于C/S结构的双通道协议(数据和命令连接) ...