Mybatis多表联查(一对一)

发布时间 2023-05-24 18:45:46作者: liangkuan

1、在搭建好Mybatis之后,建两个实体类(将从表实体类型建在主表实体类中)

点击查看代码
package com.bh.po;

public class Student1 {
    private int stuid;
    private String stuname;
   //从表类型
    private Studetail sd;

    public Studetail getSd() {
        return sd;
    }

    public void setSd(Studetail sd) {
        this.sd = sd;
    }

    public int getStuid() {
        return stuid;
    }

    public void setStuid(int stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public Student1() {
    }

    public Student1(int stuid, String stuname, Studetail sd) {
        this.stuid = stuid;
        this.stuname = stuname;
        this.sd = sd;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "stuid=" + stuid +
                ", stuname='" + stuname + '\'' +
                ", sd=" + sd +
                '}';
    }
}

点击查看代码
package com.bh.po;

public class Studetail {
    private int stuid;
    private String stuaddress;
    private String stuage;

    public int getStuid() {
        return stuid;
    }

    public void setStuid(int stuid) {
        this.stuid = stuid;
    }

    public String getStuaddress() {
        return stuaddress;
    }

    public void setStuaddress(String stuaddress) {
        this.stuaddress = stuaddress;
    }

    public String getStuage() {
        return stuage;
    }

    public void setStuage(String stuage) {
        this.stuage = stuage;
    }

    public Studetail() {
    }

    public Studetail(int stuid, String stuaddress, String stuage) {
        this.stuid = stuid;
        this.stuaddress = stuaddress;
        this.stuage = stuage;
    }

    @Override
    public String toString() {
        return "Studetail{" +
                "stuid=" + stuid +
                ", stuaddress='" + stuaddress + '\'' +
                ", stuage='" + stuage + '\'' +
                '}';
    }
}

2、写dao接口
点击查看代码
package com.bh.dao;

import com.bh.po.Student;
import com.bh.po.Student1;

import java.util.List;

public interface IStudent1DAO {
    public List<Student1> findAll();


}

3、写mapper.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.bh.dao.IStudent1DAO">
    <resultMap id="studentMap" type="com.bh.po.Student1">
        <!--column代表数据表=库表的列名-->
        <id property="stuid" column="stuid"/>
        <result property="stuname" column="stuname"/>

        <!-- studentdetail -->
        <!-- 这里需要一个 一对一的对象 -->
        <association property="sd" javaType="com.bh.po.Studetail" column="stuid"><!--这里的column是两个表的关联字段-->
            <id property="stuid" column="stuid"/>
            <result property="stuaddress" column="stuaddress"/>
            <result property="stuage" column="stuage"/>
        </association>

    </resultMap>
<!--多表查询用resultMap-->
    <select resultMap="studentMap" id="findAll">
        select s.*, sd.* from student s , studentdetail sd where s.stuid = sd.stuid
    </select>
    


</mapper>

4、导入mybatis.xml文件
点击查看代码
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--DB的四大步骤-->
    <properties>
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/class?characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="jdbc"></transactionManager>
            <dataSource type="POOLED">
                <property value="${driver}" name="driver"/>
                <property value="${url}" name="url"/>
                <property value="${username}" name="username"/>
                <property value="${password}" name="password"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>

    <!--<mapper resource="mapper/StudentMapper.xml"/>-->
    <!--<mapper resource="mapper/Student1Mapper.xml"/>-->
    <mapper resource="mapper/DeptMapper.xml"/>
    <!-- <mapper resource="mapper/TableMapper.xml" />-->
    </mappers>
</configuration>

5、测试
点击查看代码
package com.bh.Test;

import com.bh.dao.IDeptDAO;
import com.bh.dao.IStudent1DAO;
import com.bh.dao.IStudentDAO;
import com.bh.po.Dept;
import com.bh.po.Emp;
import com.bh.po.Student;
import com.bh.po.Student1;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        System.out.println("start--------");
        //读取配置文件
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis.xml");
        //获取工厂的builder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //创建工厂
        SqlSessionFactory sqlSessionFactory = builder.build(in);
        //创建mybatis的sqlSqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        /***********************************单表*********************************************/

        //IStudentDAO studentDAO = sqlSession.getMapper(IStudentDAO.class);
       /* List<Student> all = studentDAO.findAll();
        for (int i = 0; i < all.size(); i++) {
            Student stu = all.get(i);
            System.out.println(stu.getId() + "_" + stu.getName());
        }*/
        //Student stu = new Student();
        //stu.setName("kuan");
        //stu.setId(23);
        /*studentDAO.save(stu);*/
        /*studentDAO.modify(stu);*/
       // studentDAO.remove(stu);
        //sqlSession.commit();
        //System.out.println("end---------");

        /***********************************多表联查一对一*********************************************/
       /* IStudent1DAO student1DAO = sqlSession.getMapper(IStudent1DAO.class);
        List<Student1> all = student1DAO.findAll();
        for (Student1 stu:all) {
            System.out.println(stu.getStuid() +"----"+ stu.getStuname()+"----"+stu.getSd().getStuaddress()+"---"+stu.getSd().getStuage());
        }
        System.out.println("end---------");*/
        /***********************************多表联查一对多*********************************************/
        IDeptDAO mapper = sqlSession.getMapper(IDeptDAO.class);
        List<Dept> all = mapper.findAllWithEmp();
        for (Dept dep:all) {
            Set<Emp> emps = dep.getEmps();
            for (Emp em:emps) {
                System.out.println(em.getDeptno()+"---"+em.getEmpno()+"---"+em.getEname()+"---"+em.getJob()+"---"+dep.getDname());
            }
        }
    }
}