当前位置: 首页 > news >正文

Spring Cloud + MyBatis Plus + GraphQL 完整示例

Spring Cloud + MyBatis Plus + GraphQL 完整示例

  • 1、创建Spring Boot子项目
    • 1.1 配置POM,添加必要的依赖
    • 1.2 配置MyBatis-Plus
  • 2、集成GraphQL
    • 2.1 定义schema.graphqls
    • 2.2 添加GraphQL解析器
    • 2.3 配置schame文件配置
  • 3、访问测试
    • 3.1 查询测试(演示)
    • 3.2 添加测试(演示)
    • 3.3 修改测试(演示)
    • 3.4 删除测试(演示)

为了创建一个完整的Spring Cloud应用,结合MyBatis-Plus和GraphQL,构建一个简单的微服务项目。这个示例将包括以下几个部分:- 创建Spring Boot子项目。
- 配置MyBatis-Plus以进行数据库操作。
- 集成GraphQL以提供查询接口。
- 我们将假设有一个简单的实体类Person,并为其创建CRUD操作和GraphQL查询。

1、创建Spring Boot子项目

1.1 配置POM,添加必要的依赖

  • 首先,我们需要创建一个新的Spring Boot项目,并添加必要的依赖项。以下是Maven pom.xml配置文件的内容:
          <!--graphql--><dependency><groupId>com.graphql-java-kickstart</groupId><artifactId>graphql-spring-boot-starter</artifactId><version>11.0.0</version></dependency><dependency><groupId>com.graphql-java-kickstart</groupId><artifactId>graphiql-spring-boot-starter</artifactId><version>11.0.0</version></dependency><!--mybatisPlus&mysql--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.2.0</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.4.1</version></dependency>

1.2 配置MyBatis-Plus

  • 接下来,我们配置MyBatis-Plus来处理我们的数据访问层。首先,定义一个Person实体类:
package com.springboot.demo.domain;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;/*** 用户表** @author water* @version 1.0.0* @date 2024-12-04 17:45*/
@Data
@TableName("demo_person")
public class Person {private Long id;private String name;private int age;}
  • 然后,创建对应的Mapper接口:
package com.springboot.demo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springboot.demo.domain.Person;/*** @author water* @version 1.0.0* @date 2024-12-05 08:11*/
public interface PersonMapper extends BaseMapper<Person> {
}
  • 最后,在Spring Boot主类上启用MyBatis-Plus扫描器:
package com.springboot.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** springboot -demo** @Author: water* @Date: 2024/12/4 18:09* @Version : 1.0.0*/
@SpringBootApplication
@MapperScan("com.springboot.demo.mapper")
@EnableDiscoveryClient
@EnableFeignClients
public class SpringbootDemoApp {public static void main(String[] args) {SpringApplication.run(SpringbootDemoApp.class, args);}}
  • 同时,配置application.yml文件来设置数据库连接信息:
spring:profiles:active: clouddatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/spring-dmeo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=falseusername: demopassword: Spring@demo24hikari:maximum-pool-size: 10minimum-idle: 1idle-timeout: 30000max-lifetime: 1800000transaction:default-timeout: 30graphql:graphiql:enabled: true#mybatis-plus
mybatis-plus:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.springboot.demo.**.domainconfiguration:map-underscore-to-camel-case: true# 默认日志输出: org.apache.ibatis.logging.slf4j.Slf4jImpl# 更详细的日志输出 会有性能损耗: org.apache.ibatis.logging.stdout.StdOutImpllog-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl #关闭日志记录 (可单纯使用 p6spy 分析)cache-enabled: falseauto-mapping-behavior: fullglobal-config:banner: falsedb-config:id-type: assign_id# 默认1删除logic-delete-value: 1# 正常0logic-not-delete-value: 0
#  日志
logging:level:com:example: debugorg:springframework:security: debugsecurity.access: debugsecurity.web: debugsecurity.web.access: debugweb: debugmanagement:endpoints:web:exposure:include: health,info

2、集成GraphQL

2.1 定义schema.graphqls

