MyBatis4——一对一、一对多关联查询
2024-09-02 21:16:18
关联查询:
一对一:
1、业务扩展类
核心:用resultType指定的类的属性包含多表查询的所有字段。
2、resultMap
通过添加属性成员建立两个类之间的连接
<!--利用resultMap实现一对一 -->
<select id="queryPersonsByReOnetoOne" parameterType="int" resultMap="person-card-map">
select p.*,c.* from person p inner join personcard c
on p.cardid=c.cardid
where p.id=#{id}
</select>
<!-- resultMap实现映射 -->
<resultMap type="person" id="person-card-map">
<!-- person信息 -->
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<!--一对一时,对象成员使用assocation实现映射,javaType指定该属性的类型 -->
<association property="card" javaType="PersonCard">
<id property="cardid" column="cardid"/>
<result property="cardinfo" column="cardinfo"/>
</association>
</resultMap>
一对多:
<!--一对多关联查询 -->
<select id="queryClassAndPersons" parameterType="int" resultMap="class-person-map">
select c.*,p.* from person p
inner join class c
on c.classid=p.classid
where c.classid=#{classid}
</select>
<!-- 类和表的对应关系 -->
<resultMap type="class" id="class-person-map">
<!-- 先配class -->
<id property="classid" column="classid" />
<result property="classname" column="classname" />
<!-- 配置成员属性。属性类型:jdbcType;属性的元素类型:ofType -->
<collection property="persons" ofType="Person">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</collection>
</resultMap>
最新文章
- hdu-acm steps Common Subsequence
- iOS自定义的UISwitch按钮
- poj 3020 Antenna Placement (最小路径覆盖)
- JavaScript之获取和设置元素属性
- 一些常用的Intent及intent-filter的信息
- 【转】Zookeeper-Watcher机制与异步调用原理
- C#语言之“String.Split”的使用【转】
- android 5.0 -- 主题
- Gluon炼丹(Kaggle 120种狗分类,迁移学习加双模型融合)
- 常用linux命令备忘
- Android--操作图片Exif信息
- UE4 Notes
- bzoj 2599: [IOI2011]Race (点分治 本地过了就是过了.jpg)
- Eclipse导入项目时出错提示 project is missing required library
- Eclipse + PyDev 快捷键
- Hive基础之Hive的存储类型
- JavaScript 对象属性作实参以及实参对象的callee属性
- Linux下调整根目录的空间大小
- CodeForces - 999C
- C++中函数调用时的三种参数传递方式详解