Mybatis 一对多

写在开头的话:

首先要讨论下为什么一定要用一对多、多对一,如果我的实体bean 中并不是和数据库中的表做到一一对应,实体bean 的属性比表字段要多(当然是为了满足业务) 
那么不用对应关系,也可以解决业务上的需求。
但是这样会让你的实体bean 显得是如此的丑陋和混乱,为了保持pojo 的整洁,所以有必要用对应关系(个人感觉。。。)

不管你采不采用对应关系来查询,sql 语句并没有简化写法,这和hibernate 的HQL来级联查询的效果不同 

一对多之 collection: 

配置文件:(方式一 更利于resultMap 的重用)

<mapper namespace="com.azcsoft.casecade.dao.CaseCadeDao">
<!-- 方式一  --> 
<resultMap  >
<result property="teaName" column="teaname" />
<result property="teaSex" column="teasex"/>
<result property="teaAge" column="teaage" />
<collection property="stus"   resultMap="stuMap" />
</resultMap>
<resultMap  >
<result property="stuName" column="stuname" />
<result property="stuSex" column="stuage"/>
<result property="stuAddress" column="stuaddress" />
</resultMap>
<select >
select * from tea a left join stu b on a.teaid = b.teaid
</select>
 

<!-- 方式二: -->

 <resultMap type="com.azcsoft.vo.Author" >

</mapper>

 多对一 之 association

  方式一:(利于resultMap 的重用)

<resultMap type="com.azcsoft.vo.Author" >
<result property="aid" column="a_id" />
<result property="aname" column="aname" />
<result property="asex" column="asex" />
<result property="birsday" column="abirsday" />
</resultMap>
<resultMap type="com.azcsoft.vo.Blog" >
<result property="bid" column="b_id"/>
<result property="bname" column="bname"/>
<result property="bcontent" column="bcontent"/>
<result property="ctime" column="bctime"/>
<association property="author" column="a_id" resultMap="author" />
</resultMap>
<select >
select * from blog a left join author b on a.a_id = b.a_id
</select>

  方式二:
<resultMap type="com.azcsoft.vo.Blog" >     </resultMap>

<select >     </select>

------------------------------------对象:

public class Tea {
private String teaName;
private String teaSex;
private String teaAge;
private List<Stu> stus;

 }

------------------------------------表: 

STU 表中 关联 TEA 的主键