SpringBoot+Mybatis集成neo4j
1 maven依赖
< dependency>
< groupId> org.neo4j</ groupId>
< artifactId> neo4j-jdbc-driver</ artifactId>
< version> 3.4.0</ version>
</ dependency>
2 配置文件
spring :
datasource :
druid :
driver-class-name : org.neo4j.jdbc.http.HttpDriver
url : jdbc: neo4j: http: //localhost: 7474
username : neo4j
password : 123456
validation-query : MATCH (n) RETURN COUNT(n)
3 在SpringBoot的启动类的入口中添加配置
public static void main ( String [ ] args) {
DruidConnectionHolder . holdabilityUnsupported = true ;
SpringApplication . run ( Neo4jProjectPlusApplication . class , args) ;
}
4 创建dao层文件
< mapper namespace = " io.renren.dao.Neo4jPlusBaseDao" >
< select id = " getTripleList" resultType = " java.lang.Object" >
START n1= node(*) MATCH p = (n1)-[r]->(n2) WHERE 1 = 1 RETURN p
</ select>
</ mapper>
@Mapper
public interface Neo4jPlusBaseDao {
List < Object [ ] > getTripleList ( ) ;
}
5 创建service
public interface Neo4jPlusBaseService {
Map < String , Object > getTripleList ( ) ;
}
@Service
public class Neo4jPlusBaseServiceImpl implements Neo4jPlusBaseService {
@Autowired
private Neo4jPlusBaseDao neo4jPlusBaseDao;
public Map < String , Object > getTripleList ( ) {
List < Map < String , Object > > triples = new ArrayList < > ( ) ;
List < Object [ ] > list = neo4jPlusBaseDao. getTripleList ( ) ;
Integer total = neo4jPlusBaseDao. getTotal ( ) ;
for ( Object [ ] o : list) {
List < Map < String , Object > > temp = new ArrayList < > ( ) ;
for ( int i= 0 ; i< o. length; i++ ) {
temp. add ( ( LinkedHashMap ) o[ i] ) ;
}
Map < String , Object > triple = new HashMap < > ( ) ;
triple. put ( "source" , temp. get ( 0 ) . get ( "name" ) ) ;
triple. put ( "relation" , temp. get ( 1 ) . get ( "name" ) ) ;
triple. put ( "target" , temp. get ( 2 ) . get ( "name" ) ) ;
triple. put ( "id" , temp. get ( 1 ) . get ( "index" ) ) ;
triples. add ( triple) ;
}
Map < String , Object > result = new HashMap < > ( ) ;
result. put ( "total" , total) ;
result. put ( "list" , triples) ;
return result;
}
}