MyBatis(四)多参数处理问题
作者:互联网
这里总结了关于多参数传递时,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 }
标签:map,openSession,处理,lastName,public,参数,Emp,MyBatis,id 来源: https://www.cnblogs.com/wanerhu/p/10713813.html