1.resultMap标签
1.1,使用场合
(1)数据库字段 和 实体类的属性名称 不一致时
(2)在处理数据库表间一对一关系的时候
(3)在处理数据库标间一对多关系的时候
1.2,具体例子
(1)当mybatis查询数据库数据,返回数据的字段名与其要封装的实体类的属性不一致时,不可以使用resultType属性,应该使用resultMap自定义映射关系
<resultMap id="map1" type="package.Student">
<id column="id" property="id"/>
<result column="sname" property="name"/>
<result column="sage" property="age"/>
<result column="sgender" property="gender"/>
<result column="stelephone" property="telephone"/>
</resultMap>
(2)当查询两张表数据,并且两张表间存在一对一关系的时候,例如学生表和身份证表,一个学生对应唯一的身份证,使用resultMap标签中的association标签
<resultMap id="map2" type="Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
<result column="telephone" property="telephone"/>
<association property="identity" javaType="Identity">
<id column="id" property="id"/>
<result column="sid" property="sid"/>
</association>
</resultMap>
(3)当查询的两张表存在一对多关系的时候,例如一个学生有多张银行卡,使用resultMap标签中的collection标签
<resultMap id="map2" type="Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
<result column="telephone" property="telephone"/>
<association property="identity" javaType="Identity">
<id column="id" property="id"/>
<result column="sid" property="sid"/>
</association>
<collection property="bandCards" ofType="BandCard">
<id column="bid" property="bid"/>
<result column="id" property="id"/>
<result column="bno" property="bno"/>
<result column="openbank" property="openBank"/>
<result column="opentime" property="openTime"/>
</collection>
</resultMap>
ps.注意: 在student实体类中要定义identity和bandcards属性,并且还要新建identity和bandcard实体类
ps: requestMap的用法和resultMap的用法一致