spring boot mybatis 訪問 neo4j

spring boot   mybatis 訪問 neo4j

之前有通過rest的風格去訪問,但是每次需要訪問時候將statement一併加入header中去資料庫執行,方式簡單、且思路清晰,但是不便於形成模板呼叫,固採用mybaits來整合。

1.關鍵pom.xml依賴

 <dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>

1.關鍵pom.xml依賴

    <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.1.0</version>
</dependency>

2.mybatis訪問資料庫xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="LOCAL">
<environment id="LOCAL">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.neo4j.jdbc.Driver"/>
<property name="poolMaximumActiveConnections" value="10"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="url" value="${connectString}"/>
</dataSource>
</environment>
</environments>
</configuration>

3.properties 檔案屬性配置

spring.data.neo4j.username=xxx
spring.data.neo4j.password=xxx
#uri
#spring.data.neo4j.uri=bolt://ip:7687
spring.data.neo4j.rest.uri=http://ip:7474
spring.data.neo4j.mybatis.uri=jdbc:neo4j:bolt://ip:port
mybatis.neo4j.config.file=neo4j/mybatis-config-neo4j.xml

4.mybatis連線初始化java配置檔案,通過spring注入初始化

package neo4j.data;
import neo4j.service.Neo4jCrudService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
@Configuration
public class MyBatisInitial {
private static Logger logger = LoggerFactory.getLogger(MyBatisInitial.class);
private SqlSessionFactory sessionFactory;
@Value("${spring.data.neo4j.username}")
private String username ;
@Value("${spring.data.neo4j.password}")
private String password ;
@Value("${spring.data.neo4j.mybatis.uri}")
private String connectString ;
@Value("${mybatis.neo4j.config.file}")
private String resourceFile;
private List<String> mapperFiles = Arrays.asList("neo4j/MyMapper.xml");
/**
* @throws IOException
*/
protected void initialize() throws Exception {
sessionFactory = null;
try {
Properties properties = new Properties();
properties.setProperty("username", username);
properties.setProperty("password", password);
properties.setProperty("connectString", connectString);
sessionFactory = (new SessionFactoryProvider()).produceFactory(resourceFile, properties, mapperFiles);
} catch (Exception e) {
logger.error(e.getMessage());
throw e;
}
}
@Bean("mybatisNeo4jMapper")
public Neo4jCrudService getSqlSessionFactory() throws Exception {
initialize();
return sessionFactory.openSession().getMapper(Neo4jCrudService.class);
}
}

2.mybatis訪問資料庫xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="LOCAL">
<environment id="LOCAL">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.neo4j.jdbc.Driver"/>
<property name="poolMaximumActiveConnections" value="10"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="url" value="${connectString}"/>
</dataSource>
</environment>
</environments>
</configuration>

3.properties 檔案屬性配置

spring.data.neo4j.username=xxx
spring.data.neo4j.password=xxx
#uri
#spring.data.neo4j.uri=bolt://ip:7687
spring.data.neo4j.rest.uri=http://ip:7474
spring.data.neo4j.mybatis.uri=jdbc:neo4j:bolt://ip:port
mybatis.neo4j.config.file=neo4j/mybatis-config-neo4j.xml

4.mybatis連線初始化java配置檔案,通過spring注入初始化

import neo4j.service.Neo4jCrudService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
@Configuration
public class MyBatisInitial {
private static Logger logger = LoggerFactory.getLogger(MyBatisInitial.class);
private SqlSessionFactory sessionFactory;
@Value("${spring.data.neo4j.username}")
private String username ;
@Value("${spring.data.neo4j.password}")
private String password ;
@Value("${spring.data.neo4j.mybatis.uri}")
private String connectString ;
@Value("${mybatis.neo4j.config.file}")
private String resourceFile;
private List<String> mapperFiles = Arrays.asList("neo4j/MyMapper.xml");
/**
* @throws IOException
*/
protected void initialize() throws Exception {
sessionFactory = null;
try {
Properties properties = new Properties();
properties.setProperty("username", username);
properties.setProperty("password", password);
properties.setProperty("connectString", connectString);
sessionFactory = (new SessionFactoryProvider()).produceFactory(resourceFile, properties, mapperFiles);
} catch (Exception e) {
logger.error(e.getMessage());
throw e;
}
}
@Bean("mybatisNeo4jMapper")
public Neo4jCrudService getSqlSessionFactory() throws Exception {
initialize();
return sessionFactory.openSession().getMapper(Neo4jCrudService.class);
}
}

5.建立節點物件,@NodeEntity 註解表示了,這是一個neo4j的節點物件

package neo4j.node;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class Product {
private String prodname;
private String levelid;
public String getProdname() {
return prodname;
}
public void setProdname(String prodname) {
this.prodname = prodname;
}
public String getLevelid() {
return levelid;
}
public void setLevelid(String levelid) {
this.levelid = levelid;
}
@Override
public String toString() {
return "Product{"  
"prodname='"   prodname   '\''  
", levelid='"   levelid   '\''  
'}';
}
}

6.mybatis 的 mapper 檔案配置,這個地方就是寫 cypher語言的地方,這個id與service介面中的方法名保持一致,因為名稱空間已經指定了Neo4jCrudService

<?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="neo4j.service.Neo4jCrudService">
<select id="selectProduct" resultType="neo4j.node.Product">
MATCH (n) RETURN  n.prodname as prodname, n.levelid as levelid  LIMIT 25
</select>
</mapper>
package neo4j.service;
import neo4j.node.Product;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import java.util.List;
public interface Neo4jCrudService {
public ResponseEntity<String> queryDataWithNoParam(HttpEntity<String> request);
public List<Product> selectProduct();
}

7.封裝control

   @RequestMapping(value = "/postDataViaMybaits", method = RequestMethod.POST)
public Object getNeoDatabyMybatis() throws Exception {
//建立連線
log.info("postData begin time:"   new Date());// 介面呼叫開始時間
List<Product> response = neo4jService.selectProduct();
log.info("result: "   response "");
log.info("postData end time:"   new Date());// 介面呼叫結束時間
return response;
}

這層邏輯只有一句有效的程式碼。neo4jService通過注入、然後通過mybatis進行訪問

整體架構如下:
這裡寫圖片描述