1 如果在查询方法中有多个参数,可以使用map对象将所有数据都存储进去。比如分页查询,需要用到两个参数,可以将这两个参数包装到map中。
例子:分页查询
dao层方法
public List<Student> getStudentPage(int pstart, int pnumber) throws Exception{ SqlSession sqlSession = MybatisUtil.getSqlSession(); Map<String,Integer> map = new HashMap<String, Integer>(); map.put("pstart", pstart); map.put("pnumber", pnumber); try{ return sqlSession.selectList(Student.class.getName() + ".getStudentPage", map); }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); }finally{ MybatisUtil.closeSqlSession(); } }
映射文件studentmapper.xml
<select id="getStudentPage" parameterType="map" resultMap="studentMap"> select id,sname,salary from student limit #{pstart},#{pnumber} </select>
时间: 2024-10-24 18:23:03