做网站去哪里接单,作网站流程,手机怎么编辑网页,wordpress 子主题1 安装插件
我这边使用的是IDEA#xff0c;需要先按照Graphql插件#xff0c;步骤如下#xff1a;
#xff08;1#xff09;打开插件管理
在IDEA中#xff0c;打开主菜单#xff0c;选择 File - Settings (或者使用快捷键 Ctrl Alt S …1 安装插件
我这边使用的是IDEA需要先按照Graphql插件步骤如下
1打开插件管理
在IDEA中打开主菜单选择 File - Settings (或者使用快捷键 Ctrl Alt S 或 Cmd ,)然后在弹出的对话框中选择 Plugins。
2搜索GraphQL插件
在插件管理器中你会看到一个搜索框。在搜索框中输入 GraphQL 或者其他相关的关键词然后按下回车键或者点击搜索图标。
3安装插件 4重启IDEA
安装完成以后重启IDEA 2 新建数据库
脚本如下: CREATE DATABASE IF NOT EXISTS BOOK_API_DATA;
USE BOOK_API_DATA;CREATE TABLE IF NOT EXISTS Book (id int(20) NOT NULL AUTO_INCREMENT,name varchar(255) DEFAULT NULL,pageCount varchar(255) DEFAULT NULL,PRIMARY KEY (id),UNIQUE KEY Index_name (name)) ENGINEInnoDB AUTO_INCREMENT235 DEFAULT CHARSETutf8;CREATE TABLE Author (id INT(20) NOT NULL AUTO_INCREMENT,firstName VARCHAR(255) NULL DEFAULT NULL COLLATE utf8_general_ci,lastName VARCHAR(255) NULL DEFAULT NULL COLLATE utf8_general_ci,bookId INT(20) NULL DEFAULT NULL,PRIMARY KEY (id) USING BTREE,UNIQUE INDEX Index_name (firstName) USING BTREE,INDEX FK_Author_Book (bookId) USING BTREE,CONSTRAINT FK_Author_Book FOREIGN KEY (bookId) REFERENCES BOOK_API_DATA.Book (id) ON UPDATE CASCADE ON DELETE CASCADE
)COLLATEutf8_general_ci
ENGINEInnoDB
AUTO_INCREMENT6
;INSERT INTO Book (id, name, pageCount) VALUES (1, the golden ticket, 255);
INSERT INTO Book (id, name, pageCount) VALUES (2, coding game, 300);INSERT INTO Author (id, firstName, LastName, bookId) VALUES (4, Brendon, Bouchard, 1);
INSERT INTO Author (id, firstName, LastName, bookId) VALUES (5, John, Doe, 2);3 代码实现
下面实现一个简单的查询
3.1 引入依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-graphql/artifactIdversion3.3.0/version/dependency 完整pom文件如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdspringbootgraphql/artifactIdversion1.0-SNAPSHOT/versionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.5/versionrelativePath //parentpropertiesjava.version1.8/java.version
!-- kotlin.version1.1.16/kotlin.version--/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-graphql/artifactIdversion3.3.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scopeexclusionsexclusiongroupIdorg.junit.vintage/groupIdartifactIdjunit-vintage-engine/artifactId/exclusion/exclusions/dependencydependencygroupIdcom.google.guava/groupIdartifactIdguava/artifactIdversion26.0-jre/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project 3.2 新建实体类
新建Author实体类
package com.example.demo.model;import javax.persistence.*;Entity
Table(name Author, schema BOOK_API_DATA)
public class Author {IdGeneratedValue(strategy GenerationType.AUTO)Integer id;Column(name firstname)String firstName;Column(name lastname)String lastName;Column(name bookid)Integer bookId;public Author(Integer id, String firstName, String lastName, Integer bookId) {this.id id;this.firstName firstName;this.lastName lastName;this.bookId bookId;}public Author() {}public Integer getId() {return id;}public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public void setId(Integer id) {this.id id;}public void setFirstName(String firstName) {this.firstName firstName;}public void setLastName(String lastName) {this.lastName lastName;}public Integer getBookId() {return bookId;}public void setBookId(Integer bookId) {this.bookId bookId;}
}
新建Book实体类
package com.example.demo.model;import javax.persistence.*;Entity
Table(name Book, schema BOOK_API_DATA)
public class Book {IdGeneratedValue(strategy GenerationType.AUTO)private Integer id;private String name;Column(name pagecount)private String pageCount;public Book(Integer id, String name, String pageCount) {this.id id;this.name name;this.pageCount pageCount;}public Book() {}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getPageCount() {return pageCount;}public void setPageCount(String pageCount) {this.pageCount pageCount;}
}新建输入AuthorInput实体
package com.example.demo.model.Input;public class AuthorInput {String firstName;String lastName;Integer bookId;public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public void setFirstName(String firstName) {this.firstName firstName;}public void setLastName(String lastName) {this.lastName lastName;}public Integer getBookId() {return bookId;}public void setBookId(Integer bookId) {this.bookId bookId;}
}3.3 新建repository类
新建AuthorRepository类
package com.example.demo.repository;import com.example.demo.model.Author;
import org.springframework.data.repository.CrudRepository;public interface AuthorRepository extends CrudRepositoryAuthor, Integer {Author findAuthorByBookId(Integer bookId);
}新建BookRepository类
package com.example.demo.repository;import com.example.demo.model.Book;
import org.springframework.data.repository.CrudRepository;public interface BookRepository extends CrudRepositoryBook, Integer {Book findBookByName(String name);
}3.4 新建Service层
package com.example.demo.service;import com.example.demo.model.Author;
import com.example.demo.repository.AuthorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class AuthorSevice {Autowiredprivate AuthorRepository authorRepository;public void addAuthon(Author author) {authorRepository.save(author);}public Author queryAuthorByBookId(int bookid) {Author author authorRepository.findAuthorByBookId(bookid);return author;}}3.5 新建控制器
package com.example.demo.controller;import com.example.demo.model.Author;
import com.example.demo.model.Input.AuthorInput;
import com.example.demo.service.AuthorSevice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;CrossOrigin
RestController
public class AuthorController {Autowiredprivate AuthorSevice authorSevice;/*** 经典 hollo graphql*/QueryMappingpublic String hello(){return hello graphql;}QueryMappingpublic Author getAuthorBybookId(Argument int bookid) {System.out.println(bookid:bookid);return authorSevice.queryAuthorByBookId(bookid);}MutationMappingpublic Author createUser(Argument AuthorInput authorInput) {Author author new Author();BeanUtils.copyProperties(authorInput,author);authorSevice.addAuthon(author);return author;}
}3.6 新建graphql文件
在resource文件中新建graphql文件 编写root.graphql文件
schema {query: Querymutation: Mutation
}
type Mutation{
}
type Query{
}
编写books.graphql文件
extend type Query {hello:StringgetAuthorBybookId(bookid:Int): Author
}extend type Mutation {createUser(authorInput: AuthorInput):Author
}input AuthorInput {firstName: StringlastName: StringbookId: Int
}type Author {id: IntfirstName: StringlastName: StringbookId: Int
}type Book {id: Intname: StringpageCount: Stringauthor: Author
} 3.7 编写yml配置文件
spring:jpa:hibernate:use-new-id-generator-mappings: falsegraphql:graphiql:enabled: truewebsocket:path: /graphqlschema:printer:enabled: truelocations: classpath:/graphql #test.graphql文件位置file-extensions: .graphqldatasource:url: jdbc:mysql://192.168.222.156:3306/book_api_data?useUnicodetruecharacterEncodingutf-8useSSLfalsepassword: 123456username: root
server:port: 8088 3.8 编写启动类
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class graphqlApplication {public static void main(String[] args) {SpringApplication.run(graphqlApplication.class,args);}
}4 启动测试
启动后访问http://localhost:8088/graphiql?path/graphqlwsPath/graphql 测试查询功能 测试新增功能