有些数据的查询需要我们查询三个表来获得总的数据。
一、准备工作:
我前面写过两表联查的方式,三表联查就是在两表联查的基础上进行的,下面是我写的两表联查博客
三表查询和两表查询的区别就只是在配置文件里的映射文件不一样,其余不管是调用方式还底层逻辑都是一样的。
所以,我们就更改了三表联查的映射文件。
<?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="org.dao.oneselMapper">
<!-- 主表字段映射-->
<resultMap id="sanbiaoMapper" type="org.Emp.students">
<id column="sno" property="sno"></id>
<result column="sname" property="sname"></result>
<result column="ssex" property="ssex"></result>
<result column="sbirthday" property="sbirthday"></result>
<result column="classZ" property="classZ"></result>
<!--副表字段映射-->
<collection property="scores" ofType="org.Emp.scores">
<id column="sno" property="sno"></id>
<id column="cno" property="cno"></id>
<result column="degree" property="degree"></result>
<!--副表字段映射-->
<association property="courses" javaType="org.Emp.courses">
<id column="cno" property="cno"></id>
<result column="cname" property="cname"></result>
<result column="tno" property="tno"></result>
</association>
</collection>
</resultMap>
<!--数据库的三表查询-->
<select id="sanbiaoall" resultMap="sanbiaoMapper">
select students.*,courses.*,scores.* from students,courses,scores
where courses.cno=scores.cno and scores.sno=students.sno
</select>
</mapper>
只要对这个映射文件进行调用即可,写映射文件的接口
public interface oneselMapper {
List<students> sanbiaoall();}
最后是在主方法里调用:
package org.example;
import org.Emp.order;
import org.Emp.students;
import org.Emp.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.apache.log4j.PropertyConfigurator;
import org.dao.oneselMapper;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
public class testuser {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
@Test
public void star1() throws IOException {
//配置日志信息
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
properties.load(fileInputStream);
PropertyConfigurator.configure(properties);
// 加载 MyBatis 配置文件
//从类路径中获取 MyBatis 配置文件 mybatis-config.xml 的输入流。
in = Resources.getResourceAsStream("config/mybatis-config.xml");
//创建 SqlSessionFactory 对象,它是 MyBatis 的工厂类,用于创建 SqlSession。
factory = new SqlSessionFactoryBuilder().build(in);
//创建一个新的 SqlSession 实例,用于执行 SQL 语句。
session = factory.openSession();
//通过 SqlSession 获取 oneselMapper 接口的代理实现。
oneselMapper mapper = session.getMapper(oneselMapper.class);
//接口中定义了一个名为 sanbiaoall 的方法,该方法返回一个 List<students> 类型的列表。
List<students> sanbiaoall = mapper.sanbiaoall();
for (students a : sanbiaoall) {
System.out.println(a);
}
}
public static void main(String[] args) throws IOException {
testuser testuser = new testuser();
testuser.star1();
}
}