我正在尝试将此查询写入 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.
关于java - MyBatis 参数索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884936/