MyBatis学习(四)MyBatis一对一关联查询

一对一关联查询即、两张表通过外键进行关联、从而达到查询外键直接获得两张表的信息。本文基于业务拓展类的方式实现。
项目骨架
配置文件conf.xml和db.properties前几节讲过、这里就不细说了。


1、前期准备

1、建表

2、创建相关的实体类

StudentCard

package com.feng.entity;

public class StudentCard {
    private int cardid;
    private String cardinfo;

    public int getCardid() {
        return cardid;
    }

    public void setCardid(int cardid) {
        this.cardid = cardid;
    }

    public String getCardinfo() {
        return cardinfo;
    }

    public void setCardinfo(String cardinfo) {
        this.cardinfo = cardinfo;
    }
}

Student

package com.feng.entity;

public class Student {
    private int stuNo;
    private String stuName;
    private int stuAge;
    private String graName;

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public String getGraName() {
        return graName;
    }

    public void setGraName(String graName) {
        this.graName = graName;
    }

    public Student(int stuNo, String stuName, int stuAge, String graName) {
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.graName = graName;
    }
    public Student() {
    }

    @Override
    public String toString() {
        return '{'+"stuNo=" + stuNo +
                ", stuName='" + stuName + '\'' +
                '}';
    }
}

2、写接口和配置xml文件

1、studentMapper.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">
<!--namespace:该mapper.xml映射文件的唯一标识-->
<mapper namespace="com.feng.mapper.StudentMapper">
    <select id="queryStudentByNoWithOO"  parameterType="int" resultType="StudentBusiness">
        select s.*,c.* from student s inner join studentcard c
        on s.cardid=c.cardid
        where s.stuno = #{stuno}
    </select>
</mapper>

2、StudentMapper

package com.feng.mapper;

import com.feng.entity.Student;
import com.feng.entity.StudentBusiness;

import java.util.List;

// 操作mybatis的接口
public interface StudentMapper {
    StudentBusiness queryStudentByNoWithOO(int stuno);
}

注意:JAVA中不能多继承、只能单继承、故要接收返回的信息、只能写一个业务类、该类继承相关的表、其他字段自己写。

3、StudentBusiness

package com.feng.entity;

// 学生业务拓展类
public class StudentBusiness extends Student{
    private int cardid;
    private String cardinfo;

    public int getCardid() {
        return cardid;
    }

    public void setCardid(int cardid) {
        this.cardid = cardid;
    }

    public String getCardinfo() {
        return cardinfo;
    }

    public void setCardinfo(String cardinfo) {
        this.cardinfo = cardinfo;
    }

    @Override
    public String toString() {
        return super.toString() + "," + this.cardid + "," + this.cardinfo;
    }
}

3、编写测试类

package com.feng.test;

import com.feng.entity.Student;
import com.feng.entity.StudentBusiness;
import com.feng.mapper.StudentMapper;
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 java.io.IOException;
import java.io.Reader;
import java.util.List;

public class Test {
    // 7、一对一联表查询
    public static void queryStudentByNoWithOO() throws IOException {
        // conf.xml ---->  reader
        Reader reader = Resources.getResourceAsReader("conf.xml");
        // reader ---> SqlSession
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sessionFactory.openSession();

        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        StudentBusiness studentBusiness = studentMapper.queryStudentByNoWithOO(2);// 查询二号
        System.out.println(studentBusiness);
        session.close();
    }
    public static void main(String[] args) throws IOException {
        queryStudentByNoWithOO();
    }
}

4、测试结果

posted @ 2021-02-20 10:22  Frank_7  阅读(133)  评论(0编辑  收藏  举报