前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >@Param在Mybatis中的使用

@Param在Mybatis中的使用

作者头像
唐怀瑟
发布2018-08-30 11:12:50
4K0
发布2018-08-30 11:12:50
举报
文章被收录于专栏:Java面试笔试题Java面试笔试题

用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中 1.如果mapper接口里参数是两个普通参数;如下图

代码语言:javascript
复制
public List<student> selectuser(int pn ,String i);
代码语言:javascript
复制
<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的真是难懂。

代码语言:javascript
复制
public List<student> selectuser(@Param(value = "page")int pn ,@Param(value = "str")String i);
代码语言:javascript
复制
<select id="selectuser"  resultType="com.user.entity.student">
    SELECT * FROM student
    where sname like concat(concat("%",#{str}),"%")
    LIMIT #{page} ,5
</select>

2,如果传入的参数是基本类型参数和实体类对象。

代码语言:javascript
复制
public List<student> selectuser(@Param(value = "page")int pn ,@Param(value = "st")student student);
代码语言:javascript
复制
<select id="selectuser"  resultType="com.user.entity.student">
    SELECT * FROM student
    where sname like concat(concat("%",#{st.sname}),"%")
    LIMIT #{page} ,5
</select>

3.如果传入的参数只有一个,基本上不用@Param这个注解了。正常用

代码语言:javascript
复制
public List<student> selectuser(int pn);
代码语言:javascript
复制
<select id="selectuser"  resultType="com.user.entity.student">
        SELECT * FROM student
        <!--where sname like concat(concat("%",#{st.sname}),"%")-->
        LIMIT #{page} ,5
</select>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档