junit 的 neo4j 配置和在密码查询中传递 arraylist 实例
neo4j configuration for junit and passing arraylist instance in cypher query
我的情况是通过 MySQL 检索数据并将其保存在 ArrayList 对象中,我需要将相同的对象放入密码查询中,运行 我的程序带有 junit 测试` public class DummyTest {
@Test
@Rollback(true)
public void checkMysqlConnection() {
Connection connection = null;
ResultSet tableDatasRsult = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "root");
String url = "127.0.0.1:3306/unicity";
connection = DriverManager.getConnection("jdbc:mysql://" + url, properties);
Assert.assertNotNull(connection);
String sqlq = "select DIST_ID,PV_DATE,POST_DATE from odh";
PreparedStatement preparedStatement = connection.prepareStatement(sqlq);
tableDatasRsult = preparedStatement.executeQuery();
ArrayList<OdhDO> odhDOs = new ArrayList<>();
while (tableDatasRsult.next()) {
OdhDO odhDO = new OdhDO();
odhDO.setDistId(tableDatasRsult.getLong("DIST_ID"));
odhDO.setPvDate(tableDatasRsult.getString("PV_DATE"));
odhDO.setPostDate(tableDatasRsult.getString("POST_DATE"));
odhDOs.add(odhDO);
Map<String, Object> params = new HashMap<>();
params.put(odhDO.getDistId().toString(), odhDO);
System.out.println(params);
}
} catch (ClassNotFoundException e) {
System.out.println("MySql Driver Class Not Found Exception");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Sql Exception");
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
@Rollback(true)
public void checkneo4jConnection(){
String URL= "http://localhost:7474";
Configuration configuration = new Configuration();
configuration.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver").setURI(URL).setCredentials("neo4j", "admin");
String cypher = "MATCH (Distributor) WHERE Distributor.DISTID IN {odhDOs} RETURN Distributor";
Map<String, Object> params = new HashMap<>();
List<String> odhDOs = new ArrayList<>();
params.put("odhDOs", odhDOs);
//add some names to the names list
//params.put(key, value)
}`
在上面的 checkMysqlConnection() 方法代码中,我检索了数据并将这些数据保存在 ArrayList odhDOs = new ArrayList<>() 对象,我需要在密码查询中传递这个 Arraylist 实例,但我不知道如何从这个 class 配置和连接 neo4j 以及如何在查询中传递这个 arraylist 实例
"MATCH (Distributor) WHERE Distributor.DISTID IN {odhDOs} RETURN Distributor"
请帮助使用 neo4j 配置 junit 以及如何在密码查询中传递数组实例..提前致谢...
如果您已根据 OGM reference guide 中的文档正确设置了所有内容,那么类似下面的内容应该可以工作:
@Test
public void checkneo4jConnection(){
Configuration configuration = new Configuration();
configuration.driverConfiguration()
.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
.setURI("http://localhost:7474")
.setCredentials("neo4j", "admin");
SessionFactory sessionFactory = new SessionFactory(configuration, "package.where.your.distributor.class.is");
Session session = sessionFactory.openSession();
String cypher = "MATCH (Distributor) WHERE Distributor.DISTID IN {odhDOs} RETURN Distributor";
Map<String, Object> params = new HashMap<>();
params.put("odhDOs", Arrays.asList(new String[] {"odhDO1", "odhDO2"});
// NOTE: If more than one result use session.query().
// If just one, use session.queryForObject()
Iterable<Distributor> distributors = session.query(Distributor.class, cypher, params);
for (Distributor distributor: distributors) {
System.out.println(distributor.getDISTID());
}
}
我的情况是通过 MySQL 检索数据并将其保存在 ArrayList 对象中,我需要将相同的对象放入密码查询中,运行 我的程序带有 junit 测试` public class DummyTest {
@Test
@Rollback(true)
public void checkMysqlConnection() {
Connection connection = null;
ResultSet tableDatasRsult = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "root");
String url = "127.0.0.1:3306/unicity";
connection = DriverManager.getConnection("jdbc:mysql://" + url, properties);
Assert.assertNotNull(connection);
String sqlq = "select DIST_ID,PV_DATE,POST_DATE from odh";
PreparedStatement preparedStatement = connection.prepareStatement(sqlq);
tableDatasRsult = preparedStatement.executeQuery();
ArrayList<OdhDO> odhDOs = new ArrayList<>();
while (tableDatasRsult.next()) {
OdhDO odhDO = new OdhDO();
odhDO.setDistId(tableDatasRsult.getLong("DIST_ID"));
odhDO.setPvDate(tableDatasRsult.getString("PV_DATE"));
odhDO.setPostDate(tableDatasRsult.getString("POST_DATE"));
odhDOs.add(odhDO);
Map<String, Object> params = new HashMap<>();
params.put(odhDO.getDistId().toString(), odhDO);
System.out.println(params);
}
} catch (ClassNotFoundException e) {
System.out.println("MySql Driver Class Not Found Exception");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Sql Exception");
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
@Rollback(true)
public void checkneo4jConnection(){
String URL= "http://localhost:7474";
Configuration configuration = new Configuration();
configuration.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver").setURI(URL).setCredentials("neo4j", "admin");
String cypher = "MATCH (Distributor) WHERE Distributor.DISTID IN {odhDOs} RETURN Distributor";
Map<String, Object> params = new HashMap<>();
List<String> odhDOs = new ArrayList<>();
params.put("odhDOs", odhDOs);
//add some names to the names list
//params.put(key, value)
}`
在上面的 checkMysqlConnection() 方法代码中,我检索了数据并将这些数据保存在 ArrayList odhDOs = new ArrayList<>() 对象,我需要在密码查询中传递这个 Arraylist 实例,但我不知道如何从这个 class 配置和连接 neo4j 以及如何在查询中传递这个 arraylist 实例 "MATCH (Distributor) WHERE Distributor.DISTID IN {odhDOs} RETURN Distributor" 请帮助使用 neo4j 配置 junit 以及如何在密码查询中传递数组实例..提前致谢...
如果您已根据 OGM reference guide 中的文档正确设置了所有内容,那么类似下面的内容应该可以工作:
@Test
public void checkneo4jConnection(){
Configuration configuration = new Configuration();
configuration.driverConfiguration()
.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
.setURI("http://localhost:7474")
.setCredentials("neo4j", "admin");
SessionFactory sessionFactory = new SessionFactory(configuration, "package.where.your.distributor.class.is");
Session session = sessionFactory.openSession();
String cypher = "MATCH (Distributor) WHERE Distributor.DISTID IN {odhDOs} RETURN Distributor";
Map<String, Object> params = new HashMap<>();
params.put("odhDOs", Arrays.asList(new String[] {"odhDO1", "odhDO2"});
// NOTE: If more than one result use session.query().
// If just one, use session.queryForObject()
Iterable<Distributor> distributors = session.query(Distributor.class, cypher, params);
for (Distributor distributor: distributors) {
System.out.println(distributor.getDISTID());
}
}