解决属性名和字段名不一致的问题
文章目录
数据库中的字段
public class User{
private int id;
private String name;
private String password;
}测试查出来password为null
//select id,name,pwd from mybatis.user where id=#{id}解决办法:
起别名
<select id="getUserById" resultType="com.jialidun.pojo.User"> select id,name,pwd as password from mybatis.user where id=#{id} </select>
resultMap
结果集映射
id name pwd
id name password<resultMap id="UserMap" type="User">
<!--column数据库中的字段,property实体类中的属性-->
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserById" resultMap="UserMap">
select id,name,pwd from mybatis.user where id=#{id}
</select>- resultMap 元素是 MyBatis 中最重要最强大的元素。
- ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论