MyBatis级联

    xiaoxiao2023-10-15  145

    简述

    级联是一个数据库实体的概念,级联不是必须的,级联的好处是获取关联数据十分方便,但是级联过多会增加系统的复杂度,同时降低系统的性能,所以当级联的层级超过3层时,就不要考虑使用级联,这样会导致多个对象的关联,导致系统耦合度增加,难以维护。

    MyBatis的级联分为3种:

    鉴别器(discriminator):它是一个根据某些条件决定采用具体实现类级联的方案。一对一(association):比如身份证和人就是一种一对一的级联关系。一对多(collection):比如班主任和学生就是一种一对多的级联关系。

    注意!MyBatis中没有多对多级联关系,因为多对多级联比较复杂,使用困难,而且可以通过两个一对多级联进行替换,所以MyBatis不支持多对多级联。

    下面主要讲解常用的一对一级联与一对多级联。

    一对一级联

    JavaBean

    package com.ronin.blog.entity; import lombok.Data; import java.util.Date; import java.util.List; @Data public class Article { private Integer articleId; private Integer articleUserId; private String articleTitle; private String articleSummary; private Integer articleViewCount; private Integer articleCommentCount; private Integer articleLikeCount; private Integer articleIsComment; private Integer articleStatus; private Date articleUpdateTime; private Date articleCreateTime; private String articleHtml; private String articleContent; //一对一关系 private User user; }

    配置文件

    <resultMap id="BaseResultMap" type="com.ronin.blog.entity.Article" > <id column="article_id" property="articleId" jdbcType="INTEGER" /> <result column="article_user_id" property="articleUserId" jdbcType="INTEGER" /> <result column="article_title" property="articleTitle" jdbcType="VARCHAR" /> <result column="article_summary" property="articleSummary" jdbcType="VARCHAR" /> <result column="article_view_count" property="articleViewCount" jdbcType="INTEGER" /> <result column="article_comment_count" property="articleCommentCount" jdbcType="INTEGER" /> <result column="article_like_count" property="articleLikeCount" jdbcType="INTEGER" /> <result column="article_is_comment" property="articleIsComment" jdbcType="INTEGER" /> <result column="article_status" property="articleStatus" jdbcType="INTEGER" /> <result column="article_update_time" property="articleUpdateTime" jdbcType="TIMESTAMP" /> <result column="article_create_time" property="articleCreateTime" jdbcType="TIMESTAMP" /> <result column="article_html" property="articleHtml" jdbcType="LONGVARCHAR" /> <result column="article_content" property="articleContent" jdbcType="LONGVARCHAR" /> <!-- 一对一级联 --> <association property="user" javaType="User"> <id column="user_id" property="userId"/> <result column="user_name" property="userName"/> </association> </resultMap>

    一对多级联

    JavaBean

    package com.ronin.blog.entity; import lombok.Data; import java.util.Date; import java.util.List; @Data public class Article { private Integer articleId; private Integer articleUserId; private String articleTitle; private String articleSummary; private Integer articleViewCount; private Integer articleCommentCount; private Integer articleLikeCount; private Integer articleIsComment; private Integer articleStatus; private Date articleUpdateTime; private Date articleCreateTime; private String articleHtml; private String articleContent; //一对多关系 private List<Tag> tagList; }

    配置文件

    <resultMap id="BaseResultMap" type="com.ronin.blog.entity.Article" > <id column="article_id" property="articleId" jdbcType="INTEGER" /> <result column="article_user_id" property="articleUserId" jdbcType="INTEGER" /> <result column="article_title" property="articleTitle" jdbcType="VARCHAR" /> <result column="article_summary" property="articleSummary" jdbcType="VARCHAR" /> <result column="article_view_count" property="articleViewCount" jdbcType="INTEGER" /> <result column="article_comment_count" property="articleCommentCount" jdbcType="INTEGER" /> <result column="article_like_count" property="articleLikeCount" jdbcType="INTEGER" /> <result column="article_is_comment" property="articleIsComment" jdbcType="INTEGER" /> <result column="article_status" property="articleStatus" jdbcType="INTEGER" /> <result column="article_update_time" property="articleUpdateTime" jdbcType="TIMESTAMP" /> <result column="article_create_time" property="articleCreateTime" jdbcType="TIMESTAMP" /> <result column="article_html" property="articleHtml" jdbcType="LONGVARCHAR" /> <result column="article_content" property="articleContent" jdbcType="LONGVARCHAR" /> <!-- 一对多级联 --> <collection property="tagList" javaType="list" ofType="Tag"> <!-- 映射主键 --> <id column="tag_id" property="tagId"/> <!-- 映射普通属性 --> <result column="tag_name" property="tagName"/> <result column="tag_description" property="tagDescription"/> </collection> </resultMap>
    最新回复(0)