Skip to content
On this page

参数循环

遍历的部分跟 MyBatis 的差异比较大,使用了 Beetl 的遍历语法。具体可参考官方文档【1.10 循环语句

因配置的差异,文档中如涉及界定符,则由文档中的 <% %> 替换成 @ \n,如:

diff
- <%for ( item in list ) { 
-                         
- } %>                
+ @for ( item in list ) {
+                       
+ @}

对 Map 的遍历,可用于动态查询

  • PersonDao.java

    java
    // org.springframework.data.repository.query.Param
    // person: { "name": "张三", "gender": "F" }
    Person selectByPerson( @Param("p") Person person );
  • PersonDao.xml

    xml
    <select id="selectByPerson">
        MATCH (n: person)
        WHERE 1 == 1 
        @for ( entry in p ) {
          @if ( isNotEmpty( entry.value ) ) {
            AND n.person.`${ entry.key }` == $p.${ entry.key }
          @}
        @}
        RETURN n
        LIMIT 1
    </select>

对 List 遍历,可用于批处理

  • PersonDao.java

    java
    // org.springframework.data.repository.query.Param
        // personList: [{"gender":"F","name":"张三"},{"gender":"M","name":"王五"},{"gender":"F","name":"赵六"}]
        void insertPersonList( @Param("personList") List<Person> personList );
  • 参数为:

    json
    :param personList => [{"gender":"F","name":"张三"},{"gender":"M","name":"王五"},{"gender":"F","name":"赵六"}]
  • PersonDao.xml

    xml
    <insert id="insertPersonList">
            @for ( p in personList ) {
              INSERT VERTEX `person` ( name, gender ) VALUES '${ p.name }' : ( '${ p.name }', '${ p.gender }' );
            @}
        </insert>
  • 执行的语句为:

    sql
    INSERT VERTEX `person` ( name, gender ) VALUES '张三' : ( '张三', 'F' );
        INSERT VERTEX `person` ( name, gender ) VALUES '王五' : ( '王五', 'M' );
        INSERT VERTEX `person` ( name, gender ) VALUES '赵六' : ( '赵六', 'F' );