mybatis join使用

mybatis join使用

需求

兩張表 class 班級表 group小組表 一個班級會有多個小組

使用pgsql 外鍵關聯

希望一條sql 查詢所有的 班級和小組數據

數據庫表

裏面外鍵使用on delete cascade on update cascade

cascade 刪除和更新父表的時候 字表跟着變化

使用powerdesigner默認的外鍵是Restrict 也就是不運行修改

這裏使用cascade

注意點:使用了外鍵之後使用drop table IF exists t_person_class; 會報錯

​ 需要使用 drop table IF exists t_person_class cascade;

/*==============================================================*/
/* Table: t_person_class                                        */
/*==============================================================*/
create table t_person_class (
   id                   INT8                 not null,
   class_no             VARCHAR(128)         not null,
   class_name           VARCHAR(128)         not null,
   constraint PK_T_PERSON_CLASS primary key (id)
);

comment on table t_person_class is
'人員班級表';

/*==============================================================*/
/* Index: Idx_class_no                                          */
/*==============================================================*/
create  index Idx_class_no on t_person_class (
class_no
);

/*==============================================================*/
/* Index: Idx_class_name                                        */
/*==============================================================*/
create  index Idx_class_name on t_person_class (
class_name
);

/*==============================================================*/
/* Table: t_person_group                                        */
/*==============================================================*/
create table t_person_group (
   id                   INT8                 not null,
   group_class_id       INT8                 not null,
   group_no             VARCHAR(128)         not null,
   group_name           VARCHAR(128)         not null,
   constraint PK_T_PERSON_GROUP primary key (id)
);

comment on table t_person_group is
'人員小組表';

alter table t_person_group
   add constraint FK_T_PERSON_REFERENCE_T_PERSON foreign key (group_class_id)
      references t_person_class (id)
      on delete cascade on update cascade;

使用easycode生成mybatis對應的基本查詢

說明點:easycode生成的xml 數據庫表名會是 數據庫.表名 其實數據庫名沒有實際意義 且會導致數據庫名換了時候不能運行,建議直接使用表名即可 可以修改easycode的腳本解決。

@Data
public class PersonOrgDto implements Serializable {
    private Long id;

    private String classNo;

    private String className;

    List<PersonGroup> personGroupList;

}

集合使用collection去做

注意點:當有兩個相同的id的時候 會用第一個 所以在sql裏面加了一個gid 用gid 對應小組表的主鍵,否則會用班級表的id替代

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.test.dao.PersonClassDao">

    <resultMap type="com.xxx.test.dto.PersonOrgDto" id="PersonOrgDtoMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="classNo" column="class_no" jdbcType="VARCHAR"/>
        <result property="className" column="class_name" jdbcType="VARCHAR"/>
        <collection property="personGroupList" javaType="java.util.List" ofType="com.hikvision.test.entity.PersonGroup">
            <id property="id" column="gid" jdbcType="INTEGER"/>
            <result property="groupClassId" column="group_class_id" jdbcType="INTEGER"/>
            <result property="groupNo" column="group_no" jdbcType="VARCHAR"/>
            <result property="groupName" column="group_name" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>



    <!--查詢組織信息 group+class-->
    <select id="queryOrg" resultMap="PersonOrgDtoMap">
        select
               c.id, c.class_no, c.class_name,
               g.id gid, g.group_class_id,g.group_name,g.group_no
        from t_person_class c left join  t_person_group g
                on c.id=g.group_class_id;
    </select>


</mapper>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章