这个注解在之前公司的培训上就使用过,但是那时候用的是公司的一个框架,也不知道到底在我自己搭的框架上好使不好使,今天在做毕业设计的时候正好遇到这个需求,我的表里只有另一张表的主键,但是用户关心的并不是他的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!