首先:MyBatis的关联查询!!!(一对一、一对多、多对多)-CSDN博客以这个项目为基础。
1.在UserMapper接口中创建一个方法:
package com.by.mapper;
import com.by.pojo.User;
import java.util.List;
/**
* <p>Project: mybatis - UserMapper</p>
* <p>Powered by scl On 2023-12-22 15:52:05</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public interface UserMapper {
/**
* 一对多的延迟加载
*/
User findUserById(Integer id);
}
2.在UserMapper.xml文件在实现这个方法:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.by.mapper.UserMapper">
<resultMap id="findAllResultMap" type="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="birthday" property="birthday"></result>
<result column="sex" property="sex"></result>
<result column="address" property="address"></result>
<collection property="accountList"
ofType="account"
select="com.by.mapper.AccountMapper.selectAccountByUid"
column="id"
fetchType="lazy">
<id column="aid" property="id"></id>
<result column="uid" property="uid"></result>
<result column="money" property="money"></result>
</collection>
</resultMap>
<!--一对多-->
<select id="findAllUserAccount" parameterType="int" resultMap="findAllResultMap">
select a.id aid, a.uid uid, a.money money, u.*
from user u
left join account a on u.id = a.uid
where u.id = #{id}
</select>
<!--一对多延迟加载-->
<select id="findUserById" parameterType="int" resultMap="findAllResultMap">
select * from user where id=#{id}
</select>
</mapper>
3.在AccountMapper.xml文件中实现这个方法:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.by.mapper.AccountMapper">
<!--一对多延迟加载-->
<select id="selectAccountByUid" resultType="account">
select * from account where uid=#{uid}
</select>
</mapper>
4.测试类:
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by;
import com.by.mapper.AccountMapper;
import com.by.mapper.RoleMapper;
import com.by.mapper.UserMapper;
import com.by.pojo.Account;
import com.by.pojo.Role;
import com.by.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* <p>Project: mybatis - MyBatisTest</p>
* <p>Powered by scl On 2023-12-18 11:44:53</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class MyBatisTestRole {
private InputStream inputStream;
private SqlSession sqlSession;
@Before
public void init() throws IOException {
// 加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
// 创建sqlSessionFActory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得数据的绘画实例
sqlSession = sessionFactory.openSession();
}
/**
* 一对多延迟加载
*
* @throws IOException
*/
@Test
public void findUserById() {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(41);
System.out.println(user.getUsername());
//将注释接触就可以触发懒加载
//List<Account> accountList = user.getAccountList();
//for (Account account : accountList) {
// System.out.println(account);
//}
}
@After
public void close() throws IOException {
inputStream.close();
sqlSession.close();
}
}
5.结果展示:
总结:懒加载的触发条件是执行"equals", “clone”, “hashCode”, "toString"这4个方法。或者是执行getComments,获取懒加载的对象时。
注意事项1:如果你报了这样的错不要着急,看我表演:
解决方法:
注意事项2:如果你报了这个错,你就要看看你的返回值类型是否写错了。
解决方法:仔细查看这两个地方
最后再补充一下开启懒加载的配置:
(1)局部开启懒加载:
(2)全局懒加载:在mybaits-config配置文件中加上配置信息
<settings>
<!-- <!– 开启懒加载(开启延迟加载)–>-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>