MyBatis多对一和一对多

date: 2019/8/15 21:23:48


MyBatis的强大特性之一。



1.collection 标签

第一种 一对多查询
先配置一个 resultMap

1
2
3
4
5
6
7
8
9
10
<resultMap id="model2" type="tdept">
<id column="deptid" property="deptid"/>
<result column="dname" property="dname"/>
<!--多对一的标签 -->
<!-- tEmps 为 tdept中的 list集合名称 -->
<!--column 就是 resultMap中的id的column-->
<!--select 就是 从表中的外键 对于 deptid 进行查询 -->
<collection property="tEmps" column="deptid" select="com.xiaolan.mapper.TEmpMapper.selectAllByDId"/>
</resultMap>

看看 两张表的 查询语句 以及返回 类型

1
2
3
4
5
6
<!-- 主表的返回类型是 resultMap="model2" -->
<!-- 查询的是 主键id -->
<select id="selectById" resultMap="model2" parameterType="int">
select * from t_dept where deptid = #{deptid};
</select>
1
2
3
4
5
6
<!-- 从表的返回类型是 resultType="temp" -->
<!-- 查询的是 depid 外键-->
<select id="selectAllByDId" resultType="temp" parameterType="int">
select * from t_emp where depid = #{depid}
</select>

第二种 一对多查询
新配置一个 resultMap

1
2
3
4
5
6
7
8
9
10
11
12
13
<resultMap id="" type="tdept">
<id column="deptid" property="deptid"/>
<result column="dname" property="dname"/>
<!-- property 实体类list 名字 -->
<!-- ofType 里面写从表的 实体类 -->
<!-- 里面是映射字段 -->
<collection property="tEmps" ofType="temp">
<id column="empid" property="empid"/>
<result column="ename" property="ename"/>
<result column="egendar" property="egendar"/>
</collection>
</resultMap>

直接分析下 xml

1
2
3
4
5
6
7
8
9
10
<!-- 两张表连接查询 resultMap="model" -->
<select id="selectAll" resultMap="model">
select
*
from
t_dept d ,
t_emp e
where d.deptid = 1 and d.deptid = e.depid
</select>

2.association 标签

第一种 多对一查询
先配置一个 resultMap

1
2
3
4
5
6
7
8
9
10
11
<resultMap id="model" type="temp">
<id column="empid" property="empid"/>
<result column="ename" property="ename"/>
<result column="egendar" property="egendar"/>
<!-- property 实体类中的对象-->
<!-- javaType 对象类型 -->
<association property="dept" javaType="tdept">
<id column="deptid" property="deptid"/>
<result column="dname" property="dname"/>
</association>
</resultMap>

看看 两张表的连接 查询语句 以及返回 类型

1
2
3
4
<select id="findByAlls" resultMap="model">
select * from t_dept d, t_emp e where d.deptid = e.depid and e.empid = 1
</select>

第二种 多对一查询
先配置一个 resultMap

1
2
3
4
5
6
7
8
9
10
<resultMap id="model22" type="temp">
<id column="empid" property="empid"/>
<result column="ename" property="ename"/>
<result column="egendar" property="egendar"/>
<!-- property 实体类名称 -->
<!-- column 数据库外键字段 -->
<!-- select 主键的查询id -->
<association property="dept" column="depid" select="com.xiaolan.mapper.TDeptMapper.selectByIds">
</association>
</resultMap>

看看 两张表的 查询语句 以及返回 类型

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 主表的返回类型是 resultMap="model2" -->
<!-- 查询的是 主键id -->
<!-- 这里采用 了foreach 实现 多对一的效果 -->
<select id="selectAllById" resultMap="model22" parameterType="list">
select * from t_emp
<where>
<foreach collection="list" separator="," close=")" open="empid in (" item="ids">
#{ids}
</foreach>
</where>
</select>

1
2
3
4
5
6
<!-- 主表的返回类型是 resultType="tdept" -->
<!-- 查询的是 主键id-->
<select id="selectByIds" resultType="tdept" parameterType="int">
select * from t_dept where deptid = #{deptid};
</select>
文章目录
  1. 1. date: 2019/8/15 21:23:48
  • 1.collection 标签
  • 2.association 标签