欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于mybatis一对多查询内层排序的问题

程序员文章站 2022-03-01 20:57:39
目录mybatis一对多查询内层排序mybatis多排序问题mybatis一对多查询内层排序

mybatis一对多查询内层排序

<!--根据板块id查询所有主题->指标->维度-->
    <resultmap id="titledimensionmap" type="com.etouch.admincenter.bean.zhmddiagnosistitlebean">
        <id column="title_id" property="id"/>
        <result column="title_name" property="titlename"/>
        <collection property="diagnosiskpibeanlist" oftype="com.etouch.admincenter.bean.zhmddiagnosiskpibean">
            <id column="kpi_id" property="id"/>
            <result column="kpi_name" property="kpiname"/>
            <result column="kpi_type" property="kpitype"/>
            <result column="calculate_method" property="calculatemethod"/>
            <result column="is_add_description" property="isadddescription"/>
            <result column="description_remind" property="descriptionremind"/>
            <result column="input_description" property="inputdescription"/>
            <result column="kpi_industry_standard" property="kpiindustrystandard"/>
            <result column="kpi_brand_standard" property="kpibrandstandard"/>
            <result column="kpi_industry_standard_diff" property="kpiindustrystandarddiff"/>
            <result column="kpi_brand_standard_diff" property="kpibrandstandarddiff"/>
            <result column="input_description" property="inputdescription"/>
            <result column="kpi_description" property="kpidescription"/>
            <collection property="dimensionbeans" oftype="com.etouch.admincenter.bean.zhmddiagnosiskpidimensionbean">
                <id column="kpi_dimension_id" property="id"/>
                <result column="diagnosis_id" property="diagnosisid"/>
                <result column="dimension_name" property="dimensionname"/>
                <result column="dimension_value" property="dimensionvalue"/>
            </collection>
        </collection>
    </resultmap>

可设置多个排序规则,这样展示到前端页面时,解决每次打开页面显示顺序都不一样的问题

<select id="getdimensiononestep" resultmap="titledimensionmap">
     select
    zdt.id as title_id,zdt.title_name,
    zdk.id as kpi_id,zdk.kpi_name,zdk.kpi_type,zdk.calculate_method,zdk.is_add_description,zdk.description_remind,zdk.kpi_description,
    zdkd.id as kpi_dimension_id, zdkd.dimension_name,zdd.dimension_value,
    zdkpd.input_description
    from zhmd_diagnosis_title zdt
    left join zhmd_diagnosis_kpi zdk on zdk.diagnosis_title_id = zdt.id
    left join zhmd_diagnosis_kpi_dimension zdkd on zdkd.diagnosis_kpi_id = zdk.id
    left join zhmd_dimension_diagnosis zdd on  zdd.dimension_id = zdkd.id and zdd.diagnosis_id = #{diagnosisid}
    left join zhmd_diagnosis_kpi_problem_desc zdkpd on zdkpd.kpi_id = zdk.id and zdkpd.diagnosis_id =#{diagnosisid}
    where zdt.diagnosis_model_id = #{modelid}
    order by zdt.create_date desc,zdk.create_date desc,zdkd.dimension_sort asc
</select>

postman查询结果

基于mybatis一对多查询内层排序的问题

mybatis多排序问题

在很多业务场景中,会有多排序问题,mybatis支持多排序,写法如下:

order by contract.contractcount desc, reward.rewardtotal desc, new_job.newjobcount desc

这种写法是根据最左边的字段进行第一排序,如果第一排序字段有并列的,则根据第二字段再去排一次,依次类推。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。