Skip to content
On this page

Param Loop

There is a big difference between ngbatis and mybatis in the loop part, and the loop syntax of Beetl is used. Please refer to official documents for details.【1.10 Loop statement】 Due to the difference in configuration of Beetl in ngbatis, the <% %> will be replaced by @ \n, for example:

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

Looping in Map , which can be used for dynamic query

  • PersonDao.java

    java
    // org.springframework.data.repository.query.Param
        // person: { "name": "Diana", "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>

Looping in List , which can be used for batch processing

  • PersonDao.java

    java
    // org.springframework.data.repository.query.Param
        // personList: [{"gender":"F","name":"Diana"},{"gender":"M","name":"Tom"},{"gender":"F","name":"Jerry"}]
        void insertPersonList( @Param("personList") List<Person> personList );
  • The parameter is :

    json
    :param personList => [{"gender":"F","name":"Diana"},{"gender":"M","name":"Tom"},{"gender":"F","name":"Jerry"}]
  • PersonDao.xml

    xml
    <insert id="insertPersonList">
            @for ( p in personList ) {
              INSERT VERTEX `person` ( name, gender ) VALUES '${ p.name }' : ( '${ p.name }', '${ p.gender }' );
            @}
        </insert>
  • The statements to be executed are:

    sql
    INSERT VERTEX `person` ( name, gender ) VALUES 'Diana' : ( 'Diana', 'F' );
        INSERT VERTEX `person` ( name, gender ) VALUES 'Tom' : ( 'Tom', 'M' );
        INSERT VERTEX `person` ( name, gender ) VALUES 'Jerry' : ( 'Jerry', 'F' );