mybaties association 只返回一个结果问题处理

mybatis xml文件为:

 <resultMap id="BaseResultMap" type="com.test.SubscribeOrder">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="channel_id" jdbcType="INTEGER" property="channelId" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="user_id" jdbcType="VARCHAR" property="userId" />
    <result column="jr_id" jdbcType="VARCHAR" property="jrId" />
    <result column="trade_amount" jdbcType="BIGINT" property="tradeAmount" />
    <result column="profit" jdbcType="BIGINT" property="profit" />
    <result column="trade_time" jdbcType="TIMESTAMP" property="tradeTime" />
    <result column="state" jdbcType="TINYINT" property="state" />
    <result column="created_date" jdbcType="TIMESTAMP" property="createdDate" />
    <result column="modified_date" jdbcType="TIMESTAMP" property="modifiedDate" />
    <association property="project" resultMap="projectMap" javaType="com.test.Project"/>
  </resultMap>
  <resultMap id="projectMap" type="com.test.Project">
    <id column="project_id" property="id" jdbcType="INTEGER"/>
    <result column="project_name" property="projectName" jdbcType="VARCHAR" />
    <result column="project_code" property="projectCode" jdbcType="VARCHAR" />
  </resultMap>

sql语句为:

   select
      a.id,
      a.channel_id,
      a.order_no,
      a.project_id,
      b.project_name,
      b.project_code
      from fp_subscribe_order a inner join fp_project b
      on a.project_id = b.id where 1=1
      <if test="orderNo!=null">
        and a.order_no=#{orderNo}
      </if>
      <if test="projectCode!=null">
        and  b.project_code=#{projectCode}
      </if>
      <if test="projectName!=null">
        and  b.project_name=#{projectName}
      </if>
      <if test="userId!=null">
        and  a.user_id=#{userId}
      </if>

结果只会返回一个结果

处理:首先需要说明 select的列不需要和对应的resultMap的元素数量一一对应;mybatis使用association 时必须要保证key和association并列,简单来说就是select后面的列很多都可以省但BaseResultMap中的数据库字段不可以省,修改过的sql语句为

<select id="findSubscribeOrdersByPage" resultMap="BaseResultMap">
      select
      a.id,
      a.channel_id,
      a.order_no,
      a.user_id,
      a.jr_id,
      a.trade_time,
      a.trade_amount,
      a.profit,
      a.state ,
      a.created_date,
      a.modified_date,
      a.project_id,
      b.project_name,
      b.project_code
      from fp_subscribe_order a inner join fp_project b
      on a.project_id = b.id where 1=1
      <if test="orderNo!=null">
        and a.order_no=#{orderNo}
      </if>
      <if test="projectCode!=null">
        and  b.project_code=#{projectCode}
      </if>
      <if test="projectName!=null">
        and  b.project_name=#{projectName}
      </if>
      <if test="userId!=null">
        and  a.user_id=#{userId}
      </if>
  </select>
原文地址:https://www.cnblogs.com/rainydayfmb/p/7451647.html