Querying db for a list of records using mybatis 原因:An unexpected token "IN" was found following
Querying db for a list of records using mybatis causes: An unexpected token "IN" was found following
知道 mybatis 映射器可能有什么问题吗?
手动 运行 这个 SQL,我得到了预期的结果..
SELECT * FROM TABLE_NAME WHERE ID = '304ed0ea-07c1-4261-bb0f-b44036f285e6' AND RUN_ID IN ('be7e0d61-bdb4-4341-ab35-fd5007e99e58');
并且 SQL 在我的映射器中看起来一样 xml ..
<select id="selectByIdAndRunIds" parameterType="java.util.HashMap" resultMap="result">
SELECT * FROM TABLE_NAME
WHERE ID = #{id} AND RUN_ID IN
<foreach item="item" collection="runIds" open="(" separator="," close=")">
#{item}
</foreach>
</select>
Java..
public List<SomeObject> selectByIdAndRunIds(final String id, final List<String> runIds) {
SqlSession session = MyBatisSqlSession.getInstance().getSqlSessionFactory().openSession(true);
Map<String, Object> input = new HashMap<>();
input.put("id", id);
input.put("runIds", runIds);
List<SomeObject> result = session.selectList("selectByIdAndRunIds", input);
session.close();
return result;
}
这是我得到的错误:
### Error querying database.
Cause: com.ibm.db2.jcc.am.SqlSyntaxErrorException:
An unexpected token "IN" was found following "= ? AND RUN_ID".
Expected tokens may include: "<boolean_factor>"..
SQLCODE=-104, SQLSTATE=42601, DRIVER=4.24.92
runIds
是一个空列表,即 runIds.size() == 0
。
添加逻辑以在 runIds.size() == 0
时不执行查询
知道 mybatis 映射器可能有什么问题吗?
手动 运行 这个 SQL,我得到了预期的结果..
SELECT * FROM TABLE_NAME WHERE ID = '304ed0ea-07c1-4261-bb0f-b44036f285e6' AND RUN_ID IN ('be7e0d61-bdb4-4341-ab35-fd5007e99e58');
并且 SQL 在我的映射器中看起来一样 xml ..
<select id="selectByIdAndRunIds" parameterType="java.util.HashMap" resultMap="result">
SELECT * FROM TABLE_NAME
WHERE ID = #{id} AND RUN_ID IN
<foreach item="item" collection="runIds" open="(" separator="," close=")">
#{item}
</foreach>
</select>
Java..
public List<SomeObject> selectByIdAndRunIds(final String id, final List<String> runIds) {
SqlSession session = MyBatisSqlSession.getInstance().getSqlSessionFactory().openSession(true);
Map<String, Object> input = new HashMap<>();
input.put("id", id);
input.put("runIds", runIds);
List<SomeObject> result = session.selectList("selectByIdAndRunIds", input);
session.close();
return result;
}
这是我得到的错误:
### Error querying database.
Cause: com.ibm.db2.jcc.am.SqlSyntaxErrorException:
An unexpected token "IN" was found following "= ? AND RUN_ID".
Expected tokens may include: "<boolean_factor>"..
SQLCODE=-104, SQLSTATE=42601, DRIVER=4.24.92
runIds
是一个空列表,即 runIds.size() == 0
。
添加逻辑以在 runIds.size() == 0