zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    POJ3070 Fibonacci 快速矩阵幂
    HDU1299 Diophantus of Alexandria 素因子分解
    HUTXXXX The window of the dazzling 模拟
    HUTXXXX 周正虎的难题 二分
    使用js给页面元素添加样式
    javascript 获取操作系统语言
    div层一直处于页面中间
    javascript:history.go()和History.back()的区别
    javascript的一些常用正则表达式
    [七日成魔2.1版]完美PHOTOSHOP教程新手培训套餐
  • 原文地址:https://www.cnblogs.com/rainydayfmb/p/7451647.html
Copyright © 2011-2022 走看看