mybatis-mapper icon indicating copy to clipboard operation
mybatis-mapper copied to clipboard

There is a problem with parameter matching with a special name and list.

Open bestbykim opened this issue 2 years ago • 0 comments

There is a problem with parameter matching with a special name and list.

I wrote it to execute a multi-query.

The same where statement was used twice.

The condition has some text and some list.

after excute foreach, some parameter(suffix is 'name') have disappeared.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">  
    <sql id="test1">
        <if test="item_name!=''">
        and item_name = #{item_name}
        </if>
        <foreach collection="main_name" item="name"  open="and (" close=")" index="index" separator="or">
        main_name like CONCAT('%', #{name}, '%')
        </foreach>
        <if test="item_name!=''">
        and item_name = #{item_name}
        </if>
    </sql>

    <sql id="test2">
        <if test="category!=''">
        and category = #{category}
        </if>
        <foreach collection="main_name" item="name"  open="and (" close=")" index="index" separator="or">
        main_name like CONCAT('%', #{name}, '%')
        </foreach>
        <if test="category!=''">
        and category = #{category}
        </if>
    </sql>

    <sql id="test3">
        <if test="cate_name!=''">
        and cate_name = #{cate_name}
        </if>
        <foreach collection="main_na" item="name"  open="and (" close=")" index="index" separator="or">
        main_name like CONCAT('%', #{name}, '%')
        </foreach>
        <if test="cate_name!=''">
        and cate_name = #{cate_name}
        </if>
    </sql>
</mapper>
const mybatisMapper = require('mybatis-mapper');
mybatisMapper.createMapper(['./config/test.xml']);

var format = { language: 'sql' };

var param = {
    item_name: 'a',
    main_name: [ 'b', 'c' ],
}

var param2 = {
    category: 'a',
    main_name: [ 'b', 'c' ],
}

var param3 = {
    cate_name: 'a',
    main_na: [ 'b', 'c' ],
}


var sql1 = mybatisMapper.getStatement('test', 'test1', param, format);
var sql2 = mybatisMapper.getStatement('test', 'test2', param2, format);
var sql3 = mybatisMapper.getStatement('test', 'test3', param3, format);

console.log(sql1)
console.log('--------------------------------------')
console.log(sql2)
console.log('--------------------------------------')
console.log(sql3)

result is

and item_name = 'a'
and (
  main_name like CONCAT('%', 'b', '%')
  or main_name like CONCAT('%', 'c', '%')
)
--------------------------------------
and category = 'a'
and (
  main_name like CONCAT('%', 'b', '%')
  or main_name like CONCAT('%', 'c', '%')
)
and category = 'a'
--------------------------------------
and cate_name = 'a'
and (
  main_name like CONCAT('%', 'b', '%')
  or main_name like CONCAT('%', 'c', '%')
)

Parameters are missing in first and third result. Parameter(name ends with 'name') is missing....

Only the second result is a success.

I think that "if test" has some bug. If remove the "if test" in id=test1 (like below), return the collect result.

<sql id="test1">
and item_name = #{item_name}
<foreach collection="main_name" item="name"  open="and (" close=")" index="index" separator="or">
main_name like CONCAT('%', #{name}, '%')
</foreach>
and item_name = #{item_name}	
</sql>	

bestbykim avatar Nov 11 '22 09:11 bestbykim