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>