考虑到文章的主表和分类表示一对多关联,标签表示多对多,使用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