接下来,我们集成GraphQL以暴露API供客户端查询使用。首先,创建一个GraphQL schema文件schema.graphqls:
# src/main/resources/schema.graphqls
type Query {users: [Person]personById(id: ID!): Person
}type Mutation {addUser(name: String!, age: Int!): PersonupdateUser(id: ID!, name: String, age: Int): PersondeleteUser(id: ID!): Boolean
}type Person {id: ID!name: String!age: Int!
}

2.2 添加GraphQL解析器

  • 然后,为这些GraphQL查询和变更创建解析器:
package com.springboot.demo.resolver;import com.springboot.demo.domain.Person;
import com.springboot.demo.mapper.PersonMapper;
import graphql.kickstart.tools.GraphQLMutationResolver;
import graphql.kickstart.tools.GraphQLQueryResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.List;/*** GraphQL 查询创建解析器。** @author water* @version 1.0.0* @date 2024-12-05 11:29*/
@Component
public class UserQuery implements GraphQLQueryResolver, GraphQLMutationResolver {@Autowiredprivate PersonMapper personMapper;public List<Person> users() {return personMapper.selectList(null);}public Person personById(String id) {personMapper.selectById(id);return personMapper.selectById(id);}public Person addUser(String name, int age) {Person user = new Person();user.setName(name);user.setAge(age);personMapper.insert(user);return user;}public Person updateUser(Long id, String name, Integer age) {Person user = personMapper.selectById(id);if (user != null) {if (name != null) {user.setName(name);}if (age != null) {user.setAge(age);}personMapper.updateById(user);}return user;}public boolean deleteUser(Long id) {return personMapper.deleteById(id) > 0;}}

2.3 配置schame文件配置

  • 最后,配置GraphQL工具包以加载schema文件和其他相关配置:
package com.springboot.demo.config;import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;/*** @author water* @version 1.0.0* @date 2024-12-05 14:30*/
@Configuration
public class GraphQLConfig {@Beanpublic Resource graphQLSchemaFile() {return new ClassPathResource("schema.graphqls");}@Beanpublic TypeDefinitionRegistry typeDefinitionRegistry(Resource resource) throws Exception {SchemaParser parser = new SchemaParser();return parser.parse(resource.getInputStream());}
}

至此,我们就完成了一个整合了Spring Cloud、MyBatis-Plus和GraphQL的简单示例项目的搭建。 您可以启动应用程序并在浏览器中访问http://localhost:8080/graphiql来测试GraphQL API。
在这里插入图片描述

3、访问测试

  • 测试请求的脚本
# Welcome to GraphiQL
#
# GraphiQL is an in-browser tool for writing, validating, and
# testing GraphQL queries.
#
# Type queries into this side of the screen, and you will see intelligent
# typeaheads aware of the current GraphQL type schema and live syntax and
# validation errors highlighted within the text.
#
# GraphQL queries typically start with a "{" character. Lines that start
# with a # are ignored.
#
# An example GraphQL query might look like:
#
#     {
#       field(arg: "value") {
#         subField
#       }
#     }
#
# Keyboard shortcuts:
#
#  Prettify Query:  Shift-Ctrl-P (or press the prettify button above)
#
#     Merge Query:  Shift-Ctrl-M (or press the merge button above)
#
#       Run Query:  Ctrl-Enter (or press the play button above)
#
#   Auto Complete:  Ctrl-Space (or just start typing)
#
query select {personById(id:"1"){idnameage}}
query selectList {users{idnameage}}
mutation addUser{addUser(name:"王麻子", age:22){idnameage}
}mutation updateUser{updateUser(id:1864582448746012673,name:"钢蛋", age:31){idnameage}
}mutation updateUserById{updateUser(id:1864569571339378690,name:"赵六六"){idname}
}mutation deleteUser{deleteUser(id:1864582448746012673)
}

3.1 查询测试(演示)

  • 根据Id查询
    在这里插入图片描述
  • 查询所有
    在这里插入图片描述

3.2 添加测试(演示)

在这里插入图片描述

