Mybatis关联查询-Hey

本文介绍了一个使用MyBatis进行复杂关联查询的例子,展示了如何定义POJO类及映射文件来实现从数据库中获取文章及其分类详情的功能。

Mapping:

<?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="com.hey.liyankun.admin.mapper.HeyArticleCustomMapper" >
  <resultMap id="article_map" type="com.hey.liyankun.admin.pojo.HeyArticle" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="author" property="author" jdbcType="VARCHAR" />
    <result column="category_id" property="categoryId" jdbcType="INTEGER" />
    <result column="content" property="content" jdbcType="LONGVARCHAR" />
      <association property="heyCategory" javaType="com.hey.liyankun.admin.pojo.HeyCategory">
          <id column="id" property="id" jdbcType="INTEGER"/>
          <result column="name" property="name"/>
      </association>
  </resultMap>


    <select id="getArticleList" resultMap="article_map">
         SELECT
         hey_article.*,
         hey_category.id,
         hey_category.name
         FROM hey_article LEFT JOIN
         hey_category ON hey_article.category_id = hey_category.id
    </select>



</mapper>

pojo:

package com.hey.liyankun.admin.pojo;

import java.util.Date;
import javax.persistence.*;

@Table(name = "hey_article")
public class HeyArticle {
    /**
     * 文章id
     */
    @Id
    private Integer id;

    /**
     * 文章标题
     */
    private String title;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;

    /**
     * 文章作者
     */
    private String author;

    /**
     * 文章分类id
     */
    @Column(name = "category_id")
    private Integer categoryId;

    /**
     * 文章内容
     */
    private String content;

    private HeyCategory heyCategory;

    public HeyCategory getHeyCategory() {
        return heyCategory;
    }

    public void setHeyCategory(HeyCategory heyCategory) {
        this.heyCategory = heyCategory;
    }

    /**
     * 获取文章id
     *
     * @return id - 文章id
     */
    public Integer getId() {
        return id;
    }

    /**
     * 设置文章id
     *
     * @param id 文章id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * 获取文章标题
     *
     * @return title - 文章标题
     */
    public String getTitle() {
        return title;
    }

    /**
     * 设置文章标题
     *
     * @param title 文章标题
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 获取创建时间
     *
     * @return create_time - 创建时间
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * 设置创建时间
     *
     * @param createTime 创建时间
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * 获取文章作者
     *
     * @return author - 文章作者
     */
    public String getAuthor() {
        return author;
    }

    /**
     * 设置文章作者
     *
     * @param author 文章作者
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * 获取文章分类id
     *
     * @return category_id - 文章分类id
     */
    public Integer getCategoryId() {
        return categoryId;
    }

    /**
     * 设置文章分类id
     *
     * @param categoryId 文章分类id
     */
    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    /**
     * 获取文章内容
     *
     * @return content - 文章内容
     */
    public String getContent() {
        return content;
    }

    /**
     * 设置文章内容
     *
     * @param content 文章内容
     */
    public void setContent(String content) {
        this.content = content;
    }

    public HeyArticle(Integer id, String title, Date createTime, String author, Integer categoryId, String content, HeyCategory heyCategory) {
        this.id = id;
        this.title = title;
        this.createTime = createTime;
        this.author = author;
        this.categoryId = categoryId;
        this.content = content;
        this.heyCategory = heyCategory;
    }

    public HeyArticle() {
    }

    @Override
    public String toString() {
        return "HeyArticle{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", createTime=" + createTime +
                ", author='" + author + '\'' +
                ", categoryId=" + categoryId +
                ", content='" + content + '\'' +
                ", heyCategory=" + heyCategory +
                '}';
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值