java Mybatis Xml Sql 中 IN 的两种写法: #{item} => #{collection[${index}]}

夏宏旷
2023-12-01

今天使用到一个开源框架JeePlus, 它里边的 Page 是自己封装的
在获取 count 时, 他需要对 已经生成的sql 做一层count() 封装

public class SQLHelper {
	public static int getCount(
		final String countSql = "select count(1) from (" + countsql + ") tmp_count";
	}
}

然后通过下面方法 对SQL参数(?) 设值

public static void setParameters(
}

然后现在我需要对他xml自动生成的sql添加一个 IN 的操作
如下:

<select id="findList" resultType="XX" >
		SELECT
			<include refid="smpStoreOrderColumns"/>
		FROM smp_store_order a
		<where>
			a.create_by in
			<foreach collection="underUserIds" item="id" index="index" separator="," open="(" close=")">
				#{id}
			</foreach>
			and a.del_flag = '0'
			${dataScope}
		</where>
	</select>

跟平时一样, 我使用了 #{id} 进行 值的获取, 但是在这个框架中,他是不支持的:


Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named '__frch_id_0' in 'class com.jeeplus.modules.tongji.order.entity.SmpStoreOrder'
	at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:375)
	at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
	at com.jeeplus.core.persistence.interceptor.SQLHelper.setParameters(SQLHelper.java:79)
	at com.jeeplus.core.persistence.interceptor.SQLHelper.getCount(SQLHelper.java:131)
	at com.jeeplus.core.persistence.interceptor.PaginationInterceptor.intercept(PaginationInterceptor.java:104)
// 去 SQLHelper.java:79 看看
// propertyName = __frch_id_0
value = metaObject == null ? null : metaObject.getValue(propertyName);
// 原来这里解析不了我上边写的#{id}  但是我又不知道为什么, 进去看看
// 此处省略一万字

切换一下写法

<foreach collection="underUserIds" item="id" index="index" separator="," open="(" close=")">
	#{id}
</foreach>
<foreach collection="underUserIds" item="id" index="index" separator="," open="(" close=")">
	#{underUserId[${index}]}
</foreach>

成功执行 !

 类似资料: