码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis(四)多参数处理问题

时间:2019-04-15 22:58:09      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:传递   问题   email   temp   out   color   turn   ati   obj   

这里总结了关于多参数传递时,MyBatis接收的三种方式。

(1)接口中编写方法

1   public Emp getEmpByParams(Integer id,String lastNmae);
2     
3     public Emp getEmpByParam(@Param("id")Integer id,@Param("lastName")String lastNmae);
4     
5     public Emp getEmpByParam(Map<String, Object> map);

(2)编写Mapper文件

  <select id="getEmpByParams" resultType="com.eu.bean.Emp">
        select id,last_name lastName,gender geder,email from Emp where id = #{param1} and last_name= #{param2}
   </select>
  <select id="getEmpByParam" resultType="com.eu.bean.Emp">
        select id,last_name lastName,gender geder,email from Emp where id = #{id} and last_name= #{lastName}
    </select>

 (3)编写测试

 1   public SqlSessionFactory getSqlSessionFactory() throws IOException {
 2         String resource = "conf/mybatis-config.xml";
 3         InputStream inputStream = Resources.getResourceAsStream(resource);
 4         return new SqlSessionFactoryBuilder().build(inputStream);
 5     }
 6   @Test
 7     public void testMapperPar() throws IOException {
 8         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
 9         //1.获取到sqlsession 不会自动提交数据
10         SqlSession openSession = sqlSessionFactory.openSession();
11         EmpDao mapper = openSession.getMapper(EmpDao.class);
12         
13         Emp emp = mapper.getEmpByParam(1, "张三");
14         System.out.println(emp);
15         
16         //手动提交数据
17         openSession.commit();
18         openSession.close();
19     }
20     
21     @Test
22     public void testMapperMap() throws IOException {
23         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
24         //1.获取到sqlsession 不会自动提交数据
25         SqlSession openSession = sqlSessionFactory.openSession();
26         EmpDao mapper = openSession.getMapper(EmpDao.class);
27         
28         Map<String, Object> map=new HashMap<String, Object>();
29         map.put("id", 1);
30         map.put("lastName", "张三");
31         Emp emp = mapper.getEmpByParam(map);
32         System.out.println(emp);
33         
34         //手动提交数据
35         openSession.commit();
36         openSession.close();
37     }

 

MyBatis(四)多参数处理问题

标签:传递   问题   email   temp   out   color   turn   ati   obj   

原文地址:https://www.cnblogs.com/wanerhu/p/10713813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!