使用mybatis进行数据库连接时对于select语句返回结果的处理方式有两种:resultMap和resultType。
- resultType
使用resultType时,对于select语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType中的内容就是pojo在本项目中的位置。
具体实例
<select id="findAdmin" resultType="test.bean.Admin" >
select * from tb_admin where id=#{id}
</select>
- resultMap
当使用resultMap做select语句返回结果类型处理时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。
当我们遇到在实体类中的字段名和数据库中的字段名不一致时,resultMap就可以帮助我们将数据库字段名和本类字段名相应射。
具体实例
<resultMap id="UserType" type="test.entity.User">
<!-- id 唯一标识符
cloumn 简单说就是数据库表的字段名
property 就是User类中和要数据库表相对应的属性名
-->
<id property="id" column="id"></id>
<result property="username" column="account"></result>
</resultMap>
<select id="findUser" resultMap="UserType" >
select id,account,password from tb_user where account=#{user.account} and password=#{user.password}
</select>
注意:resultType跟resultMap不能同时存在。