@Param在Mybatis中的使用


用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中 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>
优质内容筛选与推荐>>
1、[导入]puring
2、普通的智能手机可以集成热敏打印机吗
3、网赚经验之谈:成为高手之路
4、2008就要过去了
5、hdu4570Multi-bit Trie (间隙DP)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号