Unity3D + SpringCloud 实现对学生信息的 CRUD

2022-06-19 12:45:44

数据库

学生表
在这里插入图片描述

课程表
在这里插入图片描述

成绩表
在这里插入图片描述


搭建微服务框架

Server 环境配置

创建一个 maven 工程,配置pom.xml 文件

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.12</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

创建一个eurekaserver 子工程,并配置其pom.xml 文件如下:

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId><version>2.0.2.RELEASE</version></dependency></dependencies>

eurekaserver 子工程的 resources 目录下创建application.yml 配置如下:

server:port:8761#当前eureka server 服务端口eureka:client:register-with-eureka:false#是否将当前的 eureka server 服务作为客户端进行注册fetch-registry:false#是否获取其他 eureka server 服务的数据service-url:defaultZone: http://localhost:8761/eureka/#注册中心的访问地址

eurekakaserver 子工程的 java 目录下创建启动类:

@SpringBootApplication//SpringBoot 服务的入口@EnableEurekaServer//声明该类是一个 eureka server 微服务,提供服务注册和服务发现,即注册中心publicclassEurekaServerApplication{publicstaticvoidmain(String[] args){
        SpringApplication.run(EurekaServerApplication.class,args);}}

之后启动EurekaServerApplication,可以成功访问localhost:8761


Client 环境配置

创建一个eurekaclient 子工程,并配置其pom.xml 文件如下:

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.0.2.RELEASE</version></dependency></dependencies>

eurekaclient 子工程的 resources 目录下创建application.yml 配置如下:

server:port:8010spring:application:name: provider#当前服务注册在 eureka server 的名称eureka:client:service-url:defaultZone: http://localhost:8761/eureka/#注册中心的访问地址instance:prefer-ip-address:true#是否将当前服务的ip注册到 eureka server

eurekakaclient 子工程的 java 目录下创建启动类:

@SpringBootApplication//SpringBoot 服务的入口publicclassProviderApplication{publicstaticvoidmain(String[] args){
        SpringApplication.run(ProviderApplication.class,args);}}

DAO 层

搭建 eurekaserver 和 erurekaclient,在之前博客写过如何搭建
在父工程pom.xml 引入 mybatis、mysql、test 的依赖

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency>

resources/application.yml 配置数据源和 mybatis

spring:application:name: provider#当前服务注册在 eureka server 的名称datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///permission?useUnicode=true&characterEncoding=utf8&serverTimezone=UTCusername: rootpassword: sjh2019mybatis:#pojo别名扫描包type-aliases-package: com.sjh#加载Mybatis映射文件mapper-locations: classpath:mapper/*Mapper.xml

java/entiy/ 下编写学生实体类 Student

@DatapublicclassStudent{private Integer id;private String sname;private String cname;private Integer score;}

java/mapper/ 下编写操作数据库的接口 StudentMapper

@MapperpublicinterfaceStudentMapper{

    StudentfindStudentById(Integer sid,Integer cid);}

resources/mapper/ 下配置接口文件 studentMapper.xml

<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mappernamespace="com.sjh.mapper.StudentMapper"><selectid="findStudentById"parameterType="java.lang.Integer"resultType="Student">
        select s1.id, s1.sname, c.cname, s2.score
        from students as s1, scores as s2 , courses as c
        where s1.id = #{sid} and s2.cid = #{cid} and s2.cid = c.id</select></mapper>

表现层

@RestController@RequestMapping("/unity")publicclassUnityController{@Autowiredprivate StudentMapper studentMapper;@GetMapping("/findStudentById/{sid}/{cid}")public StudentfindStudentById(@PathVariable("sid") Integer sid,@PathVariable("cid") Integer cid){return studentMapper.findStudentById(sid, cid);}}

Unity

UI

创建一个新场景,创建输入域、按钮、文本框等,组合如下:
在这里插入图片描述
为了具有更好的语义,将组件重命名如下:
在这里插入图片描述


脚本编写

工具-》Nuget 包管理器 -》管理解决方案的 Nuget 程序包,搜索安装 newtonsoft.json
在这里插入图片描述

编写脚本SelectScores.cs

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using UnityEngine.Networking;using Newtonsoft.Json;publicclassSelectScores:MonoBehaviour{// 学生id的输入框publicInputField sidInput;// 学生所选课程号的输入框publicInputField cidInput;// 学生姓名的显示文本框publicText nameText;// 学生所选课程名的显示文本框publicText courseText;// 学生成绩的显示文本框publicText scoreText;publicvoidEndInput(){// 获取学生id,string 转为 intint sid=int.Parse(sidInput.text);
        Debug.Log("学生id:"+ sid);// 获取学生课程 id,string 转为 intint cid=int.Parse(cidInput.text);
        Debug.Log("学生课程id:"+ sid);// 通过 GET 请求获取数据StartCoroutine(GetRequest(sid, cid));}publicIEnumeratorGetRequest(int sid,int cid){string url="http://localhost:8010/unity/findStudentById/"+ sid+"/"+ cid;
        Debug.Log("请求的url:"+ url);using(UnityWebRequest webRequest= UnityWebRequest.Get(url)){yieldreturn webRequest.SendWebRequest();// 接受返回的数据
            Dictionary<string,object> data=null;// 出现网络错误if(webRequest.isHttpError|| webRequest.isNetworkError){
                Debug.LogError(webRequest.error+"\n"+ webRequest.downloadHandler.text);}else{//webRequest.downloadHandler.text// 输出获取到的数据到控制台Student student= JsonConvert.DeserializeObject<Student>(webRequest.downloadHandler.text);
                nameText.text= student.sname;
                courseText.text=  student.cname;
                scoreText.text=""+ student.score;}}}classStudent{publicstring sname;publicstring cname;publicint score;}}

将脚本添加到 Canvas 组件,并将组件拖动到脚本的 public 属性上
在这里插入图片描述
给提交按钮绑定鼠标点击事件
在这里插入图片描述


测试

运行游戏
在这里插入图片描述
输入数据点击查询
在这里插入图片描述


  • 作者:2020GetGoodOffer
  • 原文链接:https://blog.csdn.net/qq_41112238/article/details/108061633
    更新时间:2022-06-19 12:45:44