1. pom.xml
2. 核心配置代码
import com.alibaba.druid.pool.DruidDataSource;
import com.zax.common.ResourceMapping;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.datasource.DataSourceFactory;
import org.apache.ibatis.datasource.jndi.JndiDataSourceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.neo4j.driver.v1.Config;
import org.neo4j.jdbc.Neo4jDataSource;
import org.neo4j.jdbc.bolt.BoltNeo4jDataSource;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.validation.DataBinder;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
/***
*@作者
*@功能描述 杨慕义
*@时间19:17
*@方法05
*2019/5/13
*/
@Configuration
@EnableNeo4jRepositories(basePackages = {"com.data.demo.model.mapper","com.data.model.mapper"})
@MapperScan(basePackages = {"com.data.demo.model.mapper","com.data.model.mapper"},sqlSessionTemplateRef = "neo4jSqlSessionTemplate")
public class Neo4jConf {
@Bean(name="neo4jSqlsessionFactory")
public SqlSessionFactory neo4jSqlsessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource());
String mapping = "classpath*:com/data/demo/model/mapping/*.xml";
//如需添加多个xml包的路径 这样写 //"classpath*:com/data/model/mapping/*.xml,com/data/demo/model/mapping/*.xml";
bean.setMapperLocations(new ResourceMapping().getResources(new PathMatchingResourcePatternResolver(), mapping));
return bean.getObject();
}
@Bean(name = "neo4jSqlSessionTemplate")
public SqlSessionTemplate neo4jSqlSessionTemplate(
@Qualifier("neo4jSqlsessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
//此处是为了方便兼容 spirng data neo4j jpa 但是mybatis有了所以 聊胜于无
@Bean(name="sessionFactory")
public SessionFactory sessionFactory() throws Exception {
org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder()
.uri("bolt://localhost:7687")
.credentials("neo4j", "你的密码")
.build();
String [] packages = {"com.data.demo.model.domain","com.data.model.domain"};
SessionFactory sessionFactory = new SessionFactory(configuration,packages);
return sessionFactory;
}
// public SqlSessionFactory sqlSessionFactory() throws Exception {
// SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// bean.setDataSource(dataSource());
// String mapping = "classpath*:com/data/model/mapping/*.xml,com/data/demo/model/mapping/*.xml";
// bean.setMapperLocations(new ResourceMapping().getResources(new PathMatchingResourcePatternResolver(), mapping));
// return bean.getObject();
// }
public DataSource dataSource(){
// HikariConfig hikariConfig = new HikariConfig();
// hikariConfig.setJdbcUrl("jdbc:neo4j:bolt://localhost:7687/");
// hikariConfig.setUsername("neo4j");
// hikariConfig.setPassword("你的密码");
// hikariConfig.setDriverClassName("org.neo4j.jdbc.bolt.BoltDriver");
// HikariDataSource h = new HikariDataSource(hikariConfig);
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDbType("javax.sql.DataSource");
druidDataSource.setDriverClassName("org.neo4j.jdbc.bolt.BoltDriver");
druidDataSource.setUrl("jdbc:neo4j:bolt://localhost:7687/");
druidDataSource.setUsername("neo4j");
druidDataSource.setPassword("你的密码");
return druidDataSource;
// return h;
}
3. 辅助代码 ResourceMapping .java ListUitl.java
import org.frameworkset.util.ResourceUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.tio.utils.hutool.ResourceUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/***
*@作者 杨慕义
*@功能描述 读取多个XML 路径 不使用spring自带的 自定义修改
*@时间13:39
*@方法01
*2019/1/31
*/
public class ResourceMapping {
String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
/*classpath*多个mapping*/
public Resource[] getResources(ResourcePatternResolver resolver, String mappingclasspath) throws IOException {
String [] classpaths = mappingclasspath.split(",");
List list = new ArrayList();
for(int i = 0 ; i<classpaths.length; i ++){
if(classpaths[i].startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX)){
list.add(resolver.getResources(classpaths[i]));
}else{
list.add(resolver.getResources("classpath*:"+classpaths[i]));
}
}
Resource [] resources= null;
for(int i = 0 ; i<list.size(); i ++){
if((i+1)<list.size()){
if(i == 0){
resources = ListUitl.concatAll((Resource [])list.get(0),(Resource [])list.get(i+1));
}else{
resources = ListUitl.concatAll(resources,(Resource [])list.get(i+1));
}
//这里是判断对象是否为空 代码直接百度替换
}else if(StringUtil.isNullOrEmpty(resources)){
resources = (Resource[]) list.get(i);
}else {
continue;
}
}
return resources;
}
/*classpath多个mapping 不适合 *.后缀 多不同路径 如果需要可以修改*/
public Resource[] getResource(ResourcePatternResolver resolver, String mappingclasspath) throws IOException {
String [] classpaths = mappingclasspath.split(",");
List list = new ArrayList();
for(int i = 0 ; i<classpaths.length; i ++){
if(classpaths[i].startsWith(CLASSPATH_URL_PREFIX)){
list.add(resolver.getResources(classpaths[i]));
}else{
list.add(resolver.getResources("classpath:"+classpaths[i]));
}
}
Resource [] resources= null;
for(int i = 0 ; i<list.size(); i ++){
if((i+1)<list.size()){
if(i == 0){
resources = ListUitl.concatAll((Resource [])list.get(0),(Resource [])list.get(i+1));
}else{
resources = ListUitl.concatAll(resources,(Resource [])list.get(i+1));
}
//这里是判断对象是否为空 代码直接百度替换
}else if(StringUtil.isNullOrEmpty(resources)){
resources = (Resource[]) list.get(i);
}else {
continue;
}
}
return resources;
}
}
import java.util.*;
/***
*@作者 杨慕义
*@功能描述 list 操作工具类
*@时间15:54
*@方法01
*2019/1/29
*/
public class ListUitl {
/*
*删除重复并包含自己
*/
public static <T> List<T> removeRepeated(List<T> arr) {
List<T> tmpArr = new ArrayList<T>();
for(int i=0; i<arr.size(); i++) {
if(tmpArr.contains(arr.get(i))) {
tmpArr.remove(arr.get(i));
// i--;
}else {
tmpArr.add(arr.get(i));
}
}
return tmpArr;
}
/**
* 循环list中的所有元素然后删除重复
* @param list 待去重的list
* @return 去重后的list
*/
public static <T> List<T> removeDuplicate(List<T> list){
for(int i=0;i<list.size()-1;i++){
for(int j=list.size()-1;j>i;j--){
if (list.get(j).equals(list.get(i))){
list.remove(j);
}
}
}
return list;
}
/**
* 通过HashSet去重
* @param list 待去重的list
* @return 去重后的list
*/
public static <T> List<T> removeDuplicateHashSet(List<T> list){
HashSet<T> hs = new HashSet<>(list);
list.clear();
list.addAll(hs);
return list;
}
/*合并数组 多个*/
public static <T> T[] concatAll(T[] first, T[]... rest) {
int totalLength = first.length;
for (T[] array : rest) {
totalLength += array.length;
}
T[] result = Arrays.copyOf(first, totalLength);
int offset = first.length;
for (T[] array : rest) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;
}
public static <T> Iterable<T> iterableReverseList(final List<T> l) {
return new Iterable<T>() {
public Iterator<T> iterator() {
return new Iterator<T>() {
ListIterator<T> listIter = l.listIterator(l.size());
public boolean hasNext() { return listIter.hasPrevious(); }
public T next() { return listIter.previous(); }
public void remove() { listIter.remove(); }
};
}
};
}
}
4.Mapper文件
5.Mappingxml
<?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.data.demo.model.mapper.PersonMapper">
<resultMap id="BaseResultMap" type="com.data.demo.model.domain.Person">
<id column="id" javaType="java.lang.Long" property="id"/>
<result column="name" javaType="java.lang.String" property="name"/>
<result column="born" javaType="java.lang.String" property="born"/>
</resultMap>
<select id="findPersons" resultMap="BaseResultMap">
MATCH (n:Person) return n.name as name,n.born as born,n.id as id
</select>
</mapper>
6.domain
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
/***
*@作者 杨慕义
*@功能描述
*@时间19:29
*@方法05
*2019/5/13
*/
@NodeEntity(label = "Person")
public class Person {
@Id
@GeneratedValue
private Long id;
@Property(name = "name")
private String name;
@Property(name = "born")
private String born;
public Person() {
}
public Person(String name, String born) {
this.name = name;
this.born = born;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBorn() {
return born;
}
public void setBorn(String born) {
this.born = born;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"id\":")
.append(id);
sb.append(",\"name\":\"")
.append(name).append('\"');
sb.append(",\"born\":\"")
.append(born).append('\"');
sb.append('}');
return sb.toString();
}
}
结果展示
转发请标注来源