3.3 修改测试(演示)

在这里插入图片描述

3.4 删除测试(演示)

在这里插入图片描述

相关文章:

Spring Cloud + MyBatis Plus + GraphQL 完整示例

Spring Cloud MyBatis Plus GraphQL 完整示例 1、创建Spring Boot子项目1.1 配置POM&#xff0c;添加必要的依赖1.2 配置MyBatis-Plus 2、集成GraphQL2.1 定义schema.graphqls2.2 添加GraphQL解析器2.3 配置schame文件配置 3、访问测试3.1 查询测试&#xff08;演示&#xff…...

uni-app简洁的移动端登录注册界面

非常简洁的登录、注册界面模板&#xff0c;使用uni-app编写&#xff0c;直接复制粘贴即可&#xff0c;无任何引用&#xff0c;全部公开。 废话不多说&#xff0c;代码如下&#xff1a; login.vue文件 <template><view class"content"><view class&quo…...

LongVU:用于长视频语言理解的空间时间自适应压缩

晚上闲暇时间看到一种用于长视频语言理解的空间时间自适应压缩机制的研究工作LongVU&#xff0c;主要内容包括&#xff1a; 背景与挑战&#xff1a;多模态大语言模型&#xff08;MLLMs&#xff09;在视频理解和分析方面取得了进展&#xff0c;但处理长视频仍受限于LLM的上下文长…...

Elasticsearch数据迁移(快照)

1. 数据条件 一台原始es服务器&#xff08;192.168.xx.xx&#xff09;&#xff0c;数据迁移后的目标服务器&#xff08;10.2.xx.xx&#xff09;。 2台服务器所处环境&#xff1a; centos7操作系统&#xff0c; elasticsearch-7.3.0。 2. 为原始es服务器数据创建快照 修改elas…...

Linux Cgroup学习笔记

文章目录 Cgroup(Control Group)引言简介Cgroup v1通用接口文件blkio子系统cpu子系统cpuacct子系统cpuset子系统devices子系统freezer子系统hugetlb子系统memory子系统net_cls子系统net_prio子系统perf_event子系统pids子系统misc子系统 Cgroup V2基础操作组织进程和线程popula…...

百问FB显示开发图像处理 - PNG图像处理

2.3 PNG图像处理 2.3.1 PNG文件格式和libpng编译 ​ 跟JPEG文件格式一样&#xff0c;PNG也是一种使用了算法压缩后的图像格式&#xff0c;与JPEG不同&#xff0c;PNG使用从LZ77派生的无损数据压缩算法。对于PNG文件格式&#xff0c;也有相应的开源工具libpng。 libpng库可从…...

【JavaWeb后端学习笔记】MySQL多表查询(内连接、外连接、子查询)

MySQL 多表查询 1、连接查询1.1 内连接1.2 外连接 2、子查询2.1 标量子查询2.2 列子查询2.3 行子查询2.4 表子查询 3、多表查询案例 多表查询有两大类&#xff1a;连接查询和子查询。 连接查询又分为隐式/显式内连接和左/右外连接。 子查询又分为标量子查询、列子查询、行子查询…...

RocketMQ 过滤消息 基于tag过滤和SQL过滤

RocketMQ 过滤消息分为两种&#xff0c;一种tag过滤&#xff0c;另外一种是复杂的sql过滤。 tag过滤 首先创建producer然后启动&#xff0c;在这里创建了字符串的数组tags。字符串数组里面放置了多个字符串&#xff0c;然后去发送15条消息。 15条消息随着i的增长&#xff0c;…...

element-ui 基本样式的一些更改【持续更新】

1、 去除el-tabs的底部灰色横线 ::v-deep .el-tabs__nav-wrap::after {height: 0px;}2、el-table设置表头颜色 <el-table:data"tableData":header-cell-style"{background:#F7F8FA,color:#4E5869}"><el-table-columnlabel"序号"type&qu…...

element-ui radio和checkbox禁用时不置灰还是原来不禁用时的样式

