多语言展示
当前在线:832今日阅读:167今日分享:16

mybatis使用association分步查询关联属性查询

mybatis使用association分步查询关联属性查询
工具/原料
1

mybatis

2

eclipse

方法/步骤
1

我们的程序是要查询员工信息的时候查询出相应的部门信息。

2

在xml中定义员工分步查询的sql实现。先按照员工id查询员工信息。根据查询员工信息中的部门id去部门表查出部门信息。然后把部门设置到员工中。

3

定义员工信息返回的resultMap。                                                                                                                       

4

定义将员工的那一列的值传值给部门。

5

编写单元测试类。 @Test        public void test1() throws Exception {                String resource = 'mybatis-config.xml';                InputStream inputStream = Resources.getResourceAsStream(resource);                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);                                SqlSession sqlSession = sqlSessionFactory.openSession();                                try {                        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);                                                Map employee = employeeMapper.getEmpByIdReturnMap(1);                        System.out.println(employee);                }finally {                        sqlSession.close();                }        }

6

执行单元测试,查看部门的信息是否能够查询出来。

推荐信息