一对一
一对一的映射配置association标签 association属性: property:表示要映射的pojo类的属性名 javaType:表示要映射的属性的类型讯享网
讯享网 <!-- 一对一的映射配置association标签 association属性: property:表示要映射的pojo类的属性名 javaType:表示要映射的属性的类型 --> <resultMap id="infoResultMap" type="Info"> <id property="id" column="id" /> <result property="title" column="title"></result> <association property="member" javaType="Member"> <id property="id" column="id"/> <result property="username" column="username"></result> </association> </resultMap> <!-- select * from dlq_info as info left join dlq_member as member on info.member_id = member.id where info.id=#{id} --> <select id="findById" parameterType="integer" resultMap="infoResultMap"> select * from dlq_info info,dlq_member member where info.member_id = member.id and info.id=#{id} </select>
一对多
<!--
一对多使用collection标签
collection标签属性:
property:pojo类中的属性名
ofType:对应的属性的类型
-->
<!-- resultMap属性: id:与select标签中的resultMap对应即可,唯一标识 type:表示对哪个pojo类进行映射 resultMap子标签: id标签:表示对主键进行映射 property:pojo类中的属性名 column:数据库中的字段名 result标签:表示对其他非主键进行映射 --> <!-- 一对多使用collection标签 collection标签属性: property:pojo类中的属性名 ofType:对应的属性的类型 --> <resultMap id="infos" type="Member"> <id property="id" column="id" /> <result property="username" column="username"></result> <collection property="infos" ofType="com.angen.pojo.Info"> <result column="id" property="id"></result> <result column="title" property="title"></result> </collection> </resultMap> <select id="findInfoById" parameterType="integer" resultMap="infos"> select * from dlq_member as member left join dlq_info as info on member.id=info.member_id where member.id=#{id} </select>
多对多跟一对多差不多
一对一映射,嵌套查询,会产生n+1,也就是多执行一次sql语句
Mybatis(十二)一对一关系处理 - 知乎
讯享网association标签中的select属性和column属性
场景:两条sql语句,一条sql语句需要另一条sql语句的查询结果的某个值来查询
<select id="findById" parameterType="integer" resultType="com.angen.pojo.Member"> select * from dlq_member where id=#{id} </select>
<resultMap id="infoTwo" type="com.angen.pojo.Info"> <id property="id" column="id"/> <result property="title" column="title"></result> <association property="member" javaType="Member" column="member_id" select="com.angen.dao.MemberDao.findById" > </association> </resultMap> <select id="findByIdTwo" parameterType="integer" resultMap="infoTwo"> select * from dlq_info where id=#{id} </select>
以上,这样就可以不用在association标签中自己写映射了。

column属性:传入哪个字段提供给select="com.angen.dao.MemberDao.findById"对应的那个sql语句来查询
select属性:映射配置文件的命名空间+对应的id.
上面是分步骤查询
延迟加载(懒加载)fetchType="lazy"局部延迟加载
<resultMap id="infoTwo" type="com.angen.pojo.Info"> <id property="id" column="id"/> <result property="title" column="title"></result> <association property="member" javaType="Member" column="member_id" select="com.angen.dao.MemberDao.findById" fetchType="lazy" > </association> </resultMap>
加上这个属性后,如果我们不用member里面的数据就不会查询member的sql语句。
全局延迟加载,在核心配置文件中配置

<settings> <setting name="lazyLoadingEnabled" value="ture"/> </settings>
一对多的分步执行,延时加载跟一对一差不多,只不过是collection标签
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/44499.html