MyBatis 将配置文件中的每一个mapper 节点抽象为一个 Mapper 接口,而这个接口中声明的方法和跟 节点中的<select|update|delete|insert> 节点项对应,即<select|update|delete|insert> 节点的id值为Mapper 接口中的方法名称。
resultType
当使用resultType做SQL语句返回结果类型处理时,对于SQL语句查询出的字段在相应的实体类(entity|pojo)中必须有和它相同的字段对应,而resultType中的内容就是entity在本项目中的位置。因此对于单表查询的话用resultType是最合适的。
resultMap
若是一对多,使用内连接查询,则很可能出现查询出的字段有重复。
所以使用resultMap做SQL语句返回结果类型处理时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。
<!-- 订单查询关联用户的resultMap
将整个查询的结果映射到cn.itcast.mybatis.po.Orders中
-->
<resultMap type="cn.kgc.mybatis.pojo.Orders" id="OrdersUserResultMap">
<!-- 配置映射的订单信息 -->
<!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id
column:订单信息的唯 一标识 列
property:订单信息的唯 一标识 列所映射到Orders中哪个属性
-->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
</resultMap>