DO层:
package com.imooc.o2o.entity;
import java.util.Date;
public class ShopCategory {
private Long shopCategoryId;
private String shopCategoryName;
private String shopCategoryDesc;
private String shopCategoryImg;
private Integer priority;
private Date createTime;
private Date lastEditTime;
private ShopCategory parent;
public Long getShopCategoryId() {
return shopCategoryId;
}
public void setShopCategoryId(Long shopCategoryId) {
this.shopCategoryId = shopCategoryId;
}
public String getShopCategoryName() {
return shopCategoryName;
}
public void setShopCategoryName(String shopCategoryName) {
this.shopCategoryName = shopCategoryName;
}
public String getShopCategoryDesc() {
return shopCategoryDesc;
}
public void setShopCategoryDesc(String shopCategoryDesc) {
this.shopCategoryDesc = shopCategoryDesc;
}
public String getShopCategoryImg() {
return shopCategoryImg;
}
public void setShopCategoryImg(String shopCategoryImg) {
this.shopCategoryImg = shopCategoryImg;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastEditTime() {
return lastEditTime;
}
public void setLastEditTime(Date lastEditTime) {
this.lastEditTime = lastEditTime;
}
public ShopCategory getParent() {
return parent;
}
public void setParent(ShopCategory parent) {
this.parent = parent;
}
@Override
public String toString() {
return "ShopCategory [shopCategoryId=" + shopCategoryId
+ ", shopCategoryName=" + shopCategoryName
+ ", shopCategoryDesc=" + shopCategoryDesc
+ ", shopCategoryImg=" + shopCategoryImg
+ ", priority=" + priority + ", createTime="
+ createTime + ", lastEditTime=" + lastEditTime
+ ", parent=" + parent + "]";
}
}
1、不使用@param
DAO层:
以对象为参数
List<ShopCategory> queryShopCategory(ShopCategory shopCategory);
mapper.xml:
<select id="queryShopCategory" resultType="com.imooc.o2o.entity.ShopCategory">
SELECT
shop_category_id,
shop_category_name,
shop_category_desc,
shop_category_img,
priority,
create_time,
last_edit_time,
parent_id
FROM
tb_shop_category
<where>
<!-- <if test="shopCategoryCondition.parent != null">
and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
</if> -->
<if test="parent != null">
and parent_id = #{parent.shopCategoryId}
</if>
</where>
ORDER BY
priority DESC
</select>
2、使用@param
DAO层:
List<ShopCategory> queryShopCategory(@Param("shopCategoryCondition") ShopCategory shopCategory);
mapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.o2o.dao.ShopCategoryMapper">
<select id="queryShopCategory" resultType="com.imooc.o2o.entity.ShopCategory">
SELECT
shop_category_id,
shop_category_name,
shop_category_desc,
shop_category_img,
priority,
create_time,
last_edit_time,
parent_id
FROM
tb_shop_category
<where>
<if test="shopCategoryCondition.parent != null">
and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
</if>
</where>
ORDER BY
priority DESC
</select>
</mapper>
由此可以看出,mybatis在xml中做值的注入时:
1. 若是不使用@param,则若参数为对象,则在#{}中则直接写对象中的属性。无法根据对象来绑定。
例如上例中的and parent_id = #{parent.shopCategoryId}。参数为ShopCategory。
但是由于没有用@param做参数别名,因此在#{}无法直接引用参数,只能直接通过对象参数的属性来进行#{}注入。
2. 但是如果使用了@param。则对象参数在进行#{}注入时可以使用别名。例如下例子中 shopCategoryCondition就是DAO层定义的方法中的参数shopCategory的别名
<if test="shopCategoryCondition.parent != null">
and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
</if>