您的位置:首页 > 其它

mybatis中自定义1对1的resultMap

2015-07-01 17:22 246 查看
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.id
left join jewery_tbl dt
on 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: