- Mybatis 中又3种查询 seleteOne, seleteList, seleteMap.
- SeleteOne 查询单个对象:
映射文件:
<!-- 根据id查询 --> <select id="findById" parameterType="int" resultType="cn.wh.vo.Role"> select * from t_role where id = #{id} </select> <select id="totalCount" resultType="int"> select count(*) from t_role </select>
测试
//查询单个 @Test public void testSelectOne(){ Role role = (Role)session.selectOne("cn.wh.mapper.RoleMapper.findById",1); System.out.println(role.getName()); } //查询count @Test public void testSelectCount(){ Integer count = (Integer)session.selectOne("cn.wh.mapper.RoleMapper.totalCount"); System.out.println(count); }
- selectList 查询结果集:
//查询所有 @Test public void testSelectList(){ List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.findAll"); for(Role role:list){ System.out.println(role.getId()+"----"+role.getName()); } }
- selectMap 查询Map集合:
@Test public void testSelectMap(){ //第二参数是列名 作为 map的key Map<Integer,Role> map = session.selectMap("cn.wh.mapper.RoleMapper.findAll", "id"); Iterator<Integer> iter = map.keySet().iterator(); while(iter.hasNext()){ Integer key = iter.next(); System.out.println(key+"--------"+map.get(key).getName()); } }