MyBatis之一对多映射
温故而知新:
上个项目我们学习了关于Mybatis的多对一映射,今天学习一对多映射,它的概念也很好理解,就好比一个老师可以管理一个班学生的关系。下面我们进行实操!
1.编写实体类(Student与Teacher)
Student
@Data
public class Student
{
private int id;
private String name;
private int tid;
}
Teacher
@Data
public class Teacher
{
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
2.编写接口(StudentMapper与TeacherMapper)
StudentMapper
public interface StudentMapper
{
}
TeacherMapper
//获取老师
List<Teacher> getTeacher();
//根据id查询
//按照结果嵌套查询
Teacher getTeacherById(@Param("tid") int id);
//分布嵌套查询
Teacher getTeacherById2(@Param("tid") int id);
3.编写接口对应的 mapper.xml配置文件(StudentMapper.xml与TeacherMapper.xml)
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzx.dao.StudentMapper">
</mapper>
TeacherMapper.xml(提供两种方法)
- 按照结果嵌套查询
<!--按照结果嵌套查询--> <select id="getTeacherById" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid = t.id and t.id = #{tid} </select> <resultMap id="TeacherStudent" type="Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!--复杂的属性,我们需要单独处理 对象:association 集合:collection javaType="" 指定属性的类型 集合中的泛型信息,我们使用ofType获取 --> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap>
- 分布嵌套查询
<!--分布嵌套查询--> <select id="getTeacherById2" resultMap="TeacherStudent2"> select * from teacher where id = #{tid} </select> <resultMap id="TeacherStudent2" type="Teacher"> <result property="id" column="id"/> <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/> </resultMap> <select id="getStudentByTeacherId" resultType="Student"> select * from student where tid = #{tid} </select>
4.编写Mybatis的核心配置文件,将我们的接口的配置文件与其他进行绑定
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--引入外部配置文件-->
<properties resource="db.properties">
<property name="username" value="root"/>
<property name="password" value=""/>
</properties>
<!--日志工厂-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean-->
<typeAliases>
<package name="com.zzx.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--绑定接口-->
<mappers>
<mapper class="com.zzx.dao.StudentMapper"/>
<mapper class="com.zzx.dao.TeacherMapper"/>
</mappers>
</configuration>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=
5.编写工具类与测试类
工具类
public class MybatisUtils
{
private static SqlSessionFactory sqlSessionFactory;
static
{
try
{
//获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession()
{
return sqlSessionFactory.openSession(true);
}
}
测试类
public class Test
{
@org.junit.Test
public void test()
{
//1.获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//2.执行SQL
TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
List<Teacher> teacherList = teacherMapper.getTeacher();
for (Teacher teacher : teacherList)
{
System.out.println(teacher);
}
//关闭SqlSession
sqlSession.close();
}
@org.junit.Test
public void getTeacherById()
{
//1.获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//2.执行SQL
TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = teacherMapper.getTeacherById(1);
System.out.println(teacher);
//关闭SqlSession
sqlSession.close();
}
@org.junit.Test
public void getTeacherById2()
{
//1.获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//2.执行SQL
TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = teacherMapper.getTeacherById2(1);
System.out.println(teacher);
//关闭SqlSession
sqlSession.close();
}
}
6.多对一与一对多的总结
- 对象:association 【多对一】
- 集合:collection【一对多】
- javaType:用来指定实体类中属性的类型
- ofType:用来指定映射到List或者集合中的实体类类型,泛型中的约束类型
OK,今天的学习就结束啦,如果对你有帮助麻烦给个三连(点赞关注加收藏),我是猪猪侠,一个码农,我们在Mybatis08篇再会
往期MyBatis教学地址-----MyBatis文章合集