最近项目需要连接NebulaGraph图数据库获取部分数据,于是查看了一些相关资料,发现一个NgBatis框架。
NgBatis是一个使用类似MyBatis+MyBatis-Plus的方式操作NebulaGraph的JavaORM框架。同时NgBatis也是一款针对NebulaGraph+SpringBoot的数据库ORM框架。借鉴于MyBatis的使用习惯进行开发,当中包含了部分类似于mybatis-plus的单表操作。另外还有部分图特有的实体-关系基本操作。具体的可以点击查看官方文档。
NgBatis连接
- 相关依赖包
<!-- SpringBoot依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- Client依赖包 -->
<dependency>
<groupId>com.vesoft</groupId>
<artifactId>client</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.nebula-contrib</groupId>
<artifactId>ngbatis</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>com.vesoft</groupId>
<artifactId>client</artifactId>
</exclusion>
</exclusions>
</dependency>
- 项目引入配置
# https://github.com/nebula-contrib/ngbatis
nebula:
ngbatis:
session-life-length: 300000 # since v1.1.2
check-fixed-rate: 300000 # since v1.1.2
# space name needs to be informed through annotations(@Space) or xml(space="test")
# default false(false: Session pool map will not be initialized)
use-session-pool: false # since v1.1.2
hosts: 127.0.0.1:19669, 127.0.0.1:9669
username: root
password: nebula
space: test
pool-config:
min-conns-size: 0
max-conns-size: 10
timeout: 6000
idle-time: 0
interval-idle: -1
wait-time: 6000
min-cluster-health-rate: 1.0
enable-ssl: false
- 部分参考代码
@SpringBootApplication
@ComponentScan(basePackages = { "org.nebula.contrib" })
public class NebulaGraphApplication {
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Table(name = "player_with_default")
@NoArgsConstructor
@AllArgsConstructor
public class PlayerWithDefaultPO {
@Id
private String name;
private Integer age;
public PlayerWithDefaultPO(String name) {
this.name = name;
}
}
import org.nebula.contrib.ngbatis.proxy.NebulaDaoBasic;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Map;
public interface PlayerWithDefaultDAO extends NebulaDaoBasic<PlayerWithDefaultPO, String> {
Integer returnAge(PlayerWithDefaultPO playerWithDefaultPO);
PlayerWithDefaultPO selectOne(@Param("name") String name);
List<PlayerWithDefaultPO> selectAgeGt(Integer age);
List<PlayerWithDefaultPO> selectNameAndAgeGt(String name, Integer age);
List<PlayerWithDefaultPO> selectList();
List<String> selectListString();
List<Map> selectListMap();
Map<String, Object> selectMap();
}
<mapper namespace="com.dao.PlayerWithDefaultDAO">
<!-- new features from v1.2.0 start -->
<nGQL id="include-player-value">
${playerWithDefaultPO}
</nGQL>
<nGQL id="ngql-return-age">
RETURN @ng.include('include-player-value',{'playerWithDefaultPO':age});
</nGQL>
<!--
The same as:
RETURN ${person.age};
You can try extracting more common and meaningful scripts.
-->
<select id="returnAge" resultType="java.lang.Integer">
@ng.include('ngql-return-age',playerWithDefaultPO);
</select>
<select id="selectOne" resultType="com.domain.po.PlayerWithDefaultPO">
MATCH (v:player_with_default) WHERE id(v) == $name return v limit 1
</select>
<select id="selectAgeGt" resultType="com.domain.po.PlayerWithDefaultPO">
MATCH (n:player_with_default)
WHERE n.player_with_default.age > $p0
RETURN n
LIMIT 100
</select>
<select id="selectNameAndAgeGt" resultType="com.domain.po.PlayerWithDefaultPO">
MATCH (n:player_with_default)
WHERE id(n) == $p0 AND n.player_with_default.age > $p1
RETURN n
LIMIT 100
</select>
<select id="selectList" resultType="com.domain.po.PlayerWithDefaultPO">
MATCH (v:player_with_default) RETURN v LIMIT 100
</select>
<select id="selectListString" resultType="java.lang.String">
MATCH (v:player_with_default) RETURN id(v) as name LIMIT 100
</select>
<select id="selectListMap" resultType="java.util.Map">
MATCH (v:player_with_default) RETURN id(v) as name, v.player_with_default.age as age LIMIT 100
</select>
<select id="selectMap" resultType="java.util.Map">
MATCH (n: player_with_default)-[r: like]->(n2: player_with_default)
RETURN n, r, n2
LIMIT 100
</select>
</mapper>