resultMap配置:

Mybatis 配置resultMap一对多关联映射_Test

引用:

Mybatis 配置resultMap一对多关联映射_Test_02

PO类:

Mybatis 配置resultMap一对多关联映射_sql_03

接口:

Mybatis 配置resultMap一对多关联映射_User_04

测试:

public class UserMapperTest {
    
    private SqlSessionFactory sqlSessionFactory = null;

    @Before
    public void init() throws Exception {
        // 第一步:创建一个SQLSessionFactoryBuilder对象。
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 第二步:加载配置文件。
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 第三步:创建SQLSessionFactory对象
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void testGetUserById() {
        //和spring整合后省略
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //获得代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUserById(10);
        System.out.println(user);
        
        //和spring整合后省略
        sqlSession.close();
    }

    @Test
    public void testGetUserByName() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = userMapper.getUserByName("张");
        for (User user : list) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void testInsertUser() {
        fail("Not yet implemented");
    }

}

Mybatis 配置resultMap一对多关联映射_User_05