Mybatis 3.0 参数的传递
2017-03-13 15:23
351 查看
在mybatis 中调用映射器执行sql 查询中参数的传递有多种形式,大致可以分为一下几类:
按照参数的顺序;
通过hashmap进行参数传递,其中map中的key对应映射器中的站位符#{key};
通过list进行参数传递,在sql中使用foreach标签对参数进行遍历;
对于接口中,采用注解的方式@Param(“占位符”),对接口中方法的入参进行修饰,在对应的映射器中使用#{占位符}进行参数的传递;
入参采用javaBean 对象;
详解
映射器sql:
调用接口:
public Person selectPersonMultiParam(String username,int age);
其中参数的顺序不能改变。
映射器:
调用接口:
public Person selectPersonMapParam(Map
其中:map中的key对应#{key},必须一致,否则会报错,找不到对应的参数。
映射器:
该查询语句返回多个结果,故其需要一个resultMap来修饰其结果集。
接口:
public List selectPersonWithListParam(List paramList);
测试:
映射器:
调用接口:
public Person selectPersonAnnotationParam(@Param(“username”) String username,@Param(“age”) int age);
测试:
映射器:
调用接口:
public Person selectPersonBeanParam(Person person);
测试:
关于mybatis 中参数的传递最简单的几种方式大概就这些,但在具体的应用中会将这些参数的传递混合使用,熟练的掌握,才能更好的运用。
按照参数的顺序;
通过hashmap进行参数传递,其中map中的key对应映射器中的站位符#{key};
通过list进行参数传递,在sql中使用foreach标签对参数进行遍历;
对于接口中,采用注解的方式@Param(“占位符”),对接口中方法的入参进行修饰,在对应的映射器中使用#{占位符}进行参数的传递;
入参采用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 中参数的传递最简单的几种方式大概就这些,但在具体的应用中会将这些参数的传递混合使用,熟练的掌握,才能更好的运用。
相关文章推荐
- mybatis传递多个参数
- MyBatis学习——参数的传递和返回
- Mybatis参数传递的集中写法和方式
- mybatis传递多个参数的问题
- mybatis参数传递的问题
- Mybatis传递多个参数
- MyBatis多参数传递之注解方式示例
- Spring MVC+Mybatis 执行存储过程,使用Map进行参数的传递
- mybatis 使用一个参数传递数值 sql主要是使用到 in
- mybatis 关联映射传递多参数方法
- mybatis常见问题处理:批量操作,传递多个参数,like语句写法等等
- mybatis 传递多个参数的方法(Multiple parameters)
- MyBatis学习之传递多个参数(一)
- Mybatis传递多个参数的解决办法(三种)
- Mybatis传递多个参数的解决办法(三种)
- myBatis的参数传递问题解析
- mybatis 传递一个参数,多个参数的应用
- mybatis补充教程之二:传递多个参数的方法(Multiple parameters)
- mybatis 中sql语句传递多个参数
- mybatis查询嵌套之参数传递