把要紧用的内容加上一个class"notEdit-page" z注意要在style里面写不能加上scoped /*//checkBox自定义禁用样式*//*//checkBox自定义禁用样式*/ .notEdit-page.el-checkbox__input.is-disabled.is-checked.el-checkbox__inner::after {border-color: #fff; } .notEdi…...

第一部分:基础知识 6. 函数 --[MySQL轻松入门教程]

MySQL 提供了丰富的内置函数&#xff0c;涵盖了字符串处理、数值计算、日期时间操作、聚合分析以及控制流等多个方面。这些函数可以帮助用户更高效地进行数据查询和处理。 1.字符串函数 MySQL 提供了丰富的字符串函数来帮助用户处理和操作字符串数据。下面是一些常用的 MySQL…...

【蓝桥杯每日一题】扫雷

扫雷 知识点 2024-12-3 蓝桥杯每日一题 扫雷 dfs &#xff08;bfs也是可行的&#xff09; 题目大意 在一个二维平面上放置这N个炸雷&#xff0c;每个炸雷的信息有$(x_i,y_i,r_i) $&#xff0c;前两个是坐标信息&#xff0c;第三个是爆炸半径。然后会输入M个排雷火箭&#xff0…...

【算法】棋盘覆盖问题源代码及精简版

目录 一、题目 二、样例 三、示例代码 四、精简代码 五、总结 对于棋盘覆盖问题的解答和优化。 一、题目 输入格式&#xff1a; 第一行&#xff0c;一个整数n&#xff08;棋盘n*n&#xff0c;n确保是2的幂次&#xff0c;n<64&#xff09; 第二行&#xff0c;两个整数…...

Django的介绍

Django是一个高级的Python Web框架&#xff0c;它鼓励快速开发和干净、实用的设计。Django遵循MVC设计模式&#xff0c;即模型&#xff08;Model&#xff09;、视图&#xff08;View&#xff09;和控制器&#xff08;Controller&#xff09;&#xff0c;并提供了一个即时可用的…...

【Spring工具插件】lombok使用和EditStarter插件

阿华代码&#xff0c;不是逆风&#xff0c;就是我疯 你们的点赞收藏是我前进最大的动力&#xff01;&#xff01; 希望本文内容能够帮助到你&#xff01;&#xff01; 目录 引入 一&#xff1a;lombok介绍 1&#xff1a;引入依赖 2&#xff1a;使用 3&#xff1a;原理 4&…...

掌控时间,成就更好的自己

在个人成长的道路上&#xff0c;时间管理是至关重要的一环。有效的时间管理能够让我们更加高效地完成任务&#xff0c;实现自己的目标&#xff0c;不断提升自我。 时间对每个人都是公平的&#xff0c;一天只有 24 小时。然而&#xff0c;为什么有些人能够在有限的时间里做出卓…...

Ruby On Rails 笔记2——表的基本知识

Active Record Basics — Ruby on Rails Guides Active Record Migrations — Ruby on Rails Guides 原文链接自取 1.Active Record是什么&#xff1f; Active Record是MVC模式中M的一部分&#xff0c;是负责展示数据和业务逻辑的一层&#xff0c;可以帮助你创建和使用Ruby…...

【AI系统】EfficientNet 系列

EfficientNet 系列 本文主要介绍 EffiicientNet 系列&#xff0c;在之前的文章中&#xff0c;一般都是单独增加图像分辨率或增加网络深度或单独增加网络的宽度&#xff0c;来提高网络的准确率。而在 EfficientNet 系列论文中&#xff0c;会介绍使用网络搜索技术(NAS)去同时探索…...

【Python小白|Python内置函数学习2】Python有哪些内置函数?不需要导入任何模块就可以直接使用的!现在用Python写代码的人还多吗?

【Python小白|Python内置函数学习2】Python有哪些内置函数&#xff1f;不需要导入任何模块就可以直接使用的&#xff01;现在用Python写代码的人还多吗&#xff1f; 【Python小白|Python内置函数学习2】Python有哪些内置函数&#xff1f;不需要导入任何模块就可以直接使用的&a…...

蓝桥杯分治

P1226 【模板】快速幂 题目描述 给你三个整数 &#x1d44e;,&#x1d44f;,&#x1d45d;a,b,p&#xff0c;求 &#x1d44e;&#x1d44f; mod &#x1d45d;abmodp。 输入格式 输入只有一行三个整数&#xff0c;分别代表 &#x1d44e;,&#x1d44f;,&#x1d45d;a,b,p。…...

Cursor实现用excel数据填充word模版的方法

cursor主页&#xff1a;https://www.cursor.com/ 任务目标&#xff1a;把excel格式的数据里的单元格&#xff0c;按照某一个固定模版填充到word中 文章目录 注意事项逐步生成程序1. 确定格式2. 调试程序 注意事项 直接给一个excel文件和最终呈现的word文件的示例&#xff0c;…...

golang循环变量捕获问题​​

在 Go 语言中&#xff0c;当在循环中启动协程&#xff08;goroutine&#xff09;时&#xff0c;如果在协程闭包中直接引用循环变量&#xff0c;可能会遇到一个常见的陷阱 - ​​循环变量捕获问题​​。让我详细解释一下&#xff1a; 问题背景 看这个代码片段&#xff1a; fo…...

在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module

1、为什么要修改 CONNECT 报文&#xff1f; 多租户隔离&#xff1a;自动为接入设备追加租户前缀&#xff0c;后端按 ClientID 拆分队列。零代码鉴权&#xff1a;将入站用户名替换为 OAuth Access-Token&#xff0c;后端 Broker 统一校验。灰度发布&#xff1a;根据 IP/地理位写…...

剑指offer20_链表中环的入口节点

链表中环的入口节点 给定一个链表&#xff0c;若其中包含环&#xff0c;则输出环的入口节点。 若其中不包含环&#xff0c;则输出null。 数据范围 节点 val 值取值范围 [ 1 , 1000 ] [1,1000] [1,1000]。 节点 val 值各不相同。 链表长度 [ 0 , 500 ] [0,500] [0,500]。 …...

Frozen-Flask :将 Flask 应用“冻结”为静态文件

Frozen-Flask 是一个用于将 Flask 应用“冻结”为静态文件的 Python 扩展。它的核心用途是&#xff1a;将一个 Flask Web 应用生成成纯静态 HTML 文件&#xff0c;从而可以部署到静态网站托管服务上&#xff0c;如 GitHub Pages、Netlify 或任何支持静态文件的网站服务器。 &am…...

如何将联系人从 iPhone 转移到 Android

从 iPhone 换到 Android 手机时&#xff0c;你可能需要保留重要的数据&#xff0c;例如通讯录。好在&#xff0c;将通讯录从 iPhone 转移到 Android 手机非常简单&#xff0c;你可以从本文中学习 6 种可靠的方法&#xff0c;确保随时保持连接&#xff0c;不错过任何信息。 第 1…...

【服务器压力测试】本地PC电脑作为服务器运行时出现卡顿和资源紧张(Windows/Linux)

要让本地PC电脑作为服务器运行时出现卡顿和资源紧张的情况&#xff0c;可以通过以下几种方式模拟或触发&#xff1a; 1. 增加CPU负载 运行大量计算密集型任务&#xff0c;例如&#xff1a; 使用多线程循环执行复杂计算&#xff08;如数学运算、加密解密等&#xff09;。运行图…...

【C语言练习】080. 使用C语言实现简单的数据库操作

080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...

Fabric V2.5 通用溯源系统——增加图片上传与下载功能

fabric-trace项目在发布一年后,部署量已突破1000次,为支持更多场景,现新增支持图片信息上链,本文对图片上传、下载功能代码进行梳理,包含智能合约、后端、前端部分。 一、智能合约修改 为了增加图片信息上链溯源,需要对底层数据结构进行修改,在此对智能合约中的农产品数…...

搭建DNS域名解析服务器(正向解析资源文件)

正向解析资源文件 1&#xff09;准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2&#xff09;服务端安装软件&#xff1a;bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...