mybatis中自定义1对1的resultMap

来源:互联网 发布:水泥自流平环保吗 知乎 编辑:程序博客网 时间:2024/06/17 20:21

1·首先是javabean:

public class jeweryCartTbl {    private String id;    private String memberid;    private String jeweryAllId;    private String type;    private Integer count;    private String ischeck;    private Date createTime;    private Date lastmodifyTime;    private String categoryid;        private JeweryTbl jeweryTbl;  <span style="color:#FF0000;"><strong>//这个是1对1的属性值</strong></span>        public jeweryTbl getjeweryTbl() {return jeweryTbl;}public void setjeweryTbl(jeweryTbl jeweryTbl) {this.jeweryTbl = jeweryTbl;}public String getId() {        return id;    }    public void setId(String id) {        this.id = id == null ? null : id.trim();    }    public String getMemberid() {        return memberid;    }    public void setMemberid(String memberid) {        this.memberid = memberid == null ? null : memberid.trim();    }    public String getjeweryAllId() {        return jeweryAllId;    }    public void setjeweryAllId(String jeweryAllId) {        this.jeweryAllId = jeweryAllId == null ? null : jeweryAllId.trim();    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type == null ? null : type.trim();    }    public Integer getCount() {        return count;    }    public void setCount(Integer count) {        this.count = count;    }    public String getIscheck() {        return ischeck;    }    public void setIscheck(String ischeck) {        this.ischeck = ischeck == null ? null : ischeck.trim();    }    public Date getCreateTime() {        return createTime;    }    public void setCreateTime(Date createTime) {        this.createTime = createTime;    }    public Date getLastmodifyTime() {        return lastmodifyTime;    }    public void setLastmodifyTime(Date lastmodifyTime) {        this.lastmodifyTime = lastmodifyTime;    }    public String getCategoryid() {        return categoryid;    }    public void setCategoryid(String categoryid) {        this.categoryid = categoryid == null ? null : categoryid.trim();    }}

2·编写resultMap

<span style="color:#FF0000;"><strong>//编写对应的resultMap//1·首先property与javabean中的属性一致//2·如果两个关联的有相同的属性 例如:id 设置成不同的属性</strong></span><resultMap id="BaseResultMap_1" type="com.model.jeweryCartTbl" >    <id column="id" property="id" jdbcType="VARCHAR" />    <result column="memberid" property="memberid" jdbcType="VARCHAR" />    <result column="jewery_all_id" property="jeweryAllId" jdbcType="VARCHAR" />    <result column="type" property="type" jdbcType="VARCHAR" />    <result column="count" property="count" jdbcType="INTEGER" />    <result column="ischeck" property="ischeck" jdbcType="VARCHAR" />    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />    <result column="lastmodify_time" property="lastmodifyTime" jdbcType="TIMESTAMP" />    <result column="categoryid" property="categoryid" jdbcType="VARCHAR" />    <association property="jeweryTbl" javaType="com.model.jeweryTbl">     <id <span style="color:#FF0000;"><strong>column="jeweryid"</strong></span> property="id" jdbcType="VARCHAR" />    <result column="name" property="name" jdbcType="VARCHAR" />    <result column="price" property="price" jdbcType="DECIMAL" />    </association>  </resultMap>

3·调用:

  //相同的属性定义重命名  //如果不设置不同名字。相同的属性值会显示一个值(会覆盖)  <select id="findCheckedCartByMember" resultMap="BaseResultMap_1">    select dct.*,dt.price,<span style="color:#FF0000;"><strong>dt.id as jeweryid</strong></span>    from jewery_cart_tbl dct    left join jewery_category_value_tbl dcvt    on dct.jewery_all_id = dcvt.idleft join jewery_tbl dton dcvt.jewery_all_id = dt.id    where dct.ischeck = '1' and dt.is_sale = '1'    <if test="memberid != null">    and dct.memberid = #{memberid,jdbcType=VARCHAR}    </if>  </select>


0 0