1. 程式人生 > >MyBatis 多對一配置

MyBatis 多對一配置

自己在工作中遇到的Mybatis多對一的情況,以此記錄。

分類總標籤:Category_total
分類標籤:Category
關聯關係:Category_total 為Category的父分類。
對應欄位:Category 中的 totalid 為Category_total 中的id
實現功能:因為Category中只存有總標籤的ID(即totalid),但是展示的時候要展示總標籤的名稱。所以需要Mybatis的多對一實現。

分類總標籤
package com.wenyibuluo.bean;

import java.util.Date;

public class
Category_total {
private int id; private String name; private String description; private String imageurl; private Date createtime; private Date updatetime; private String createname; private String updatename; private int type; public int getId() { return
id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription
(String description) { this.description = description; } public String getImageurl() { return imageurl; } public void setImageurl(String imageurl) { this.imageurl = imageurl; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public Date getUpdatetime() { return updatetime; } public void setUpdatetime(Date updatetime) { this.updatetime = updatetime; } public String getCreatename() { return createname; } public void setCreatename(String createname) { this.createname = createname; } public String getUpdatename() { return updatename; } public void setUpdatename(String updatename) { this.updatename = updatename; } public int getType() { return type; } public void setType(int type) { this.type = type; } }
分類標籤
package com.wenyibuluo.bean;

import java.util.Date;

public class Category {

    private int id;
    private String name;
    private String description;
    private String imageurl;
    private Date createtime;
    private Date updatetime;
    private String createname;
    private String updatename;
    private int totalid;

    private Category_total category_total;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImageurl() {
        return imageurl;
    }

    public void setImageurl(String imageurl) {
        this.imageurl = imageurl;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public Date getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(Date updatetime) {
        this.updatetime = updatetime;
    }

    public String getCreatename() {
        return createname;
    }

    public void setCreatename(String createname) {
        this.createname = createname;
    }

    public String getUpdatename() {
        return updatename;
    }

    public void setUpdatename(String updatename) {
        this.updatename = updatename;
    }

    public int getTotalid() {
        return totalid;
    }

    public void setTotalid(int totalid) {
        this.totalid = totalid;
    }

    public Category_total getCategory_total() {
        return category_total;
    }

    public void setCategory_total(Category_total category_total) {
        this.category_total = category_total;
    }

}
<?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必須指向Dao介面 -->
<mapper namespace="com.wenyibuluo.dao.IBgCategoryDao">

    <resultMap type="com.wenyibuluo.bean.Category_total" id="categoryTotalResultMap">
        <!-- 屬性名和資料庫列名對映 -->
        <id property="id" column="id" />
        <result property="name" column="name" />
    </resultMap>

    <resultMap type="com.wenyibuluo.bean.Category" id="categoryResultMap">
        <!-- 屬性名和資料庫列名對映 -->
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="description" column="description" />
        <result property="imageurl" column="imageurl" />
        <result property="createtime" column="createtime" />
        <result property="updatetime" column="updatetime" />
        <result property="createname" column="createname" />
        <result property="updatename" column="updatename" />
        <result property="totalid" column="totalid" />
        **association關聯指向一的那一方,javaType為一的那一方的實體類物件,column為多的一方的欄位(和一的那一方相關聯的欄位)**
        <association property="category_total" javaType="com.wenyibuluo.bean.Category_total"
            column="totalid" select="getCategoryIdAndName" />
    </resultMap>

    <!-- 獲取category_total -->
    <select id="getCategoryIdAndName" parameterType="Integer"
        resultMap="categoryTotalResultMap">
        select id,name from category_total where id = #{id}
    </select>

    <!-- 獲取類別 -->
    **重要欄位解釋:resultMap="categoryResultMap" 為 返回值型別結構為categoryResultMap的結構**
    <select id="getCategoryList" resultMap="categoryResultMap">
        select
        category.id
        id,category.name
        name ,category.description
        description,category.totalid
        totalid,category_total.name totalname
        from
        category left join
        category_total on category.totalid = category_total.id
        order by
        category.updatetime desc
    </select>

</mapper>