引言
经过考虑,我感觉与NebulaGraph交互的ORM框架还是Ngbatis好。因为现在这个框架开发的比较完善,而且还在不断更新,社区活跃的用户多。从今日开始学习,首先要配置一下环境。
1.安装maven和jdk
选择的版本是maven3.8和jdk17.以下是安装步骤:
#安装jdk17
sudo apt install openjdk-17-jdk
nano ~/.bashrc
# 在文件末尾添加以下内容
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
#使得环境生效
source ~/.bashrc
#验证
echo $JAVA_HOME
java -version
#2.安装maven3.8.8
wget https://dlcdn.apache.org/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz
#-C后换成自己想安的路径 /opt只是个示例。
sudo tar -zxvf apache-maven-3.8.8-bin.tar.gz -C /opt
#3.创建符号链接
sudo ln -s /opt/apache-maven-3.8.8 /opt/maven
#4.配置环境变量
sudo nano /etc/profile.d/maven.sh
#在文件添加以下内容
export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
#使得环境生效
source /etc/profile.d/maven.sh
#5.验证是否安装成功
mvn -v
(1)接下来maven仓库设置一下,
在 settings.xml
文件中,添加或修改 localRepository
元素,例如:
<localRepository>/path/to/your/local/repo</localRepository>
运行以下命令验证Maven 是否使用了新的本地仓库路径
mvn help:evaluate -Dexpression=settings.localRepository
结果:
(2)设置国内阿里云镜像(160行附近)
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
(3)配置jdk17版本项目构建(260行附近)
<profile>
<id>jdk-17</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>17</jdk>
</activation>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
</properties>
</profile>
(4)IDEA配置本地Maven
我的Maven地址后来改成了/home/kg.IDEA我安装的是社区版。
2.Ngbatis具体环境配置
1.NebulaGraph 中创建的 Schema
要先进入控制台
#创建标签
CREATE tag `person` (
`name` string NULL ,
`gender` string NULL ,
`age` int NULL ,
`birthday` date NULL
);
#创建边类型
CREATE edge `like` (`likeness` double NULL );
-- 为查询创建索引
CREATE TAG INDEX `i_person_name_age` on `person`(`name`(50), `age`);
CREATE TAG INDEX `i_person_name` on `person`(`name`(50));
#插入person节点数据
INSERT VERTEX person(name, gender, age, birthday) VALUES "1":("zhangsan", "男", 18, date("1999-02-02"));
2.后端配置
(1)先创建一个项目
groupId写错了,请改成com.ngbatis
(2)相关依赖包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>3.0.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ngbatis</groupId>
<artifactId>Ngbatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Ngbatis -->
<dependency>
<groupId>org.nebula-contrib</groupId>
<artifactId>ngbatis</artifactId>
<version>1.2.2-jdk17</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3)项目配置(application.yaml)
我的NebulaGraph数据库放在另一台虚拟机上
# server配置
server:
port: 8081
servlet:
context-path: /
nebula:
ngbatis:
# ^v1.1.2
# 连接使用 nebula-java 中的 SessionPool
session-life-length: 300000
check-fixed-rate: 300000
use-session-pool: true
# 填入 graphd 的 ip 和端口号,下面仅供参考
hosts: 192.168.145.138:9669
# 连接图数据库所用的用户名
username: root
# 连接图数据库所用的密码
password: nebula
# 所要连接的图数据库图空间名
space: my_space
# 连接池配置
pool-config:
# 连接池中最小空闲连接数
min-conns-size: 0
# 连接池中最大空闲连接数
max-conns-size: 10
# 客户端同服务端建立连接的超时时间设置,单位为 ms;超过设定时间未建立起连接,则报错
timeout: 6000
# 连接空闲时间,为 0 表示连接永不删除,单位为 ms
idle-time: 0
# 连接池检测空闲连接的时间间隔,为 -1 表示不进行检测
interval-idle: -1
# 连接等候时间,超过则不再等候连接
wait-time: 6000
# 集群允许最小的服务可用率,1.0 表示为所有机器 graphd 可用,0.25 表示集群中 1/4 机器可用即可
min-cluster-health-rate: 1.0
# 是否允许 SSL 连接,目前暂不支持
enable-ssl: true
(4)实体类Person
package com.ngbatis.entity;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import lombok.Data;
@Data
@Table(name = "person")
public class Person {
@Id
private String name;
/** use @Column to declare field's schema name in database */
@Column(name = "gender")
private String gender;
private Integer age;
private Date birthday;
/** use @Transient to declare a field which is not exists in database */
@Transient
private String fieldDbNotExists;
}
(4)PersonDao
package com.ngbatis.mapper;
import com.ngbatis.entity.Person;
import org.nebula.contrib.ngbatis.proxy.NebulaDaoBasic;
import java.util.List;
public interface PersonDao extends NebulaDaoBasic<Person, String> {
public List<Person> selectAll();
}
(5)PersonDao.xml
<mapper namespace="com.ngbatis.mapper.PersonDao">
<selcet id="selectAll" resultType="com.ngbatis.entity.Person">
match (n:person) return n limit 10;
</selcet>
</mapper>
(6)PersonController
package com.ngbatis.controller;
import com.ngbatis.entity.Person;
import com.ngbatis.mapper.PersonDao;
import com.ngbatis.service.PersonService;
import com.ngbatis.service.impl.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonDao personDao;
@GetMapping("select")
public HashMap<String, Object> selectById() {
List<Person> people = personDao.selectAll();
System.out.println(people);
return new HashMap<String, Object>() {{
put("code", 200);
}};
}
}
(7)启动类(SpringbootApplication)
package com.ngbatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(
exclude = { DataSourceAutoConfiguration.class },
scanBasePackages = { "org.nebula.contrib.ngbatis", "com.ngbatis" }
)
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
3.结果
前两次还没插入数据