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.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>
总体来说,对于习惯使用MyBatis或者MyBatis-Plus的开发人员来说还是很容易上手的。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Python学习笔记》是由皮大庆编写的一本关于Python语言学习的教材。在这本书中,作者详细介绍了Python语言的基础知识、语法规则以及常用的编程技巧。 首先,作者简要介绍了Python语言的特点和优势。他提到,Python是一种易于学习和使用的编程语言,受到了广大程序员的喜爱。Python具有简洁、清晰的语法结构,使得代码可读性极高,同时也提供了丰富的库和模块,能够快速实现各种功能。 接着,作者详细讲解了Python的基本语法。他从变量、数据类型、运算符等基础知识开始,逐步介绍了条件语句、循环控制、函数、模块等高级概念。同时,作者通过大量的示例代码和实践案例,帮助读者加深对Python编程的理解和应用。 在书中,作者还特别强调了编写规范和良好的编程习惯。他从命名规范、注释风格、代码缩进等方面指导读者如何写出清晰、可读性强的Python代码。作者认为,良好的编程习惯对于提高代码质量和提高工作效率非常重要。 此外,作者还介绍了Python的常用库和模块。他提到了一些常用的库,如Numpy、Pandas、Matplotlib等。这些库在数据处理、科学计算、可视化等领域有广泛的应用,帮助读者更好地解决实际问题。 总的来说,《Python学习笔记》是一本非常实用和全面的Python学习教材。通过学习这本书,读者可以系统地学习和掌握Python编程的基础知识和高级应用技巧,为以后的编程学习和工作打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值