mybatis join使用

本文介绍了如何在mybatis中使用join查询来获取班级与小组的关联数据。在postgresql数据库中,通过外键关联了班级表(class)和小组表(group),并设置为cascade以实现删除和更新时的联动。使用easycode生成mybatis基本查询时,需要注意生成的xml中数据库表名包含了数据库名,这可能导致更换数据库名后无法运行,建议直接使用表名。在处理集合时,由于可能出现相同的id,因此在sql中添加了 gid 来匹配小组表的主键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星仔说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值