在AccountMapper.xml中如下书写:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zte.myproject1.mapper.AccountMapper">
<resultMap id="gainAccount" type="com.zte.myproject1.entity.Account">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR" />
<result property="money" column="money" jdbcType="DOUBLE" />
</resultMap>
<select id="getAccount" parameterType="Integer" resultType="com.zte.myproject1.entity.Account">
select * from account
where
<if test="id != null">
id = #{id}
</if>
</select>
</mapper>
在Account.java中漏掉参数注解:
错误:
@Mapper
public interface AccountMapper {
Account getAccount( int id);
}
正确:
@Mapper
public interface AccountMapper {
Account getAccount(@Param("id") int id);
}
本文详细解析了在MyBatis中如何正确使用参数注解@Param,以确保在AccountMapper接口中getAccount方法能够准确接收并处理传入的ID参数。通过对比错误和正确的代码示例,读者可以了解到在XML映射文件中定义结果映射和选择查询时,如何与Java接口中的方法参数进行匹配。
998

被折叠的 条评论
为什么被折叠?



