MyBatis 使用多表联查,两张表中字段重复时,采用取别名方法解决
结果显示图:
product.* 里面包含了name属性,category也有name属性,所以将category.name
取了别名"category_name"
映射 :
<resultMap id="ResultMapSth" type="com.bmcs.mall.web.vo.ProductCateVO">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<collection property="categories" ofType="com.bmcs.mall.dal.entity.SimpleCategory">
<result property="name" column="category_name"/>
</collection>
</resultMap>
MyBatis :
<select id="findByPages" parameterType="com.bmcs.mall.web.vo.PageVO" resultMap="ResultMapSth">
select product.*,category.`name` "category_name" from product AS product INNER JOIN category AS
category ON category.id = product.categoryid
where product.`status`!=0
</select>
实体类 :
public class SimpleCategory {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ProductCateVO {
private Integer id;//商品编号
private String name;//商品名称
private List<SimpleCategory> categories;
public List<SimpleCategory> getCategories() {
return categories;
}
public void setCategories(List<SimpleCategory> categories) {
this.categories = categories;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}