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进行访问
整体架构如下:

spring boot + mybatis 访问 neo4j的更多相关文章
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...
- Spring Boot MyBatis 数据库集群访问实现
Spring Boot MyBatis 数据库集群访问实现 本示例主要介绍了Spring Boot程序方式实现数据库集群访问,读库轮询方式实现负载均衡.阅读本示例前,建议你有AOP编程基础.mybat ...
- 07.深入浅出 Spring Boot - 数据访问之Mybatis(附代码下载)
MyBatis 在Spring Boot应用非常广,非常强大的一个半自动的ORM框架. 代码下载:https://github.com/Jackson0714/study-spring-boot.gi ...
- Spring Boot数据访问之整合Mybatis
在Mybatis整合Spring - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中谈到了Spring和Mybatis整合需要整合的点在哪些方面,需要将Mybatis中数据库连接池等相关对 ...
- spring boot + mybatis + druid
因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到可能时数据库连接的问题,所以我打算 ...
- spring boot+mybatis+quartz项目的搭建完整版
1. 利用spring boot提供的工具(http://start.spring.io/)自动生成一个标准的spring boot项目架构 2. 因为这里我们是搭建spring boot+mybat ...
- Spring Boot + Mybatis + Redis二级缓存开发指南
Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...
- Spring Boot + Mybatis 实现动态数据源
动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...
- Spring boot Mybatis 整合
PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版) 这篇博客里用到了怎样 生成 mybatis 插件来写程 ...
随机推荐
- 微信小程序上传图片(附后端代码)
几乎每个程序都需要用到图片. 在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OB ...
- vim用户设置
此配置目前使用户mac,linux,win,但是win系统需要提前配置mingw32相关的gcc系统路径等信息. " Setting some decent VIM settings for ...
- N!中素因子p的个数 【数论】
求N!中素因子p的个数,也就是N!中p的幂次 公式为:cnt=[n/p]+[n/p^2]+[n/p^3]+...+[n/p^k]; 例如:N=12,p=2 12/2=6,表示1~12中有6个数是2的倍 ...
- LRU Cache数据结构简介
什么是LRU Cache LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法. 什么是Cache?狭义的Cache指的是位于CPU和主存间的快速RAM ...
- KOA 学习(二)
app.listen(...) Koa 应用并非是一个 1-to-1 表征关系的 HTTP 服务器. 一个或多个Koa应用可以被挂载到一起组成一个包含单一 HTTP 服务器的大型应用群. var ko ...
- BZOJ2982: combination Lucas模板
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 734 Solved: 437[Submit][Status][Di ...
- JAVA面试常见问题之开源框架和容器篇
1.Servlet的生命周期 加载:加载到虚拟机 初始化:init() 一个生命周期中只会被调用一次. 服务:service() 销毁:destroy() 2.转发与重定向的区别 转发在服务器端完成的 ...
- 【html、CSS、javascript-2】CSS基础
CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式 ...
- Java review-basic2
1.Implement a thread-safe (blocking) queue: Class Producer implements Runable{ Private final Blockin ...
- R330 打印机连供墨水红灯常量处理
墨水灯红灯常量,表示墨盒没墨水 1.按红灯,将墨盒移动到右侧空处 2.按住连供顶部的重置小按钮 15秒以上,复位(这个应该是让连供墨盒产生一个另外的墨盒序号,骗打印机换了个新墨盒) 3.按打印机红灯, ...