mybatis多表之间的查询resultMap

<?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">
  <!--  
  如果使用xml的方式进行接口映射
  namespace的值必须是接口的全路径
   -->
 <mapper namespace="com.et.mybatis.resultMapping.ResultMapper">
    <!-- 多对一的映射 -->
    <resultMap type="com.et.mybatis.resultMapping.Arctice" id="arctVar">
    	<result column="aid" property="id"/>
    	<result column="title" property="title"/>
    	<result column="content" property="content"/>
    	<!-- 
    		association应用于 多对一的映射
    		也就是说 如果一个类中存在一个对象  如果需要将select语句查询的列 映射到该对象中 就需要使用association标签
    		property 指定的 arctice对象中的一个对象属性名称 javaType表示你需要指定他的数据类型
    	 -->
    	<association property="user" javaType="com.et.mybatis.resultMapping.UserInf">
    	   <result column="uid" property="id"/>
    	   <result column="name" property="name"/>
    	</association>
    </resultMap>
    <!-- 查询的时候 如果需要进行特殊的映射  必须使用 resultMap 引用自定义的映射类型-->
    <select id="selectArcticle" resultMap="arctVar">
      select a.id as aid,u.id as uid,a.title,a.content,u.name from arcticle a inner join userinfo u on
		a.userid=u.id;
    </select>
    
    <!-- 一对多映射  在一的一方 会存在一个集合 应该对集合的数据进行规则的匹配 -->
    <resultMap type="com.et.mybatis.resultMapping.UserInf" id="userVar">
    	<result column="id" property="id"/>
    	<result column="name" property="name"/>
    	<!-- 
    		因为集合的数据 是多条数据返回 所以需要将当前对象产生的主键 当成 另外一个查询的条件
    		column 表示将查询当前对象的列的数据 当做另一个查询的条件传入
    		select 表示需要查询的集合的select声明
    		 autoMapping=true表示数据库的列自动匹配到对应的属性上
    	 -->
    	<collection property="arcs" column="id" 
    		select="com.et.mybatis.resultMapping.ResultMapper.selectArcticleByUserId" autoMapping="true">
               <!--select调用必须加上命名空间-->
    	</collection>
    </resultMap>
    <!-- 使用 resultMap来指定映射规则-->
    <select id="selectUserInfo" resultMap="userVar">
      select * from userinfo where id=#{id}
    </select>
    <select id="selectArcticleByUserId" resultType="com.et.mybatis.resultMapping.Arctice" >
      select * from arcticle where userid=#{userid}
    </select>
    
    
</mapper>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值