问:明明我输入的是Integer类型,为什么结果是编译器认为我输入的是String类型(Parameters: (String))
------------------HeroMapper.xml代码------------------
<!--配置public void getHeroById(@Param(value="id") Integer id);-->
<select id="getHeroById" parameterType="Integer" resultMap="HeroMap">
select * from `hero`
<where>
<if test="id != null and id != ''">
and `id` = #{id}
</if>
<if test="id = null or id = ''">
and 1=1
</if>
</where>
</select>
-----------------测试代码-------------------
@Test
public void getHeroById(){
List<Hero> heroById = heroMapper.getHeroById(1);
for (Hero hero : heroById) {
System.out.println(hero);
}
if(sqlSession != null){
sqlSession.close();
}
}
测试代码时,我传入一个int类型的参数1,
预期结果为:
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a79c292]
==> Preparing: select * from hero WHERE id = ?
==> Parameters: 1(Integer)
<== Total: 1
实际结果为:
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a79c292]
==> Preparing: select * from hero WHERE id = ? and 1=1
==> Parameters: (String)
<== Total: 0
解决方法:删除了第二个标签
HeroMapper.xml代码
<!--配置public void getHeroById(@Param(value="id") Integer id);-->
<select id="getHeroById" parameterType="Integer" resultMap="HeroMap">
select * from `hero`
<where>
<if test="id != null and id != ''">
and `id` = #{id}
</if>
</where>
</select>
测试后,预期结果与实际结果一致
原因未知