使用SpringBoot重构Django博客(一)查询所有博客的api

本文介绍如何使用MyBatis处理博客系统的多对多标签关联和一对多分类关联,涉及表结构设计、Pojo定义、DAO接口及XML配置等关键技术点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

考虑到文章的主表和分类表示一对多关联,标签表示多对多,使用Mybatis处理。
这里的表结构如下所示:

表结构

文章
  • id
  • title
  • body
  • category_id
分类
  • id
  • name
标签
  • id
  • name
文章-标签中间表
  • id
  • articles_id
  • tag_id

Pojo.java

package com.arithmeticjia.zuul.pojo;

import java.util.Date;
import java.util.List;

/**
 * @author ArithmeticJia
 * @version 1.0
 * @date 2021/3/10 2:10 上午
 */
public class Post {

    private int id;
    private String title;
    private String body;
    private String authorname;
    private List<Tag> tags;

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getAuthorname() {
        return authorname;
    }

    public void setAuthorname(String authorname) {
        this.authorname = authorname;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }
}
package com.arithmeticjia.zuul.pojo;

/**
 * @author ArithmeticJia
 * @version 1.0
 * @date 2021/3/10 8:50 下午
 */
public class Tag {

    private int id;
    private String name;

    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;
    }
}

通过继承实现一对多的分类查询

package com.arithmeticjia.zuul.pojo;

/**
 * @author ArithmeticJia
 * @version 1.0
 * @date 2021/3/13 2:23 下午
 */
public class PostCategory extends Post{
    private String category;

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}

PostDao.java

package com.arithmeticjia.zuul.dao;

import com.arithmeticjia.zuul.pojo.PostCategory;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ArithmeticJia
 * @version 1.0
 * @date 2021/3/10 2:08 上午
 */
@Mapper
@Repository
public interface PostDao {
    List<PostCategory> getPostAll();
}

PostService.java

package com.arithmeticjia.zuul.service;

import com.arithmeticjia.zuul.dao.PostDao;
import com.arithmeticjia.zuul.pojo.PostCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ArithmeticJia
 * @version 1.0
 * @date 2021/3/10 2:07 上午
 */
@Service
public class PostService {
    @Autowired
    PostDao postDao;
    public List<PostCategory> getPostAll(){return postDao.getPostAll();};
}

PostDao.xml

<?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 保证唯一 我这里填写的是映射类的名字 xml文件名和映射类的名字是一样的-->
<mapper namespace="com.arithmeticjia.zuul.dao.PostDao">
    <!--id是映射类中的函数名字 返回类型是方法返回值中实体类型-->
    <!--当我们调用映射类的方法时候,实际上会来执行这句sql-->
    <resultMap type="com.arithmeticjia.zuul.pojo.PostCategory" id="postMap">
        <id property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="body" column="body"/>
        <result property="category" column="category"/>
        <collection property="tags" ofType="com.arithmeticjia.zuul.pojo.Tag">
            <id property="id" column="tid"/>
            <result property="name" column="tname"/>
        </collection>
    </resultMap>
    <select id="getPostAll" resultType="com.arithmeticjia.zuul.pojo.PostCategory" resultMap="postMap">
        SELECT
            a.id,
            a.title,
            a.body,
            b.name as category,
            c.id AS tid,
            c.NAME AS tname
        FROM
            blog_category AS b
                JOIN blog_articles AS a ON a.category_id = b.id
                JOIN blog_articles_tags AS m ON a.id = m.articles_id
                JOIN blog_tag AS c ON c.id = m.tag_id
        WHERE
            a.status = '有效'
    </select>

</mapper>

数据库查询

SELECT
	a.id,
	a.title,
	a.body,
	b.name as category,
	c.id AS tid,
	c.NAME AS tname 
FROM
	blog_category AS b
	JOIN blog_articles AS a ON a.category_id = b.id
	JOIN blog_articles_tags AS m ON a.id = m.articles_id
	JOIN blog_tag AS c ON c.id = m.tag_id
WHERE
	a.status = '有效'

在这里插入图片描述

PostMan

http://127.0.0.1:8089/zuul/api/v1/post/list
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值