MyBatis框架连接neo4j数据库查询

MyBatis框架连接neo4j数据库查询,第1张

MyBatis框架连接neo4j数据库查询

在尚硅谷学完了mybatis框架,于是想试试用mybatis框架连接neo4j数据库,也给和我一样的小白做个参考。

具体流程
  • 搭建maven环境,在pom.xml文件中引入neo4j,mybatis,junit的依赖,写好后记得刷新maven
  • 配置mybatis核心配置文件:mybatis-config.xml
  • 配置neo4j.properties , log4j.xml(日志,非必须)
  • 在java的pojo包下创建要映射数据库表的实体类Person
  • 在java的mapper包下创建PersonMapper接口,写 *** 作的抽象方法
  • 在resources的mapper包下创建PersonMapper.xml , 写具体的查询语句
  • 在java的utils包下创建工具类SqlSessionUtil
  • 在test的包下创建测试类MyTest
按流程一个个完成

先贴上我的目录结构

  1. pom.xml文件如下

<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.0modelVersion>

    <groupId>com.atguigu.neo4jgroupId>
    <artifactId>neo4j-demo1artifactId>
    <version>1.0-SNAPSHOTversion>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>8source>
                    <target>8target>
                configuration>
            plugin>
        plugins>
    build>
    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>io.thorntailgroupId>
            <artifactId>neo4jartifactId>
            <version>2.7.0.Finalversion>
        dependency>
        <dependency>
            <groupId>org.neo4jgroupId>
            <artifactId>neo4j-jdbc-driverartifactId>
            <version>3.1.0version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.6version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
    dependencies>


project>
  1. 在resources文件夹下创建mybatis-config.xml
 DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    
    <properties resource="neo4j.properties">properties>

    <settings>
        
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        
        
    settings>
    <typeAliases>
        <package name="com.atguigu.mybatis.pojo">package>
    typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${neo4j.driver}"/>
                <property name="url" value="${neo4j.url}"/>
                <property name="username" value="${neo4j.username}"/>
                <property name="password" value="${neo4j.password}"/>
            dataSource>
        environment>
    environments>
    <mappers>
        <package name="com.atguigu.mybatis.mapper">package>
    mappers>
configuration>
  1. 在resources文件下创建neo4j.properties,username和password用自己的替换
neo4j.driver=org.neo4j.jdbc.Driver
neo4j.url=jdbc:neo4j:http://localhost:7474/
neo4j.username=neo4j
neo4j.password=neo4jsunjiaqi
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

  1. 在resources文件夹下创建log4j.xml
 DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Encoding" value="UTF-8"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/>
        layout>
    appender>
    <logger name="java.sql">
        <level value="debug"/>
    logger>
    <logger name="org.apache.ibatis">
        <level value="info"/>
    logger>
    <root>
        <level value="debug"/>
        <appender-ref ref="STDOUT"/>
    root>
log4j:configuration>
  1. 在java的pojo包下创建要映射数据库表的实体类Person
package com.atguigu.mybatis.pojo;

/**
 * @author Sun
 * @version 1.0
 */
public class Person {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    public Person() {
    }

    public Person(Integer id, String name, Integer age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

  1. 在java的mapper包下创建PersonMapper接口,写 *** 作的抽象方法
package com.atguigu.mybatis.mapper;

import com.atguigu.mybatis.pojo.Person;

import java.util.List;

/**
 * @author Sun
 * @version 1.0
 */
public interface PersonMapper {
    List<Person> getAllPerson ();
}

  1. 在resources的mapper包下创建PersonMapper.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.atguigu.mybatis.mapper.PersonMapper">
    <!--
    List<Person> getAllPerson ();
    -->
    <select id="getAllPerson" resultType="person">
        MATCH (person:Person) RETURN person
    </select>
</mapper>

8.在java的utils包下创建工具类SqlSessionUtil

package com.atguigu.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Sun
 * @version 1.0
 */
public class SqlSessionUtil {
    public static SqlSession getSqlSession () {
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sessionFactory.openSession();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }
}

9.在test的包下创建测试类MyTest

package com.atguigu.mybatis.test;

import com.atguigu.mybatis.mapper.PersonMapper;
import com.atguigu.mybatis.pojo.Person;
import com.atguigu.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author Sun
 * @version 1.0
 */
public class MtTest {
    SqlSession sqlSession;
    @Before
    public void openSession () {
        sqlSession = SqlSessionUtil.getSqlSession();
    }
    @After
    public void closeSqlSession () {
        sqlSession.close();
    }
    @Test
    public void testSelect () {
        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
        List<Person> allPerson = mapper.getAllPerson();
        allPerson.forEach(person -> System.out.println(person));
    }
}

查询后结果

neo4j数据库中Person节点图

这里因为我数据库中Person表的数据只有一个name属性,所以查出来就是这样了,虽然查出来了,却报错了java.lang.StackOverflowError,我还不知道怎么解决,也没有死循环,不知道为什么栈溢出。emm,目前还没想好节点之间的关系该怎么映射,因为Person是用python读取csv文件,手动指定两列之间的关系存进去的,还得继续学,大兄弟们加油!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/923331.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存