Mybatis源码分析--返回值ResultType和ResultMap

这一篇博客我们来介绍一下Mybatis执行sql语句返回的结果值的到实体对象的映射机制。首先ResultType和ResultMap的使用方式是不同的。

ResultType的使用方式:

resultType的值为实体类

<select id="getUser" parameterType="int" resultType="com.tianjunwei.learn.learn1.entity.User">
         select * from users where id=#{id}
 </select>
ResultMap的使用方式:

首先要定义resultMap的对应关系

<resultMap type="com.tianjunwei.learn.learn1.entity.User" id="user">    
     	<id column="id" property="id" javaType="int" jdbcType="INTEGER"></id>
     	<result column="name" property="names" javaType="string" jdbcType=VARCHAR/>
     	<result column="age" property="age" javaType="int" jdbcType="INTEGER"/>
</resultMap>
接下来直接使用id值resultMap="user"

<select id="getById" parameterType="int" resultMap="user">
     	select * from users where id=#{id} and 1=1
</select>

以上就完成了resultType和resultMap的使用。其实mybatis的默认实现机制是首先会自动使用resultType模式,当表中的字段值和实体中的名称不一致时再使用ResultMap方式,如上面的实体User中的names和表中字段name中的对应关系,所以使用resultType方式实体类中User的names属性值为空,而使用resultMap方式实体类中就会有值。

当强制使用resultMap而不是ResultType是需要进行如下配置:增加了autoMapping=false,默认值为null

<resultMap type="com.tianjunwei.learn.learn1.entity.User" id="user" autoMapping="false">    
     	<id column="id" property="id" javaType="int" jdbcType="INTEGER"></id>
     	<result column="name" property="names" javaType="string" jdbcType="VARCHAR"/>
     	<result column="age" property="age" javaType="int" jdbcType="INTEGER"/>
     </resultMap>

接下来我们介绍一下其实现机制:

其具体实现是在DefaultResultSetHandler中,在获取每行数据时会根据resultType或者resultMap来给对象赋值,返回resultObject。

private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
    final ResultLoaderMap lazyLoader = new ResultLoaderMap();
    Object resultObject = createResultObject(rsw, resultMap, lazyLoader, null);
    if (resultObject != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
      final MetaObject metaObject = configuration.newMetaObject(resultObject);
      boolean foundValues = !resultMap.getConstructorResultMappings().isEmpty();
	  //判断是否可以使用resultType,如果不做任何额外设置,返回true
      if (shouldApplyAutomaticMappings(resultMap, false)) {
        foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
      }
	  //使用resultMap方式
      foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
      foundValues = lazyLoader.size() > 0 || foundValues;
      resultObject = foundValues ? resultObject : null;
      return resultObject;
    }
    return resultObject;
  }




发布了391 篇原创文章 · 获赞 443 · 访问量 144万+
展开阅读全文

mybatis嵌套查询,一个resultMap中有多个association标签,怎么写sql语句?

03-08

