67,515
社区成员
发帖
与我相关
我的任务
分享
<parameterMap type="java.util.HashMap" id="countParam">
<parameter property="A" javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"/>
<parameter property="B" javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"/>
<parameter property="C" javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"/>
<parameter property="D" javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"/>
</parameterMap>
<select id="countByMap" parameterMap="countParam" resultType="java.lang.Integer">
select count(*) from T
where 1 = 1
<if test="A!= null" >
and T.A = #{A,jdbcType=VARCHAR}
</if>
<if test="B!= null" >
and T.B = #{B,jdbcType=VARCHAR}
</if>
<if test="C!= null" >
and T.C = #{C,jdbcType=VARCHAR}
</if>
<if test="D!= null" >
and T.D like #{D,jdbcType=VARCHAR}
</if>
</select>
<select id="countByMap" parameterMap="countParam" resultType="java.lang.Integer">
select count(*) from T
<where>
<if test="A!= null">
and T.A = #{A,jdbcType=VARCHAR}
</if>
<if test="B!= null">
and T.B = #{B,jdbcType=VARCHAR}
</if>
<if test="C!= null">
and T.C = #{C,jdbcType=VARCHAR}
</if>
<if test="D!= null">
and T.D like #{D,jdbcType=VARCHAR}
</if>
</where>
</select>
为什么不这么写?
还有你的parameterMap用HashMap就可以了,你自定义的Map在你没参数传进来的时候,肯定null,mybatis用反射查找参数的子厚自然报错,或者你没参数的时候也传个没有key-value的对象进来。
### Error querying database. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType VARCHAR . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 列索引が無効です。
### The error may exist in jp/co/tomoeshokai/kaigai/common/dao/KaiMsKamokuMapper.xml
### The error may involve jp.co.tomoeshokai.kaigai.common.dao.KAI_MS_KAMOKU.countParam
### The error occurred while setting parameters
### SQL: select count(*) from KAI_MS_KAMOKU where 1 = 1