1. 程式人生 > >@Param在Mybatis中的使用

@Param在Mybatis中的使用

自己的一些總結,方便自己以後查詢,有不對的地方請指出來,一起提高。

1.如果mapper接口裡引數是兩個普通引數;如下圖

public List<student> selectuser(int pn ,String i);

<select id="selectuser"  resultType="com.user.entity.student">
        SELECT * FROM student
         where sname like concat(concat("%",#{1}),"%")
         LIMIT #{0} ,5    
</select>

那麼xml裡只能用#{0},#{1}的方式,但這樣的表達方法,不利於後期的維護。        可以用@Param的註解來修飾引數。xml裡看起來也比較方便,否則一堆0,1,2,3的真是難懂。
public List<student> selectuser(@Param(value = "page")int pn ,@Param(value = "str")String i);

<select id="selectuser"  resultType="com.user.entity.student">
    SELECT * FROM student
    where sname like concat(concat("%",#{str}),"%")
    LIMIT #{page} ,5
</select>

2,如果傳入的引數是基本型別引數和實體類物件。
public List<student> selectuser(@Param(value = "page")int pn ,@Param(value = "st")student student);

<select id="selectuser"  resultType="com.user.entity.student">
    SELECT * FROM student
    where sname like concat(concat("%",#{st.sname}),"%")
    LIMIT #{page} ,5
</select>

3.如果傳入的引數只有一個,基本上不用@Param這個註解了。正常用
public List<student> selectuser(int pn);

<select id="selectuser"  resultType="com.user.entity.student">
        SELECT * FROM student
        <!--where sname like concat(concat("%",#{st.sname}),"%")-->
        LIMIT #{page} ,5
    </select>

暫時知道的就是這些,有缺失的地方,以後會繼續補充。