Mybatis 3.0 参数的传递

来源:互联网 发布:艾克里里用什么软件 编辑:程序博客网 时间:2024/04/30 14:53

在mybatis 中调用映射器执行sql 查询中参数的传递有多种形式,大致可以分为一下几类:

  1. 按照参数的顺序;
  2. 通过hashmap进行参数传递,其中map中的key对应映射器中的站位符#{key};
  3. 通过list进行参数传递,在sql中使用foreach标签对参数进行遍历;
  4. 对于接口中,采用注解的方式@Param(“占位符”),对接口中方法的入参进行修饰,在对应的映射器中使用#{占位符}进行参数的传递;
  5. 入参采用javaBean 对象;

详解

按照参数的顺序

在调用接口中,参数的顺序对应映射器中sql的占位符的索引的顺序,且索引的位置从0开始。如:
映射器sql:

<!-- mybatis中传入多个参数 ,由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始 -->    <select id="selectPersonMultiParam" resultType="Person">        <![CDATA[            SELECT p.id,p.username,p.age from person p            where p.username like #{0} and p.age=#{1}        ]]>    </select>

调用接口:
public Person selectPersonMultiParam(String username,int age);
其中参数的顺序不能改变。

2. 通过map进行参数传递

在调用接口中,所要求传入的参数全都放入一个map 对象中,通过该对象完成参数的传递。Map中的key对应映射器中的站位符;如:
映射器:

<select id="selectPersonMapParam" resultType="Person"        parameterType="map">        <![CDATA[            SELECT p.id,p.username,p.age from person p            where p.username like #{username} and p.age=#{age}        ]]>    </select>

调用接口:
public Person selectPersonMapParam(Map

@Test    public void selectPersonMapParam(){        session = sqlSessionFactory.openSession();        IPersonDao personDao = session.getMapper(IPersonDao.class);        Map<String,Object> hashMap=new HashMap<String, Object>();        hashMap.put("username", "b%");        hashMap.put("age", 24);        Person person = personDao.selectPersonMapParam(hashMap);        System.out.println(person);    }

其中:map中的key对应#{key},必须一致,否则会报错,找不到对应的参数。

3. 通过list进行参数传递

当映射器中执行 select …. in ….. 语句是采用可以采用list进行传递参数:如:
映射器:

<!-- List 封装 in :其中 collection="list" 自己配置好的,直接使用就行-->    <select id="selectPersonWithListParam" resultMap="listPersons">        select p.id,p.username,p.age from person p        where p.id in        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">            #{item}        </foreach>      </select>

该查询语句返回多个结果,故其需要一个resultMap来修饰其结果集。
接口:
public List selectPersonWithListParam(List paramList);
测试:

    @Test    public void selectPersonListParam(){        session = sqlSessionFactory.openSession();        IPersonDao personDao = session.getMapper(IPersonDao.class);        List<Integer> paramList=new ArrayList<Integer>();        paramList.add(1);        paramList.add(2);        List<Person> persons = personDao.selectPersonWithListParam(paramList);        for(Person person:persons){            System.out.println(person);        }    }

4. 采用入参注解的方式

最简便的方式:即在调用接口中的入参时,采用@Param(“占位符”)修饰所要传入的参数,在映射器条件语句块中 #{占位符} 对应注解的参数。如:
映射器:

<!-- 采用注解的方式 :其中#{}中的命名表达试即接口中的入参中采用@Param()所修饰的命名表达式-->    <select id="selectPersonAnnotationParam" resultType="Person">        <![CDATA[            SELECT p.id,p.username,p.age from person p            where p.username like #{username} and p.age=#{age}        ]]>    </select>

调用接口:
public Person selectPersonAnnotationParam(@Param(“username”) String username,@Param(“age”) int age);
测试:

@Test    public void selectPersonAnnotationParam(){        session = sqlSessionFactory.openSession();        IPersonDao personDao = session.getMapper(IPersonDao.class);        Person person = personDao.selectPersonAnnotationParam("a%", 23);        System.out.println(person);    }

5. 入参为JavaBean 对象

也可以通过将传入的参数封装成javaBean 对象,将该对象作为入参,但前提是映射器中的占位符必须与javabean 属性一致。如:
映射器:

<!-- 入参是 javaBean 对象 -->    <select id="selectPersonBeanParam" resultType="Person"    parameterType="Person">    select p.id,p.username,p.age from person p        where p.id=#{id} and p.username like #{username}    </select>

调用接口:
public Person selectPersonBeanParam(Person person);
测试:

@Test    public void selectPersonBeanParam(){        session = sqlSessionFactory.openSession();        IPersonDao personDao = session.getMapper(IPersonDao.class);        Person person=new Person();        person.setId(2);        person.setUsername("b%");        Person p = personDao.selectPersonBeanParam(person);        System.out.println(p);    }

关于mybatis 中参数的传递最简单的几种方式大概就这些,但在具体的应用中会将这些参数的传递混合使用,熟练的掌握,才能更好的运用。

0 0
原创粉丝点击