# 1. 一下是我的实体类 1. private int id; 1. private Users users;//房屋持有者 1. private Types types;//房屋类型 1. private String title; 1. private String description; 1. private double price; 1. private Date pubdate;//发布日期 1. private int floorage;//建筑面积 1. private int contact;//联系方式 1. private Street street;//房屋所属街区 1. public int getId() { 1. return id; 1. } 1. public void setId(int id) { 1. this.id = id; 1. } 1. public String getTitle() { 1. return title; 1. } 1. public void setTitle(String title) { 1. this.title = title; 1. } 1. public String getDescription() { 1. return description; 1. } 1. public void setDescription(String description) { 1. this.description = description; 1. } 1. public double getPrice() { 1. return price; 1. } 1. public void setPrice(double price) { 1. this.price = price; 1. } 1. public Date getPubdate() { 1. return pubdate; 1. } 1. public void setPubdate(Date pubdate) { 1. this.pubdate = pubdate; 1. } 1. public int getFloorage() { 1. return floorage; 1. } 1. public void setFloorage(int floorage) { 1. this.floorage = floorage; 1. } 1. public int getContact() { 1. return contact; 1. } 1. public void setContact(int contact) { 1. this.contact = contact; 1. } 1. public Users getUsers() { 1. return users; 1. } 1. public void setUsers(Users users) { 1. this.users = users; 1. } 1. public Types getTypes() { 1. return types; 1. } 1. public void setTypes(Types types) { 1. this.types = types; 1. } 1. public Street getStreet() { 1. return street; 1. } 1. public void setStreet(Street street) { 1. this.street = street; 1. } 1. ``` ``` ##下面是对应的xml文件,以及sql语句 ``` <mapper namespace="com.rhit.dao"> <resultMap id="HouseMap" type="com.rhit.entity.House"> <id property="id" column="ID" /> <result property="title" column="TITLE" /> <result property="description" column="DESCRIPTION" /> <result property="price" column="PRICE" /> <result property="pubdate" column="PUBDATE" /> <result property="floorage" column="FLOORAGE" /> <result property="contact" column="CONTACT" /> <association property="types" javaType="Types"> <id property="id" column="ID" /> <result property="name" column="NAME" /> </association> <association property="street" javaType="Street"> <id property="id" column="ID" /> <result property="name" column="NAME" /> </association> <association property="users" javaType="Users"> <id property="id" column="ID" /> <result property="name" column="NAME" /> <result property="password" column="PASSWORD" /> <result property="telphone" column="TELEPHONE" /> <result property="username" column="USERNAME" /> <result property="isadmin" column="ISADMIN" /> </association> </resultMap> <select id="getHouse" resultMap="HouseMap"> select * from house h,users u,types t,street s where u.id=h.user_id and t.id=h.type_id and s.id=h.street_id </select> </mapper> ``` ##下面是调用测试 public class Test { public static void main(String[] args) { SqlSession session=MyBatisUtil.getSession(); List<House> list=session.selectList("getHouse"); System.out.println(list.get(0).getStreet().getName()); System.out.println(list.get(0).getTypes().getName()); System.out.println(list.get(0).getUsers().getName()); } } ##问题 1.测试结果只获取到了user的名字,而types和street的名字跟user的名字一样? 2.一个resultMap中可以有多个association么?这种多对多的关系该怎么处理? 3.一个resultMap可以有多个collection么?怎么写sql语句呢? 求大神么解答,在线等,先谢过! 问答

用mybaits的resultMap查询数据时,为什么只能返库的最后一条数据

06-17

<!-- 根据商品的id来查询商品信息和图片,通过resultMap来进行映射 --> <select id="selectItemAndPic" resultMap="itemNumPicMap" > select tb_item.*, tb_item_pic.id as itemPic_id, tb_item_pic.item_id, tb_item_pic.url from tb_item, tb_item_pic where tb_item.id=tb_item_pic.item_id and tb_item.id in <foreach item="ids" index="index" collection="array" open="(" separator="," close=")"> #{ids} </foreach> </select> 这个是定义的resultMap <resultMap id="itemNumPicMap" type="cn.wow.ssm.vo.ItemNumPic"> <association property="item" javaType="cn.wow.ssm.po.Item"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="price" property="price"/> <result column="detail" property="detail"/> <result column="createTime" property="createTime"/> <result column="discount" property="discount"/> <result column="keyWords" property="keyWords"/> <result column="area" property="area"/> <result column="saleCount" property="saleCount"/> <result column="classify_id" property="classify_id"/> <result column="repertory" property="repertory"/> <result column="recommend" property="recommend"/> </association> <association property="itemPic" javaType="cn.wow.ssm.po.ItemPic"> <id column="itemPic_id" property="id"/> <result column="item_id" property="item_id"/> <result column="url" property="url"/> </association> </resultMap> 这个是mapper.java中的方法 public List<ItemNumPic> selectItemAndPic(Integer []ids) throws Exception; 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览