MyBatis 参数索引超出范围
MyBatis Parameter index out of range
我正在尝试将此查询写入 MyBatis
(select * from table1 where field1 like '%"name":"appname"%' limit 1)
union all
(select * from table2 where field1 like '%"name":"appname"%' limit 1)
limit 1
所以我创建了这个注释:
@Select({
"(select * from table1 where field1 like '%\"name\":\"#{name,jdbcType=VARCHAR}\"%' limit 1)",
"union all",
"(select * from table2 where field1 like '%\"name\":\"#{name,jdbcType=VARCHAR}\"%' limit 1)",
"limit 1"
})
@Results({
@Result(column="ID", property="id", jdbcType=JdbcType.INTEGER, id=true)
})
Object hasName(@Param("name")String name);
但是当我 运行 它时,我得到了这个异常:
Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.String, jdbcType=VARCHAR, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType VARCHAR . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
我可以这样映射注释吗?还是我遗漏了什么?我不想为此使用 XML。
我决定更改
中的部分查询
field1 like '%\"name\":\"#{name,jdbcType=VARCHAR}\"%'
到
field1 like #{name,jdbcType=VARCHAR}
并在 Java
中更改参数名称
parameter = "%\"name\":\"" + parameter + "\"%";
我遵循了 post 我发现
的解释
'%'||${specific_string}||'%'
or
#{do_it_in_java}
or
someFunctionOrProc(#{specific_string})
where the function/proc will
handle it. If you use this a lot in multiple SqlMaps then I might go
down this road.
不完全是你的情况,但如果你在查询中注释掉一行,并且该行包含参数,如:
-- AND (neId=#{neId}))
试试这个:
field1 like ${name}
field2 like ${name}
我正在尝试将此查询写入 MyBatis
(select * from table1 where field1 like '%"name":"appname"%' limit 1)
union all
(select * from table2 where field1 like '%"name":"appname"%' limit 1)
limit 1
所以我创建了这个注释:
@Select({
"(select * from table1 where field1 like '%\"name\":\"#{name,jdbcType=VARCHAR}\"%' limit 1)",
"union all",
"(select * from table2 where field1 like '%\"name\":\"#{name,jdbcType=VARCHAR}\"%' limit 1)",
"limit 1"
})
@Results({
@Result(column="ID", property="id", jdbcType=JdbcType.INTEGER, id=true)
})
Object hasName(@Param("name")String name);
但是当我 运行 它时,我得到了这个异常:
Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.String, jdbcType=VARCHAR, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType VARCHAR . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
我可以这样映射注释吗?还是我遗漏了什么?我不想为此使用 XML。
我决定更改
中的部分查询field1 like '%\"name\":\"#{name,jdbcType=VARCHAR}\"%'
到
field1 like #{name,jdbcType=VARCHAR}
并在 Java
中更改参数名称parameter = "%\"name\":\"" + parameter + "\"%";
我遵循了 post 我发现
的解释
'%'||${specific_string}||'%'
or
#{do_it_in_java}
or
someFunctionOrProc(#{specific_string})
where the function/proc will handle it. If you use this a lot in multiple SqlMaps then I might go down this road.
不完全是你的情况,但如果你在查询中注释掉一行,并且该行包含参数,如:
-- AND (neId=#{neId}))
试试这个:
field1 like ${name}
field2 like ${name}