mybatis中@param的使用与否

        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>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值