解决数据库字段和pojo实体类属性名字不同的问题
案例:实体类User的其中一个属性为:password,但是在数据库中它的字段名为:pwd
解决方法:
- 起别名
select pwd as password from mybatis.user where id = #{id}
或者用结果集映射
1. ResultMap(结果集映射)
在映射器下加入这个标签,可以直接映射数据库字段和pojo
<resultMap id="UserMap" type="User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<!--s通过id查询用户-->
<select id="getUserById" resultMap="UserMap" parameterType="int">
select * from mybatis.user where id = #{id}
</select>
我们可以直接把pojo的属性和数据库做映射,type是映射类型,column中是数据库字段,property是pojo属性。
在查询中的返回值类型中直接选择resultMap就可以了
如果世界总是这么简单就好了。mybatis3官网如此说到。
Comments | NOTHING