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>

  DO层:

 1 package com.imooc.o2o.entity;
 2 
 3 import java.util.Date;
 4 
 5 public class ShopCategory {
 6     
 7     private Long shopCategoryId;
 8     private String shopCategoryName;
 9     private String shopCategoryDesc;
10     private String shopCategoryImg;
11     private Integer priority;
12     private Date createTime;
13     private Date lastEditTime;
14     private ShopCategory parent;
15     public Long getShopCategoryId() {
16         return shopCategoryId;
17     }
18     public void setShopCategoryId(Long shopCategoryId) {
19         this.shopCategoryId = shopCategoryId;
20     }
21     public String getShopCategoryName() {
22         return shopCategoryName;
23     }
24     public void setShopCategoryName(String shopCategoryName) {
25         this.shopCategoryName = shopCategoryName;
26     }
27     public String getShopCategoryDesc() {
28         return shopCategoryDesc;
29     }
30     public void setShopCategoryDesc(String shopCategoryDesc) {
31         this.shopCategoryDesc = shopCategoryDesc;
32     }
33     public String getShopCategoryImg() {
34         return shopCategoryImg;
35     }
36     public void setShopCategoryImg(String shopCategoryImg) {
37         this.shopCategoryImg = shopCategoryImg;
38     }
39     public Integer getPriority() {
40         return priority;
41     }
42     public void setPriority(Integer priority) {
43         this.priority = priority;
44     }
45     public Date getCreateTime() {
46         return createTime;
47     }
48     public void setCreateTime(Date createTime) {
49         this.createTime = createTime;
50     }
51     public Date getLastEditTime() {
52         return lastEditTime;
53     }
54     public void setLastEditTime(Date lastEditTime) {
55         this.lastEditTime = lastEditTime;
56     }
57     public ShopCategory getParent() {
58         return parent;
59     }
60     public void setParent(ShopCategory parent) {
61         this.parent = parent;
62     }
63     @Override
64     public String toString() {
65         return "ShopCategory [shopCategoryId=" + shopCategoryId + ", shopCategoryName=" + shopCategoryName
66                 + ", shopCategoryDesc=" + shopCategoryDesc + ", shopCategoryImg=" + shopCategoryImg + ", priority="
67                 + priority + ", createTime=" + createTime + ", lastEditTime=" + lastEditTime + ", parent=" + parent
68                 + "]";
69     }
70     
71     
72 }

2.使用@param

  DAO层:

List<ShopCategory> queryShopCategory(@Param("shopCategoryCondition") ShopCategory shopCategory);

  mapper.xml:

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 <mapper namespace="com.imooc.o2o.dao.ShopCategoryMapper">
 5     <select id="queryShopCategory" resultType="com.imooc.o2o.entity.ShopCategory">
 6         SELECT
 7         shop_category_id,
 8         shop_category_name,
 9         shop_category_desc,
10         shop_category_img,
11         priority,
12         create_time,
13         last_edit_time,
14         parent_id
15         FROM
16         tb_shop_category
17         <where>
18             <if test="shopCategoryCondition.parent != null">
19                 and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
20             </if>
24         </where>
25         ORDER BY 
26         priority DESC
27     </select>
28 </mapper>

 

由此可以看出,mybatis在xml中做值的注入时,若是不使用@param,则若参数为对象,则在#{}中则直接写对象中的属性。无法根据对象来绑定。例如上例中的and parent_id = #{parent.shopCategoryId}。参数为ShopCategory。但是由于没有用

@param做参数别名,因此在#{}无法直接引用参数,只能直接通过对象参数的属性来进行#{}注入。

但是如果使用了@param。则对象参数在进行#{}注入时可以使用别名。例如下例子中 shopCategoryCondition就是DAO层定义的方法中的参数shopCategory的别名

  <if test="shopCategoryCondition.parent != null">
      and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
  </if>