组团学

MyBatis-resultMap参数

阅读 (838335)

1、resultMap概述

resultMap 标签可以建立查询的字段名和实体类的属性名称不一致时建立对应关系。从而实现封装。
在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。

2、需求分析

查询数据库表(user)中的所有记录

3、案例实现

3.1、修改IUserDao.xml文件

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.tianyi.dao.IUserDao"> <!-- 配置查询所有操作 --> <select id="findAll" resultMap="userMap"> select * from user </select> <resultMap id="userMap" type="com.tianyi.javabean.User"> <id column="id" property="uid"></id> <result column="username" property="uname"></result> <result column="birthday" property="ubirthday"></result> <result column="sex" property="Sex"></result> <result column="address" property="uaddress"></result> </resultMap> </mapper>

注:

resultMap中的属性:

type 属性:指定实体类的全限定类名

id 属性:给定一个唯一标识,是给查询 select 标签引用用的。

resultMap中的标签:

id 标签:用于指定主键字段

result 标签:用于指定非主键字段
column 属性:用于指定数据库列名

​ property 属性:用于指定实体类属性名称

注意:

select标签中的属性resultType,要改为resultMap。

需要 登录 才可以提问哦