Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bestbykim opened this issue Nov 11, 2022 · 0 comments

Comments

@bestbykim
Copy link

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>	
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant