MyBatis连接Neo4j问题记录:mapper参数传递(节点标签作为参数)



  • 上一篇中UserMapper.xml中与传参有关的部分是这样的:
<select id="countUser" parameterType="int" resultMap="userMap">
    MATCH ()-[p]->(n:Movie) where id(n)=#{id} RETURN id(n) as id,n.title as title,count(p) as degree
</select>
  • 对应的UserMapper.java中函数声明如下:
public UserBean countUser(int id) throws Exception;
  • id作为参数传入CQL语句中,因为当时想做id++操作。
  • 后面我换了个思路,想直接一次性把Movie标签下所有节点的信息一次都取出来,也就不用循环了。
  • 方法是把节点的标签作为参数进行传递,下面是弯路

第一个版本(去掉where,把Movie改成参数):

<select id="countUser" parameterType="String" resultMap="userMap">
    MATCH ()-[p]->(n:#{label}) RETURN id(n) as id,n.title as title,count(p) as degree
</select>
public List<UserBean> countUser(String label) throws Exception;
  • 报错了,参数传不过去。。。
  • 可能参数应该只能从where子句里面传入~~~

第二个版本(加上where,参数放到where后面):

<select id="countUser" parameterType="String" resultMap="userMap">
    MATCH ()-[p]->(n) where (n:#{label}) RETURN id(n) as id,n.title as title,count(p) as degree
</select>
public List<UserBean> UserBean countUser(String label) throws Exception;
  • 呃…跟上一版错的一模一样
  • 可能(n:#{label})这种模式就是传不进去,于是去找获取节点标签的函数,在neo4j里面找到一个:labels(Node)


------------第三个版本(替换(n:#{label})):

<select id="countUser" parameterType="String" resultMap="userMap">
    <!--#{label}的中括号不能丢 -->
    MATCH ()-[p]->(n) where labels(n)=[#{label}] RETURN id(n) as id,n.title as title,count(p) as degree
</select>
public List<UserBean> UserBean countUser(String label) throws Exception;
  • 完成!
                                </div>
原文地址:https://www.cnblogs.com/jpfss/p/11281042.html