返回类型resultType:可以是实体类、java.lang.Integer、java.util.Map等类型,返回List集合,这写的是集合的泛型。如返回List<Integer>,则resultType为ava.lang.Integer
如:<select id="selectInformationList" parameterType="com.mz.information.vo.InformationVo"
resultType="com.mz.information.vo.InformationVo"></select>
resultMap:
resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。
resultMap包含的元素:
<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性--> <resultMap id="唯一的标识" type="映射的pojo对象"> <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" /> <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/> <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象"> <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/> <result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/> </association> <!-- 集合中的property须为oftype定义的pojo对象的属性--> <collection property="pojo的集合属性" ofType="集合中的pojo对象"> <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" /> <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" /> </collection> </resultMap>
如果collection标签是使用嵌套查询,格式如下:
<collection column="传递给嵌套查询语句的字段参数" property="pojo对象中集合属性" ofType="集合属性中的pojo对象" select="嵌套的查询语句" > </collection>
心得:1、resultmap映射查询出来的字段的顺序和这里无关,而和实体类的属性顺序有关。
比如Student类 有三个属性 id name age ,顺序id name age 。
<resultMap id="BaseResultMap" type="com.mz.information.entity.Student">
<id column="student_id" jdbcType="INTEGER" property="studentId" />
<result column="student_age" jdbcType="VARCHAR" property="studentAge" />
<result column="student_name" jdbcType="VARCHAR" property="studentName" />
</resultMap>
<select id="selectInformationList" parameterType="com.mz.information.vo.InformationVo"
resultMap="BaseResultMap"></select>
查出来的结果顺序是:id name age。而不是id age name
2.resultMap有id即可,里面的字段映射可以省略。因为mybatis会先去找实体类,再找映射。如上面的可以写成
<resultMap id="BaseResultMap" type="com.mz.information.entity.Student">
</resultMap>
<select id="selectInformationList" parameterType="com.mz.information.vo.InformationVo"
resultMap="BaseResultMap"></select>