Mybatis 配置resultMap一对多关联映射

resultMap配置:

引用:

PO类:

接口:

测试:

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");
    }

}

 

posted @ 2019-01-19 17:04  学亮编程手记  阅读(309)  评论(0编辑  收藏  举报