首页 > 程序开发 > 软件开发 > Java >

java mybatis一对多集合,映射代码讲解

2017-10-25

java mybatis一对多集合,映射代码讲解。相对于关联来说,集合映射多了一个属性”ofType”,这个属性用来区分JavaBean(或字段)属性类型和集合包含的类型来说是很重要的,ofType用来表示集合包含类型。下面来一个问题对应多个选项的实例来诠释映射:

java mybatis一对多集合,映射代码讲解。

相对于关联来说,集合映射多了一个属性”ofType”,这个属性用来区分JavaBean(或字段)属性类型和集合包含的类型来说是很重要的,ofType用来表示集合包含类型。


下面来一个问题对应多个选项的实例来诠释映射:

问题对象实体类:

package com.hi.test.model;

import java.io.Serializable;
import java.util.List;

public class QuestionInfo implements Serializable {

    private static final long serialVersionUID = -5182773713250974139L;
    private String question_id;
    private String question_stem;
    private String question_problem;
    private String question_answer;
    private List answerInfos;

    public String getQuestion_id() {
        return question_id;
    }

    public void setQuestion_id(String question_id) {
        this.question_id = question_id;
    }

    public String getQuestion_stem() {
        return question_stem;
    }

    public void setQuestion_stem(String question_stem) {
        this.question_stem = question_stem;
    }

    public String getQuestion_problem() {
        return question_problem;
    }

    public void setQuestion_problem(String question_problem) {
        this.question_problem = question_problem;
    }

    public String getQuestion_answer() {
        return question_answer;
    }

    public void setQuestion_answer(String question_answer) {
        this.question_answer = question_answer;
    }

    public List getAnswerInfos() {
        return answerInfos;
    }

    public void setAnswerInfos(List answerInfos) {
        this.answerInfos = answerInfos;
    }

}

选项实体类

package com.hi.test.model;

import java.io.Serializable;

public class AnswerInfo implements Serializable {

    private static final long serialVersionUID = 8332347120442700758L;
    private String answer_id;
    private String question_id;
    private String choose_type;
    private String choose_content;

    public String getAnswer_id() {
        return answer_id;
    }

    public void setAnswer_id(String answer_id) {
        this.answer_id = answer_id;
    }

    public String getQuestion_id() {
        return question_id;
    }

    public void setQuestion_id(String question_id) {
        this.question_id = question_id;
    }

    public String getChoose_type() {
        return choose_type;
    }

    public void setChoose_type(String choose_type) {
        this.choose_type = choose_type;
    }

    public String getChoose_content() {
        return choose_content;
    }

    public void setChoose_content(String choose_content) {
        this.choose_content = choose_content;
    }

}

question_answer.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.hi.test.dao.QuestionAnswerDao">

    <resultMap type="com.hi.test.model.QuestionInfo" id="questionInfo">
        <result column="question_id" property="question_id"/>
        <result column="question_stem" property="question_stem"/>
        <result column="question_problem" property="question_problem"/>
        <result column="question_answer" property="question_answer"/>
        <collection property="answerInfos" column="question_id" ofType="com.hi.test.model.AnswerInfo">
            <result column="answer_id" property="answer_id"/>
            <result column="question_id" property="question_id"/>
            <result column="choose_type" property="choose_type"/>
            <result column="choose_content" property="choose_content"/>
        </collection>
    </resultMap>

    <select id="queryQuestionInfo" resultMap="questionInfo">
        SELECT 
            q.question_id,
            q.question_stem,
            q.question_problem,
            q.question_answer,
            qa.answer_id,
            qa.choose_type,
            qa.choose_content
        FROM 
            question q 
        JOIN question_answer qa ON q.question_id = qa.question_id 
        WHERE
            q.question_id = #{question_id}
        ORDER BY q.question_id ASC
    </select>
相关文章
最新文章
热点推荐