Mybatis 配置resultMap一对多关联映射

时间:2020-12-25 07:05:40

resultMap配置:

Mybatis 配置resultMap一对多关联映射

引用:

Mybatis 配置resultMap一对多关联映射

PO类:

Mybatis 配置resultMap一对多关联映射

接口:

Mybatis 配置resultMap一对多关联映射

测试:

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一对多关联映射