数据库
学生表
课程表
成绩表
搭建微服务框架
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 微服务,提供服务注册和服务发现,即注册中心
public class EurekaServerApplication {
public static void main(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: 8010
spring:
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 服务的入口
public class ProviderApplication {
public static void main(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.Driver
url: jdbc:mysql:///permission?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: sjh2019
mybatis:
#pojo别名扫描包
type-aliases-package: com.sjh
#加载Mybatis映射文件
mapper-locations: classpath:mapper/*Mapper.xml
java/entiy/
下编写学生实体类 Student
@Data
public class Student {
private Integer id;
private String sname;
private String cname;
private Integer score;
}
java/mapper/
下编写操作数据库的接口 StudentMapper
@Mapper
public interface StudentMapper {
Student findStudentById(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" >
<mapper namespace="com.sjh.mapper.StudentMapper">
<select id="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")
public class UnityController {
@Autowired
private StudentMapper studentMapper;
@GetMapping("/findStudentById/{sid}/{cid}")
public Student findStudentById(@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;
public class SelectScores : MonoBehaviour
{
// 学生id的输入框
public InputField sidInput;
// 学生所选课程号的输入框
public InputField cidInput;
// 学生姓名的显示文本框
public Text nameText;
// 学生所选课程名的显示文本框
public Text courseText;
// 学生成绩的显示文本框
public Text scoreText;
public void EndInput()
{
// 获取学生id,string 转为 int
int sid = int.Parse(sidInput.text);
Debug.Log("学生id:" + sid);
// 获取学生课程 id,string 转为 int
int cid = int.Parse(cidInput.text);
Debug.Log("学生课程id:" + sid);
// 通过 GET 请求获取数据
StartCoroutine(GetRequest(sid, cid));
}
public IEnumerator GetRequest(int sid, int cid)
{
string url = "http://localhost:8010/unity/findStudentById/" + sid + "/" + cid;
Debug.Log("请求的url:" + url);
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
yield return 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;
}
}
}
class Student
{
public string sname;
public string cname;
public int score;
}
}
将脚本添加到 Canvas 组件,并将组件拖动到脚本的 public 属性上
给提交按钮绑定鼠标点击事件
测试
运行游戏
输入数据点击查询