@Transient注解的使用

这个注解在之前公司的培训上就使用过,但是那时候用的是公司的一个框架,也不知道到底在我自己搭的框架上好使不好使,今天在做毕业设计的时候正好遇到这个需求,我的表里只有另一张表的主键,但是用户关心的并不是他的id,而是要通过这个id去找到用户关心的字段。用了这个注解就省去去写一个实体类的麻烦,它可以很好的用在mybatis的多表查询上(好像从网上听说这个注解是Hibernate的一个注解?),可以在一张表所对应的实体上添加属性,加上这个注解,在进行单表的增删改查的时候即忽略这个字段,很好用的一个东西。

使用它需要修改两个地方:

1:在实体集中声明一个属性,并设置它的setter和getter方法,并在字段上面加上这个注解:

    @Transient
    private String cptName;

2:在mybatis文件中的resultMap里也要配置一下:

<resultMap id="BaseResultMap" type="com.tracy.gd.domain.LendingApply">
        <id column="la_id" property="laId" jdbcType="INTEGER"/>
        <result column="la_user_id" property="laUserId" jdbcType="INTEGER"/>
        <result column="la_cpt_id" property="laCptId" jdbcType="INTEGER"/>
        <result column="la_reason" property="laReason" jdbcType="VARCHAR"/>
        <result column="la_lend_time" property="laLendTime" jdbcType="TIMESTAMP"/>
        <result column="la_return_time" property="laReturnTime" jdbcType="TIMESTAMP"/>
        <result column="la_commons" property="laCommons" jdbcType="VARCHAR"/>
        <result column="la_location" property="laLocation" jdbcType="VARCHAR"/>
        <result column="la_is_check" property="laIsCheck" jdbcType="VARCHAR"/>
        <result column="attribute1" property="attribute1" jdbcType="VARCHAR"/>
        <result column="attribute2" property="attribute2" jdbcType="VARCHAR"/>
        <result column="attribute3" property="attribute3" jdbcType="VARCHAR"/>
        <result column="cpt_name" property="cptName" jdbcType="VARCHAR"/>
    </resultMap>
然后在select标签中即可进行多表查询,举一反三,可以定义多个,最后可以从实体中拿到这个属性。达到目的,bingo!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值