欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis连表查询返回map类型

程序员文章站 2022-07-01 07:54:24
...
  • mapper.xml
<select id="test1" resultType="Map">
        select t.tid,t.tname,c.cname from teacher t join course c on t.tid=c.tid
    </select>
  • mapper.java
 //1.查询老师信息和其课程信息
   List<Map<String,Object>> test1();
  • Test
@Test
    public void test1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TestMapper mapper = sqlSession.getMapper(TestMapper.class);


        List<Map<String, Object>> maps = mapper.test1();
        
        for(Map<String,Object> map:maps){
            Set<String> set=map.keySet();
            Iterator<String> it=set.iterator();
            while(it.hasNext()){
                Object key=it.next();
                Object value=map.get(key);
                System.out.println(value);
            }
        }
        sqlSession.close();
    }

参考