Mybatis 使用distinct问题

在使用Mybatis时遇到distinct生成SQL的问题,导致查询结果不正确。通过检查发现SQL生成时缺少了关键部分。修复后的SQL去掉了'Base_Column_List',从而解决了问题。

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

使用插件自动生成sql,具体如下

 <resultMap id="versioncodeResult" type="java.lang.String" >
    <result column="code" property="code" jdbcType="VARCHAR" />
  </resultMap>
  
<select id="selectVersionCodeByParam" parameterType="com.entity.OdpsrequesttoidbtableParam" resultMap="versioncodeResult">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    versioncode as code
    <include refid="Base_Column_List" />
    from odpsrequesttoidbtable
    <if test="_parameter != null">
      <include refid="Param_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="page">
      limit #{pageIndex},#{pageSize}
    </if>
  </select>

始终查询不到我想要的结果

序号code
1266
2267

查看具体生成的sql发现,少去掉了一行

###
 SQL: select             distinct           versioncode as code    
 id, gmt_create, gmt_modified, apiname, pkey, versioncode, appkey      
 from odpsrequesttoidbtable                         WHERE (  appkey = ? )

修复后的sql如下,去掉"Base_Column_List" :

<select id="selectVersionCodeByParam" parameterType="com.entity.OdpsrequesttoidbtableParam" resultMap="versioncodeResult">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    versioncode as code
    from odpsrequesttoidbtable
    <if test="_parameter != null">
      <include refid="Param_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="page">
      limit #{pageIndex},#{pageSize}
    </if>
  </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值