MyBatis学习笔记:Mybatis如何传参给持久层接口,以及sql语句如何得到参数的值

这片文章是对自己学习的总结。

闲话不多说,直接看方法。

  • 普通传参

select标签中,parameterType的值是一个普通类型时,sql语句可以直接用#{}读取。看下面例子,我们先定义一个持久层接口,里面有个方法是根据id查找User对象,那么参数就应该是传入id,类型是int。

package com.itheima.Dao;
public interface UserDao {
    public User findUserById(int id);
}

那么对应的UserDao.xml就应该是

<mapper namespace="com.itheima.dao.UserDao">
    <select id="findUserById" parameterType="int" resultType="com.itheima.domain.User">
        select * from users where user_id = #{id}
    </select>
</mapper>

注意其中的sql语句,我们可以直接用#{id}来表示传进来的参数。因为接口定义的参数名就是id,所以#{}里填写id,即用#{id}来表示传进来的参数id的值。

我们还可以是用#{0}来表示参数id的值。用0来表示第一个传进来的参数的值。但不推荐这样做

  • 参数类型是List,Map等集合

对Mybatis的学习,我们知道,如果持久层方法有参数要传进来,<select>等标签中必须对属性parameterType赋值(不了解的可以看这篇文章)。但问题是,xml这种写法没法让我们传入多个参数,毕竟parameterType只能赋一个值,所以只能传入一个参数。如果我们想使用xml传入多个参数,那么一般我们是传入一个List,Map等集合来达到这个目的。

上面的例子我们修改一些,改成根据id的List集合返回对应的Userlist。

package com.itheima.Dao;
public interface UserDao {
    public List<User> findUsersByIds(List<int> idList);
}

那UserDao.xml的就应该这样写。

<mapper namespace="com.itheima.dao.UserDao">
    <select id="findUserById" parameterType="list" resultType="com.itheima.domain.User">
        select * from users where user_id in
        <foreach index="index" collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
</mapper>

其中涉及到<foreach>标签,不熟悉的可以看Mybaits学习笔记:映射器中的<foreach>遍历标签。不想看的可以理解成foreach标签是在对参数idList遍历,#{item}就是动态地代表其中元素的值。

如果传进来的参数不是普通类型,是封装好的类,只要大家理解#{}的含义,那也不难解决这个问题。了解#{}详细细节还是要看

MyBatis学习笔记:映射器中的标签

以上是参数是List类型,如果是Map类型,那就parameterType的值改一下,然后sql中#{}的大括号内要写key的值才能取到对应的value。我们再把例子改一下,变成根据用户的id和name找到对应的User并返回

package com.itheima.Dao;
public interface UserDao {
    public User findUser(Map<String, Object> id);
}

String name = "user1";
int id = 1;
Map map = new Map();
map.put("name", name);
map.put("id", id);
User user = roleMaper.findUser(map);

对应的map中有两个键值对,name和id,我们在UserDao.xml中可以使用#{id}和#{key}取到对应的value值

<mapper namespace="com.itheima.dao.UserDao">
    <select id="findUserById" parameterType="list" resultType="com.itheima.domain.User">
        select * from users where user_id = #{id} and user_name = #{name}
    </select>
</mapper>

如果Map中是一个封装类,我们要取封装类中的某个变量,那我们要写成#{item.id}这样的形式。

  • 参数类型是封装类,获取类中的成员变量

如果传进来的是一个User类,User类中有id和name两个成员变量,我们要获取到类中的id,那么只需要直接写成员变量名就好#{id}。

  • 使用注解传递参数

我们可以不用xml来表明参数,转而使用注解,这种比较方便。

还是上面那个例子,根据用户的id和name找到对应的User并返回。接口添加注解,代码如下。

package com.itheima.Dao;
import org.apache.ibatis.annotations.Param;
public interface UserDao {
    public User findUser(@Param("userName") String name, @Param("userId") int id);
}

在接口这里在参数前添加注解@Param,里面只有一个参数就是一个字符串。例子中第一个参数的注解参数是userName,那么在UserDao.xml中就可以使用#{userName}得到第一个参数的值,注意是userName而不是name。

在UserDao.xml中,<select>标签下就不用对parameterType赋值。

<mapper namespace="com.itheima.Dao.UserDao">
    <select id="findAll" resultType="com.itheima.domain.User">
        select * from users where user_id = #{userId} and user_name = #{userName}
    </select>
</mapper>

猜你喜欢

转载自blog.csdn.net/sinat_38393872/article/details/100746469
今日推荐