本文主要是介绍NebulaGraph学习笔记-NgBatis连接,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近项目需要连接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.2check-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.2hosts: 127.0.0.1:19669, 127.0.0.1:9669username: rootpassword: nebulaspace: testpool-config:min-conns-size: 0max-conns-size: 10timeout: 6000idle-time: 0interval-idle: -1wait-time: 6000min-cluster-health-rate: 1.0enable-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 {@Idprivate 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 > $p0RETURN nLIMIT 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 > $p1RETURN nLIMIT 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, n2LIMIT 100</select></mapper>
总体来说,对于习惯使用MyBatis或者MyBatis-Plus的开发人员来说还是很容易上手的。
这篇关于NebulaGraph学习笔记-NgBatis连接的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!