mybatis一对一,一对多查询(工作案例总结)

以下是日常工作中mybatis一对一,一对多查询,采用子查询映射方式,其中一对多查询有两种方式,一种是单个参数,一种是多个参数,非常值得参考价值,故这里做了总结

mybatis一对一查询:子查询映射方式,单个参数,先查询主表,然后根据主表查询结果再查询子表,主表可以sql分页

实体类DTO:

private String neikong_key;
private String neikong_name;
private String principal;//一对一实体属性

mapper文件:

主表查询:

List<T01_test_neikongDTO>  queryT01_test_neikongList(T01_test_neikongDTO dto);

xml文件:

<!--查询返回映射-->
<resultMap id="baseResultMap" type="T01_test_neikongDTO">
	<id property="neikong_key" column="neikong_key"/>
	<result property="neikong_name" column="neikong_name"/>
	<!--mybatis一对一查询:嵌套select语句形式(单个参数)-->
	<association property="principal" column="neikong_key" javaType="string" select="getPrincipalById"/>
</resultMap>

<!--主表查询结果:注意返回类型是resultMap而不是resultType-->
<select id="queryT01_test_neikongList" parameterType="T01_test_neikongDTO" resultMap="baseResultMap">
	select t.neikong_key,t.neikong_name
	from t03_question_neikong t
	where neikong_key = #{neikong_key}
</select>
     
<!--根据主键查询信息类型-->
<select id="getPrincipalById" parameterType="string" resultType="string">
	select wm_concat(principal) as principal
	from t01_test_neikong_person
	where neikong_key = #{neikong_key}
	group by neikong_key
</select>

备注:assocaation属性介绍

property:子查询属性名

column:数据库的列名,可以是别名

javaType:子查询属性名返回类型

select:子查询select的id值

mybatis一对多查询:子查询映射方式,单个参数,先查询主表,然后根据主表查询结果再查询子表,主表可以sql分页

实体类DTO:

private String neikong_key;
private String neikong_name;
private List<T01_test_neikong_personDTO> neikong_personList;//一对多实体属性

mapper文件:

List<T01_test_neikongVO> exportT01_test_neikong(T01_test_neikongDTO dto);

xml文件:

<!--查询返回映射-->
	<resultMap id="exportResultMap" type="T01_test_neikongVO">
        <id property="neikong_key" column="neikong_key"/>
        <result property="neikong_name" column="neikong_name"/>      
        <!--mybatis一对多查询:嵌套select语句形式(单个参数)-->
        <collection property="neikong_personList" column="neikong_key" javaType="java.util.ArrayList" ofType="T01_test_neikong_personDTO" select="getT01_test_neikong_personList"/>
    </resultMap>
	
	<!--主表查询-->
	<select id="exportT01_test_neikong" parameterType="T01_test_neikongDTO" resultMap="baseResultMap">
		select t.neikong_key,t.neikong_name
		from t03_question_neikong t
		where neikong_key = #{neikong_key}
	</select>

    <!--根据主键查询子信息-->
    <select id="getT01_test_neikong_personList" parameterType="java.lang.String" resultType="T01_test_neikong_personDTO">
        select t.neikong_key,
            t.principal,
            listagg(t1.upleader,',') within group (order by t1.upleader) upleader
        from t01_test_neikong_person t
        left join t01_test_neikong_upleader t1
          on t.neikong_key = t1.neikong_key
          and t.principal = t1.principal
        where t.neikong_key = #{neikong_key}
        group by t.neikong_key,t.principal
     </select>

collection属性介绍:

property:集合的属性名

column:数据库的列名,可以是别名

javaType:集合的属性名返回类型

ofType:集合的属性名里面的元素类型

select:子查询select的id值

mybatis一对多查询:子查询映射方式,多个参数,先查询主表,然后根据主表查询结果再查询子表,主表可以sql分页

实体类DTO:

private String tbkey = "";
private String tbname = "";
private String username = "";
private String groupkey = "";
private List<T00_toolDTO> subToolList;//一对多实体属性

mapper文件:

List<ToolboxDTO> getT00_toolboxByGroup(T00_group_authDTO dto);

xml文件:

<resultMap id="baseResultMap" type="ToolboxDTO">
        <id property="tbkey" column="tbkey"/>
        <result property="tbname" column="tbname"/>
        <result property="username" column="username"/>
        <result property="groupkey" column="groupkey"/>
        <!--mybatis一对多查询:嵌套select语句形式(多个参数)-->
        <collection property="subToolList" column="{username=username,groupkey=groupkey,tbkey=tbkey}" javaType="java.util.ArrayList" ofType="T00_toolDTO" select="getT00_toolByTbKey"/>
    </resultMap>
	
	<!--主表查询-->
	<select id="getT00_toolboxByGroup" parameterType="T00_group_authDTO" resultMap="baseResultMap">
		select t.tbkey,t.tbname,t.username,t.groupkey
		from t00_toolbox t
	</select>
	
    <!--根据传递的多个参数子查询-->
    <select id="getT00_toolByTbKey"  parameterType="java.util.Map" resultType="T00_toolDTO">
          select distinct t0.toolkey,t0.tbkey,t0.toolname
          from t00_tool t0,t00_role_resource t1,t00_group_auth t2
          where 1=1
          and t1.rolekey = t2.rolekey
          and t0.toolkey = t1.resourcekey
          and t1.resource_type_key='004'
          and t2.username = #{username}
          and t2.groupkey = #{groupkey}
          and t0.tbkey =#{tbkey}
     </select>

collection属性介绍:

property:集合的属性名

column:数据库的列名,可以是别名,注意这里的参数写法是map的集合方式{数据库列名1=主表查询返回的属性名1,数据库列名1=主表查询返回的属性名1...} 

javaType:集合的属性名返回类型

ofType:集合的属性名里面的元素类型

select:子查询select的id值

注意子查询的参数类型parameterType是根column类型是一致的,若是单个参数则是string类型,若是多个参数则是map类型

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值