MyBatis一对多(预习)

u类定义
一方:

publicclass UserObj {

   privateint id;

   privateString name;

  private int age;

 

  private List<ResultObj> resultList = newArrayList();

}

u多方:

publicclass ResultObj {

   privateint id;

   privateint point;

     private UserObj user;

}


<!-- 定义类别名 -->

 <typeAlias alias="users"type="com.map.UserObj"/>

 <typeAlias alias="results"type="com.map.ResultObj"/>

<!-- 定义按用户ID查询用户。里面包括成绩集合 -->

 <select id="findById" resultMap="user_Result">

   select * from t_user where t_id=#{id}

 </select>


定义用户结果集

<!--用户表类属性映射,extends表示可继承userMap所定义的映射 -->
<resultMap id="user_Result" type="user" extends="userMap">

     <!--属性名resultList 类型为List集合 该集合是根据UserObj的t_id进行ResultObj查询
           column是本表要传入的select语句块中的参数,集合查询的定义为resultByUid -->
    <association property="resultList" column="t_id"select="resultByUid"/>
  </resultMap>

u <!-- 定义按用户ID查询成绩,参数来自用户对象的ID -->

  <select id="resultByUid" resultMap="resultData">

   select* from t_result where uid=#{uid}

 </select>


u<!-- 定义按成绩ID查询成绩 -->

<selectid="findResultById" resultMap="rm" parameterClass="int">

  select * from t_result where id=#{id};

  </select>

<!--column为外键列名,select表示引用另一个select标签查询结果填充该属性值- ->

u <resultMap class="result"id=" rm">
u  <association property="user"column="userId" select=" findUserById"/>
u  </resultMap>


u<!-- 定义按用户ID查询用户 -->

<select id="findUserById" resultType="user" >

  select * from t_user where id=#{id};

  </select>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值