【理论】mybatis对象-关系映射



类与类的关系:
继承(子类,父类)
实现(interface实现)
关联(拥有关系)
组合/聚合(整体与部分:组合是耦合度高的; 聚合 :耦合度低,整体受影响,部分不被影响)、
依赖:人依赖于空气、水(方法的参数及返回值形式 );
关联、组合、聚合:以成员变量的方式存在 ;关系形式为:1V1,1V多,多V多;
设计的时候要考虑关联关系之间的扩展:在中国夫妻关系1V1;在国外为1V多;

迪米特法则:参数类、返回类可以作为类的朋友; 最少知道原则;

一对一关系: 丈夫(id、姓名)老婆(id、姓名、外键)
预加载查询方式: 在增加丈夫和父亲中要在JDBC中增加一个语句, 允许多条语句在标签内部同时执行的命令;但是目前我们学习这个拿不到老婆新生成的id。
根据丈夫信息查询的语句书写: 根据丈夫的id查询丈夫和妻子的信息;
<select id="getHusbandWithWifeById" resultMap=" husWithWifeMap">
select h.id as hId,h.hus_name as hName,w.id as wId,w.wife_name as wName
from t_husband as h left join t_wife as w on h.id = w.fk_hus_id
<where>
h.id = #{id}
</where>
</select>

关联关系
<resultMap type="HusbandBean" id=" husWithWifeMap">
<id property="id" column="hId" javaType="java.lang.Long"/>
<result property="name" column="hName" javaType="java.lang.String"/>
<!-- association 这个单词含义是:关联,关系-->
< association property=" wife" javaType=" WifeBean">
<id property="id" column="wId" javaType="java.lang.Long"/>
<result property="name" column="wName" javaType="java.lang.String"/>
</association>
</resultMap>
association 在关联关系为唯一方的对象的时候使用,在没有list的时候使用;

处理业务:根据id首先删除丈夫有关的对象,然后删除丈夫;
在sql语句 中;是一条语句的结束,我们要使用这个;来结束一条语句。
<delete id="deleteHusbandById">
delete from t_wife where fk_hus_id = #{id};
delete from t_husband where id = #{id};
</delete>
可以做多条语句同时执行,受影响的行数结果以最后一次执行的语句的结果返回的值;

及时加载查询模式:多条语句查询中首先查出妻子中包含的老公ID,再根据老公ID查询老公所有信息;
public WifeBean getWifeWithHusbandById(@Param("id")Long id);
在妻子端的代码
<resultMap type="WifeBean" id=" wifeWithHusMap">
<id property="id" column="id" javaType="java.lang.Long"/>
<result property="name" column="wife_name" javaType="java.lang.String"/>
<association property="hus" javaType="HusbandBean" fetchType="lazy" select="com.lovo.mybatis.mapper.HusbandMapper.getHusbandById" column="fk_hus_id">
</association>
</resultMap>
//其中select是调用哪里的制定的select方法,colimn是将上面获取的结果集中的值fk_hus_id通过占位符传到这个指定SQL标签里面去,在哪里通过#{id} 获取值;
fetchType="lazy" 为延时加载模式;
//通过映射让妻子这边的方法,去执行查询丈夫的信息;并将查询的结果关联到属性:丈夫
<select id="getWifeWithHusbandById" resultMap=" wifeWithHusMap">
select id,wife_name,fk_hus_id from t_wife where id = #{id}
</select>
查询加载的三种方式
预加载(left join on 连表查询中)
及时加载(刚才上面用到的这个select和colimn的方法)
延时加载(懒加载--通过代理模式来实现);主要用于提高性能、当查A就只查A就断开了
首先、在上面的<association>中的属性中设置为延迟加载模式 fetchType="lazy"
其次、设置在logger4J的配置:
<!-- 配置Logger4j的使用 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
<!-- 全面开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

一对多关系
1、添加儿子
<insert id="batchAddSons">
insert into t_son (son_name,fk_parents_id)values
<foreach collection="sons" item="son" separator=",">
(#{son.name},#{son.parents.id})
</foreach>
</insert>
2、根据ID查询一个父亲下的所有儿子;
<resultMap type="SonBean" id="sonWithParentMap">
<id property="id" column="sId" javaType="java.lang.Long"/>
<result property="name" column="sName" javaType="java.lang.String"/>
<association property="parents" javaType="ParentBean">
<id property="id" column="pId" javaType="java.lang.Long"/>
<result property="name" column="pName" javaType="java.lang.String"/>
</association>
</resultMap>

<select id="getSonBeanWithParentsById" resultMap="sonWithParentMap">
select s.id as sId,s.son_name as sName,p.id as pId,p.parent_name as pName from t_son as s left join t_parents as p on s.fk_parents_id = p.id
<where>
s.id = #{id}
</where>
</select>
【细节点】:1、不能用* 2、做映射,将父亲的信息以关联关系,进行存储;

3、在父亲mapper中,根据ID查询父亲;
<resultMap type="ParentBean" id="parentWithSonsMap">
<id property="id" column="id" javaType="java.lang.Long"/>
<result property=" name" column=" parent_name" javaType="java.lang.String"/>
< collection property="sons" javaType="java.util.List" fetchType=" lazy" select=" getSonsByParentId" column=" id"></collection>
</resultMap>
//查询父亲的信息
<select id="getParentWithSonsById" resultMap="parentWithSonsMap">
select id,parent_name from t_parents where id = #{id}
</select>
//查询儿子的信息,该处直接查询,并没有在MAPPer.java中申明这个方法;
<select id=" getSonsByParentId" resultType="SonBean">
select id as id,son_name as name from t_son
<where>
fk_parents_id = #{id}
</where>
</select>
【细节点】: 查询的list用<collection property="sons" javaType="java.util.List" select="调用本层的根据id查询父亲对象" column="id"></collection>
【细节点】:配置文件是接口的实现,配置文件可以实现比接口更多的文件;

4、根据id查询父亲类;
<select id="getParentById" resultType="ParentBean">
select id as id,parent_name as name from t_parents where id = #{id}
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值