简体   繁体   English

如何在MyBatis(Spring)映射器中使用表上的select创建一个对象(Spring)?

[英]How do I create an object in MyBatis (Spring) mapper using select on a table (Spring)?

I am new to MyBatis and Spring. 我是MyBatis和Spring的新手。 I have two SQL tables, let's say a list of Universities and a list of Students. 我有两个SQL表,比方说大学列表和学生列表。

UNIVERSITY table has id, name, listOfStudents

STUDENTS table has name_ofStudent, GPA, age, major, id, universityId

I have University.java, Student.java, UniversityManager.java, StudentManager.java (both with crud operations in their manager) (read's parameter is an int id). 我有University.java,Student.java,UniversityManager.java,StudentManager.java(两者在其管理器中均具有crud操作)(read的参数是int id)。

If this is my UniversityMapper.xml (below) how do I "SELECT" (by ID) a University? 如果这是我的UniversityMapper.xml(如下),如何“选择”(按ID)大学? I know how the basic select works but my main question is once I select get by id the name of the university how do I also so get the list of students belonging to that students? 我知道基本选择的工作原理,但我的主要问题是,一旦选择ID即可获得大学的名称,我也将如何获得该学生的学生名单?

I want to know how to select using mybatis the list of students that match the universityID and create a student object and make it part of the university object. 我想知道如何使用mybatis选择与universityID匹配的学生列表,并创建一个学生对象并使它成为大学对象的一部分。

<?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="java.University">
  <resultMap id="BaseResultMap" type="java.University">
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="NAME" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <insert id="createUniversity" parameterType="java.University">
    insert into DATABASE.University(ID, NAME)
    values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR})
        </insert>

   <select id="getUniversity" parameterType="int" resultMap="java.University">

(This is where I need help)

</select>

   <update id="updateUniversity" parameterType="java.University">
    update DATABASE.University
    set ID= #{id,jdbcType=VARCHAR},
      NAME= #{name,jdbcType=VARCHAR}
  </update>
  <delete id="deleteUniversity" parameterType="java.University">
    delete from DATABASE.University
    where ID= #{id,jdbcType=VARCHAR}
  </delete>
</mapper>

It's all written in the documentation in the part where they mention the Advanced ResultMap s. 所有这些都写在他们提到Advanced ResultMap的部分的文档中。

If your University class is something like this: 如果您的大学课程是这样的:

public class University{
    private Long id;
    private String name;
    private List<Student> students;
    // getters, setters, etc.
}

And your Student class is like: 您的学生班就像:

public class Student{
    private String name_ofStudent;
    private Double GPA;
    private Integer age;
    private String major;
    private Long id;
    private Long universityId;
    // getters, setters, etc.
}

You can have query that gets the University and its Students as follows: 您可以通过以下查询获得大学及其学生的查询:

<select parameterType="Long" resultMap="UniversityResultMap">
    SELECT U.uni_id, U.uni_name, S.name_ofStudent, S.gpa, S.age, S.major, S.stu_id, S.stu_uni_id
    FROM UNIVERSITY U
    JOIN STUDENT S ON U.ID = S.UNIVERSITYID
    WHERE U.ID = #{id}
</select>

Then you can map the results as: 然后,您可以将结果映射为:

<resultMap id="StudentResultMap" resultMap="java.Student">
    <result column="NAME_OFSTUDENT" jdbcType="VARCHAR" property="name_ofStudent" />
    <result column="GPA" jdbcType="DECIMAL" property="GPA" />
    <result column="AGE" jdbcType="DECIMAL" property="age" />
    <result column="MAJOR" jdbcType="VARCHAR" property="major" />
    <result column="STU_ID" jdbcType="DECIMAL" property="id" />
    <result column="STU_UNI_ID" jdbcType="DECIMAL" property="universityId" />
</resultMap>

<resultMap id="UniversityResultMap" parameterType="Long" resultMap="java.University">
    <id column="UNI_ID" jdbcType="DECIMAL" property="id" />
    <result column="NAME" jdbcType="VARCHAR" property="name" />
    <collection  property="students" ofType="java.Student" resultMap="StudentResultMap"/>
</resultMap>

If you have a different abstraction in mind about the classes, let us know. 如果您对类有不同的抽象概念,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM