注意事项: 如果是多表 例如有A表,B表 A是多端,B是一端 那么就要在B端声明A端的集合 必须
上代码:
我使用的是java语言,先看实体类 这是单表的一对多 多表跟单表逻辑是一致
public class Menu extends PublicVo {
private Integer id;
private String name;
private String path;
private String component;
private String icon;
private Integer leaf;
private Integer hidden;
private String remark;
private Integer fatherId;
private Date menuDate;
private List<Menu> children;
}
mybatis 查询的一对多的两种方式
第一种:多表联合查询sql
<!--一对多查询单表 方式一-->
<resultMap id="menuMap" type="com.example.hongxinwulian.pojo.Menu">
<id property="id" column="id"></id>
<!--配置menu对象中children集合的映射-->
<collection property="children" ofType="com.example.hongxinwulian.pojo.Menu">
<result property="fatherId" column="fatherId"></result>
<result property="name" column="name"></result>
<result property="path" column="path"></result>
<result property="component" column="component"></result>
<result property="icon" column="icon"></result>
<result property="leaf" column="leaf"></result>
<result property="hidden" column="hidden"></result>
<result property="remark" column="remark"></result>
<result property="menuDate" column="menuDate"></result>
</collection>
</resultMap>
<select id="getMenuList" resultMap="menuMap,menuTotal">
select SQL_CALC_FOUND_ROWS * from hongxin_menu as m left join hongxin_menu as m1
on m.id = m1.fatherId where m.fatherId = 0 limit #{currentPage},#{pageSize}
</select>
第二种:采用子查询方式
<!--一对多查询单表 方式二-->
<resultMap id="menuMap1" type="com.example.hongxinwulian.pojo.Menu">
<id property="id" column="id"></id>
<result property="fatherId" column="fatherId"></result>
<result property="name" column="name"></result>
<result property="path" column="path"></result>
<result property="component" column="component"></result>
<result property="icon" column="icon"></result>
<result property="leaf" column="leaf"></result>
<result property="hidden" column="hidden"></result>
<result property="remark" column="remark"></result>
<result property="menuDate" column="menuDate"></result>
<!--配置menu对象中children集合的映射
property="children" 是一端的声明的多段的结合
ofType="com.example.hongxinwulian.pojo.Menu" //映射到集合中的pojo属性
column="id" 是子查询用的参数
select="getChildMenu" 调用子查询的方法名称
-->
<collection property="children"
ofType="com.example.hongxinwulian.pojo.Menu"
column="id"
javaType="ArrayList"
select="getChildMenu">
</collection>
</resultMap>
<resultMap id="menuMap2" type="com.example.hongxinwulian.pojo.Menu">
<id property="id" column="id"></id>
<result property="fatherId" column="fatherId"></result>
<result property="name" column="name"></result>
<result property="path" column="path"></result>
<result property="component" column="component"></result>
<result property="icon" column="icon"></result>
<result property="leaf" column="leaf"></result>
<result property="hidden" column="hidden"></result>
<result property="remark" column="remark"></result>
<result property="menuDate" column="menuDate"></result>
</resultMap>
<!--主查询也就是dao层查询的如果-->
<select id="getMenuList" resultMap="menuMap1">
select * from hongxin_menu where fatherId = 0
</select>
<!--子查询-->
<select id="getChildMenu" parameterType="java.lang.Integer" resultMap="menuMap2">
select * from hongxin_menu where fatherId=#{id}
</select>
代码段里加注释了 这里不多说了 比较基础的
看一下返回的数据 两种查询方式返回的数据都是一致: