mapper扩展

1.当所返回的值与类中字段多时,可以扩展此类,并在mapper文件进行配置

1.首先在生成的类中加上新增字段,并加上get和set方法

//例子
 private String productName;

    public String getProductname() {
        return productName;
    }

    public BidInfo setProductname(String productname) {
        this.productName = productname;
        return this;
    }

2.配置mapper,在原map基础上增加

 <resultMap id="BaseResultMap" type="com.powernode.model.BidInfo">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="loan_id" jdbcType="INTEGER" property="loanId" />
    <result column="uid" jdbcType="INTEGER" property="uid" />
    <result column="bid_money" jdbcType="DOUBLE" property="bidMoney" />
    <result column="bid_time" jdbcType="TIMESTAMP" property="bidTime" />
    <result column="bid_status" jdbcType="INTEGER" property="bidStatus" />
    //加上如下配置
    <result  column="product_name" jdbcType="VARCHAR" property="productName"/>
  </resultMap>

3.sql

//在链接查询时使用此变量前不加表名as product_name
 select a.bid_time,a.bid_money,a.bid_status,a.uid,a.loan_id,b.product_name as product_name
    from
    b_bid_info as a
    join b_loan_info as b
    on a.loan_id=b.id
    where uid=#{id}
    order by a.bid_time desc
    limit #{pagenum},#{pagesize}

2.当引用其他表字段太多时可以新建一个工具类

1.新建工具类

public class BidExtUser extends BidInfo implements Serializable {
    //要引用的类
    private User user;

    public User getUser() {
        return user;
    }

    public BidExtUser setUser(User user) {
        this.user = user;
        return this;
    }
}

2.配置mapper,这是需要重写map

<resultMap id="BaseBidExtUserMap" type="com.powernode.model.vo.BidExtUser">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="loan_id" jdbcType="INTEGER" property="loanId" />
    <result column="uid" jdbcType="INTEGER" property="uid" />
    <result column="bid_money" jdbcType="DOUBLE" property="bidMoney" />
    <result column="bid_time" jdbcType="TIMESTAMP" property="bidTime" />
    <result column="bid_status" jdbcType="INTEGER" property="bidStatus" />
    <association property="user" javaType="com.powernode.model.User">
      //这里的 column="id"改成下面的形式,否则会被上面的id覆盖,这句话也可以删除,因为上面的uid就是这里的id
      <id column="uid" jdbcType="INTEGER" property="id"/>
      <result column="phone" jdbcType="VARCHAR" property="phone"/>
      <result column="login_password" jdbcType="VARCHAR" property="loginPassword"/>
      <result column="name" jdbcType="VARCHAR" property="name"/>
      <result column="id_card" jdbcType="VARCHAR" property="idCard"/>
      <result column="add_time" jdbcType="TIMESTAMP" property="addTime"/>
      <result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime"/>
      <result column="header_image" jdbcType="VARCHAR" property="headerImage"/>
    </association>
  </resultMap>

3.sql

select a.*,b.*
  from b_bid_info as a
  join u_user as b
  on a.uid=b.id
  where a.loan_id=#{id}
  order by a.bid_time desc
  limit 0,10

3.当字段较少并且不再同一个表,也可以新建一个类

1.新建一个类

public class BidVo implements Serializable {
    private String bidMoney;
    private String bidTime;
    private String bidProduct;

    public String getBidMoney() {
        return bidMoney;
    }

    public BidVo setBidMoney(String bidMoney) {
        this.bidMoney = bidMoney;
        return this;
    }

    public String getBidTime() {
        return bidTime;
    }

    public BidVo setBidTime(String bidTime) {
        this.bidTime = bidTime;
        return this;
    }

    public String getBidProduct() {
        return bidProduct;
    }

    public BidVo setBidProduct(String bidProduct) {
        this.bidProduct = bidProduct;
        return this;
    }
}

2.这里就不需要配置map,SQL语句如下

// resultType="com.powernode.model.vo.BidVo"是新建的类
<select id="queryRecentyBidByTime" resultType="com.powernode.model.vo.BidVo">
    select a.bid_money as bidMoney,a.bid_time as bidTime,
    b.product_name as bidProduct
    from b_bid_info as a
    left join b_loan_info as b
    on a.loan_id=b.id
    where a.uid=#{uid}
    order by a.bid_time desc
    limit 0,5

    </select>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值