@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、4这样不容易理解。如可以使用这样来实现:

public List<student> selectuser(@Param(value = "page")int pn ,@Param(value = "str")String i);

这样在Mapper中就可以使用value="page"这里面的值了。

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

=====================================================================

2.如果传入的参数只有一个,基本上不用@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>

====================================================================

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

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>

4.如果传入的参数是List参数。

数据接口层:

public int deleteBatch(@Param("ids")List<String>  ids);

mapper层:

<delete id="deleteBatch">
DELETE FROM s_user
WHERE id in
<foreach item="ids" collection="ids" open="(" separator=","close=")">
#{ids}
</foreach>
</delete>

=================================================

这里说下罗列下

mybatis传入List参数或者单个String 参数等问题

一:在Userdao里面定义一个方法

 public int deleteBatch(List<String>  ids);

在对应的UserDao.xml

错误写法

<delete id="deleteBatch">
DELETE FROM s_user
WHERE id in
<foreach item="ids" collection="ids" open="(" separator=","close=")">
#{ids}
</foreach>
</delete>

错误原因是:

你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。

正确写法

<delete id="deleteBatch">
DELETE FROM s_user
WHERE id in
<foreach item="ids" collection="list" open="(" separator=","close=")">
#{ids}
</foreach>
</delete>

2:你也可以使用@Param("ids") 来指定参数名称,如以下写法正确

Userdao.java

 public int deleteBatch(@Param("ids")List<String>  ids);

UserDao.xml

<delete id="deleteBatch">
DELETE FROM s_user
WHERE id in
<foreach item="ids" collection="ids" open="(" separator=","close=")">
#{ids}
</foreach>
</delete>

这样则正确。

二:如果你的入参只有一个参数并且类型是String,则需要注意:

UserDao,java

public int delete(String  id);

在对应的UserDao.xml

错误写法

<delete id="delete">
DELETE FROM s_user
WHERE id =#{id}
</delete>

正确写法

<delete id="delete">
DELETE FROM s_user
WHERE id =#{_id}
</delete>

原因是:Mybatis 对于传入的参数是单个string类型的参数的时候,明确表示要用:#{_parameter}这样的格式才能够获取到传入参数的值。

2.你还可以@Param("id") 来指定参数名称,如以下写法正确

Userdao.java

 public int delete(@Param("id")String id);

UserDao.xml

<delete id="delete">
DELETE FROM s_user
WHERE id =#{id} 
</delete>

posted @ 2022-09-20 13:16  liftsail  阅读(201)  评论(0编辑  收藏  举报