mybatis查询(一对一对多)

Posted Dream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis查询(一对一对多)相关的知识,希望对你有一定的参考价值。

一、普通方式

1、一对一

(1)创建实体类:

Student:

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String studentno;
    private String sname;
    private String sex;
    private String birthday;
    private String classno;
    private String point;
    private String phone;
    private String email;
    private Clas clas;
    @Override
    public String toString() {
        return "Student{" +
                "studentno=\'" + studentno + \'\\\'\' +
                ", sname=\'" + sname + \'\\\'\' +
                ", sex=\'" + sex + \'\\\'\' +
                ", birthday=\'" + birthday + \'\\\'\' +
                ", classno=\'" + classno + \'\\\'\' +
                ", point=\'" + point + \'\\\'\' +
                ", phone=\'" + phone + \'\\\'\' +
                ", email=\'" + email + \'\\\'\' +
                ", clas=" + clas +
                \'}\';
    }


    public Clas getClas() {
        return clas;
    }
    public void setClas(Clas clas) {
        this.clas = clas;
    }
    public String getStudentno() {
        return studentno;
    }
    public void setStudentno(String studentno) {
        this.studentno = studentno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getClassno() {
        return classno;
    }
    public void setClassno(String classno) {
        this.classno = classno;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

从一对多和多对一的关系来看,学生属于多的一方,班级属于一的一方,在学生实体中又包含了班级实体,因为每一个学生都只能属于一个班级,而一个班级里面有多个学生。

Clas:

public class Clas {
    private Integer classno;
    private String classname;
    private String monitor;
    private String department;
    @Override
    public String toString() {
        return "Clas{" +
                "classno=" + classno +
                ", classname=\'" + classname + \'\\\'\' +
                ", monitor=\'" + monitor + \'\\\'\' +
                ", department=\'" + department + \'\\\'\' +
                \'}\';
    }
    public Integer getClassno() {
        return classno;
    }

    public void setClassno(Integer classno) {
        this.classno = classno;
    }

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public String getMonitor() {
        return monitor;
    }

    public void setMonitor(String monitor) {
        this.monitor = monitor;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
}

(2)接口:

public interface StudentMapper {
     List<Student> selectStudents();
}

(3)配置文件:

<resultMap id="id" type="pers.zhb.pojo.Student">
        <id property="studentno" column="studentno"></id>
        <result property="classno" column="classno"></result>
        <association property="clas" javaType="pers.zhb.pojo.Clas">
            <id property="classno" column="classno"></id>
            <result column="classname" property="classname"></result>
            <result column="monitor" property="monitor"></result>
            <result column="department" property="department"></result>
        </association>
    </resultMap>
    <select id="selectStudents"  resultMap="id">
        SELECT *
        FROM student
        LEFT JOIN class
        ON student.classno=class.classno
    </select>

association为一对一的配置,即一个学生对应于一个班级。

sql语句为左外连接:

学生有八条信息:

 班级有两条信息:

 (4)测试:

 public void selectStudents() throws Exception {
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students=studentMapper.selectStudents();
        for(Student student:students){
            System.out.println(student);
        }
    }
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.xml
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Checking to see if class pers.zhb.mapper.StudentMapper matches criteria [is assignable to Object]
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 101478235.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@60c6f5b]
DEBUG [main] - ==>  Preparing: SELECT * FROM student LEFT JOIN class ON student.classno=class.classno 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 8
Student{studentno=\'201811\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=80501, classname=\'计算机0801\', monitor=\'刘国平\', department=\'计算机学院\'}}
Student{studentno=\'201812\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80601\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=80601, classname=\'机械0801\', monitor=\'王善执\', department=\'机械学院\'}}
Student{studentno=\'201813\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=80501, classname=\'计算机0801\', monitor=\'刘国平\', department=\'计算机学院\'}}
Student{studentno=\'201814\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=80501, classname=\'计算机0801\', monitor=\'刘国平\', department=\'计算机学院\'}}
Student{studentno=\'201815\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=80501, classname=\'计算机0801\', monitor=\'刘国平\', department=\'计算机学院\'}}
Student{studentno=\'201816\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=80501, classname=\'计算机0801\', monitor=\'刘国平\', department=\'计算机学院\'}}
Student{studentno=\'201817\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'2\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=2, classname=\'null\', monitor=\'null\', department=\'null\'}}
Student{studentno=\'201818\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'2\', point=\'null\', phone=\'null\', email=\'null\', clas=Clas{classno=2, classname=\'null\', monitor=\'null\', department=\'null\'}}

Process finished with exit code 0

2、一对多

(1)实体类:

Student:

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String studentno;
    private String sname;
    private String sex;
    private String birthday;
    private String classno;
    private String point;
    private String phone;
    private String email;
    private Clas clas;
    @Override
    public String toString() {
        return "Student{" +
                "studentno=\'" + studentno + \'\\\'\' +
                ", sname=\'" + sname + \'\\\'\' +
                ", sex=\'" + sex + \'\\\'\' +
                ", birthday=\'" + birthday + \'\\\'\' +
                ", classno=\'" + classno + \'\\\'\' +
                ", point=\'" + point + \'\\\'\' +
                ", phone=\'" + phone + \'\\\'\' +
                ", email=\'" + email + \'\\\'\' +
                ", clas=" + clas +
                \'}\';
    }


    public Clas getClas() {
        return clas;
    }
    public void setClas(Clas clas) {
        this.clas = clas;
    }
    public String getStudentno() {
        return studentno;
    }
    public void setStudentno(String studentno) {
        this.studentno = studentno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getClassno() {
        return classno;
    }
    public void setClassno(String classno) {
        this.classno = classno;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

Clas:

public class Clas {
    private Integer classno;
    private String classname;
    private String monitor;
    private String department;
    private List<Student> studentList;
    @Override
    public String toString() {
        return "Clas{" +
                "classno=" + classno +
                ", classname=\'" + classname + \'\\\'\' +
                ", monitor=\'" + monitor + \'\\\'\' +
                ", department=\'" + department + \'\\\'\' +
                ", studentList=" + studentList +
                \'}\';
    }
    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public Integer getClassno() {
        return classno;
    }

    public void setClassno(Integer classno) {
        this.classno = classno;
    }

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public String getMonitor() {
        return monitor;
    }

    public void setMonitor(String monitor) {
        this.monitor = monitor;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
}

(2)接口:

public interface StudentMapper {
     List<Clas> selectClasList();
}

(3)配置文件:

    <resultMap id="id" type="pers.zhb.pojo.Clas">
        <id column="classno" property="classno"></id>
        <result column="classname" property="classname"></result>
        <result column="monitor" property="monitor"></result>
        <result column="department" property="department"></result>
        <collection property="studentList" ofType="pers.zhb.pojo.Student">
            <id property="studentno" column="studentno"></id>
            <result property="classno" column="classno"></result>
        </collection>
    </resultMap>
    <select id="selectClasList"  resultMap="id">
        SELECT *
        FROM class
        LEFT JOIN student
        ON student.classno=class.classno
    </select>

(4)测试:

public void selectClasList() throws Exception {
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Clas> clas=studentMapper.selectClasList();
        for(Clas cla:clas){
            System.out.println(cla);
        }
    }
DEBUG [main] - Logging initialized using \'class org.apache.ibatis.logging.slf4j.Slf4jImpl\' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo
DEBUG [main] - Reader entry: Clas.class
DEBUG [main] - Reader entry: QueryVo.class
DEBUG [main] - Reader entry: Student.class
DEBUG [main] - Listing file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo/Clas.class
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo/Clas.class
DEBUG [main] - Reader entry: ����   4 Q
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo/QueryVo.class
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo/QueryVo.class
DEBUG [main] - Reader entry: ����   4 4
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo/Student.class
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/pojo/Student.class
DEBUG [main] - Reader entry: ����   4 l
DEBUG [main] - Checking to see if class pers.zhb.pojo.Clas matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pers.zhb.pojo.QueryVo matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pers.zhb.pojo.Student matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper
DEBUG [main] - Reader entry: StudentMapper.class
DEBUG [main] - Reader entry: StudentMapper.xml
DEBUG [main] - Listing file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.class
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.class
DEBUG [main] - Reader entry: ����   4    findStudentById ,(Ljava/lang/Integer;)Lpers/zhb/pojo/Student; findStudentByQueryVo )(Lpers/zhb/pojo/QueryVo;)Ljava/util/List;     Signature B(Lpers/zhb/pojo/QueryVo;)Ljava/util/List<Lpers/zhb/pojo/Student;>; countStudent ()Ljava/lang/Integer; selectAllStudent ()Ljava/util/List; +()Ljava/util/List<Lpers/zhb/pojo/Student;>; selectStudentBySexAndSname )(Lpers/zhb/pojo/Student;)Ljava/util/List; B(Lpers/zhb/pojo/Student;)Ljava/util/List<Lpers/zhb/pojo/Student;>; selectStudentByIds &([Ljava/lang/Integer;)Ljava/util/List; ?([Ljava/lang/Integer;)Ljava/util/List<Lpers/zhb/pojo/Student;>; "(Ljava/util/List;)Ljava/util/List; P(Ljava/util/List<Ljava/lang/Integer;>;)Ljava/util/List<Lpers/zhb/pojo/Student;>; selectStudents selectClasList (()Ljava/util/List<Lpers/zhb/pojo/Clas;>; 
DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.xml
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Checking to see if class pers.zhb.mapper.StudentMapper matches criteria [is assignable to Object]
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 101478235.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@60c6f5b]
DEBUG [main] - ==>  Preparing: SELECT * FROM class LEFT JOIN student ON student.classno=class.classno 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 13
Clas{classno=80501, classname=\'计算机0801\', monitor=\'刘国平\', department=\'计算机学院\', studentList=[Student{studentno=\'201811\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=null}, Student{studentno=\'201813\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=null}, Student{studentno=\'201814\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=null}, Student{studentno=\'201815\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=null}, Student{studentno=\'201816\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80501\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=80601, classname=\'机械0801\', monitor=\'王善执\', department=\'机械学院\', studentList=[Student{studentno=\'201812\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'80601\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=90501, classname=\'计算机0901\', monitor=\'马文斐\', department=\'计算机学院\', studentList=[Student{studentno=\'null\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'90501\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=90502, classname=\'计算机0902\', monitor=\'章成楠\', department=\'计算机学院\', studentList=[Student{studentno=\'null\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'90502\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=90801, classname=\'管理0901\', monitor=\'党海\', department=\'管理学院\', studentList=[Student{studentno=\'null\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'90801\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=90802, classname=\'管理0802\', monitor=\'张晓\', department=\'管理学院\', studentList=[Student{studentno=\'null\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'90802\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=90803, classname=\'通信161\', monitor=\'张丽\', department=\'信息工程学院\', studentList=[Student{studentno=\'null\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'90803\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=90804, classname=\'通信172\', monitor=\'李月\', department=\'信工学院\', studentList=[Student{studentno=\'null\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'90804\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}
Clas{classno=90805, classname=\'通信171\', monitor=\'张伟\', department=\'信工学院\', studentList=[Student{studentno=\'null\', sname=\'null\', sex=\'null\', birthday=\'null\', classno=\'90805\', point=\'null\', phone=\'null\', email=\'null\', clas=null}]}

 

 二、使用maven、Lombok实现多对一

1、环境搭建

(1)创建父模块和子模块

父模块:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--父工程-->
    <groupId>pers.zhb.study</groupId>
    <artifactId>mybatis_

以上是关于mybatis查询(一对一对多)的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 一对一关联 association 返回空值

MyBatis高级篇 - 关联查询(一对多)

mybatis怎么一对多查询语句

MyBatis注解开发多表代码操作——手把手教你实战操作

mybatis--表关系之一对多

Mybatis一对一,一对多,多对多代码