mybatis 之 mybatis中查询

  1. Mybatis 中又3种查询 seleteOne, seleteList, seleteMap.
  2. 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);
        }

     

  3. 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());
            }
        }

     

  4. 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());
            }
        }

     

posted on 2017-05-02 14:28  forever_2h  阅读(186)  评论(0编辑  收藏  举报

导航