当前位置:网站首页>Join query and subquery
Join query and subquery
2022-07-27 18:50:00 【Fantasia of the cat】
Generally, the implementation of classification list :

Link query : One time query of three-level classification
advantage : Just one query , The corresponding speed is faster when displaying the secondary classification according to the primary classification
shortcoming : The efficiency of database query is low , The speed of page loading for the first time is also relatively slow
Subquery : First, only query the first level classification , The user clicks / Move the mouse to the first level of classification , Dynamic loading of secondary classification
advantage : Improve the efficiency of database query , The speed of page loading for the first time is improved
shortcoming : You need to connect to the database multiple times
Data table structure :

encapsulation Category by CategoryVO:( by list aggregate )
package com.qfedu.fmmall.entity;
import javax.persistence.Column;
import java.util.List;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-24 18:00
*/
public class CategoryVO {
@Override
public String toString() {
return "CategoryVO{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", categoryLevel=" + categoryLevel +
", parentId=" + parentId +
", categoryIcon='" + categoryIcon + '\'' +
", categorySlogan='" + categorySlogan + '\'' +
", categoryPic='" + categoryPic + '\'' +
", categoryBgColor='" + categoryBgColor +
'}';
}
/**
* Primary key classification id Primary key
*/
@Column(name = "category_id")
private Integer categoryId;
/**
* Category name Category name
*/
@Column(name = "category_name")
private String categoryName;
/**
* Classification level Type of classification ,
1: A big classification
2: Secondary classification
3: Three levels of classification
*/
@Column(name = "category_level")
private Integer categoryLevel;
/**
* Parent level id Father id The upper level depends on id,1 Level classification is 0, The second level and the third level depend on the upper level respectively
*/
@Column(name = "parent_id")
private Integer parentId;
/**
* Icon logo
*/
@Column(name = "category_icon")
private String categoryIcon;
/**
* slogan
*/
@Column(name = "category_slogan")
private String categorySlogan;
/**
* Classification chart
*/
@Column(name = "category_pic")
private String categoryPic;
/**
* The background color
*/
@Column(name = "category_bg_color")
private String categoryBgColor;
private List<CategoryVO> categories;
public List<CategoryVO> getCategories() {
return categories;
}
public void setCategories(List<CategoryVO> categories) {
this.categories = categories;
}
/**
* Get primary key classification id Primary key
*
* @return category_id - Primary key classification id Primary key
*/
public Integer getCategoryId() {
return categoryId;
}
/**
* Set primary key classification id Primary key
*
* @param categoryId Primary key classification id Primary key
*/
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
/**
* Get the category name Category name
*
* @return category_name - Category name Category name
*/
public String getCategoryName() {
return categoryName;
}
/**
* Set classification name Category name
*
* @param categoryName Category name Category name
*/
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
/**
* Get the classification level Type of classification ,
1: A big classification
2: Secondary classification
3: Three levels of classification
*
* @return category_level - Classification level Type of classification ,
1: A big classification
2: Secondary classification
3: Three levels of classification
*/
public Integer getCategoryLevel() {
return categoryLevel;
}
/**
* Set the classification level Type of classification ,
1: A big classification
2: Secondary classification
3: Three levels of classification
*
* @param categoryLevel Classification level Type of classification ,
1: A big classification
2: Secondary classification
3: Three levels of classification
*/
public void setCategoryLevel(Integer categoryLevel) {
this.categoryLevel = categoryLevel;
}
/**
* Get the parent level id Father id The upper level depends on id,1 Level classification is 0, The second level and the third level depend on the upper level respectively
*
* @return parent_id - Parent level id Father id The upper level depends on id,1 Level classification is 0, The second level and the third level depend on the upper level respectively
*/
public Integer getParentId() {
return parentId;
}
/**
* Set parent level id Father id The upper level depends on id,1 Level classification is 0, The second level and the third level depend on the upper level respectively
*
* @param parentId Parent level id Father id The upper level depends on id,1 Level classification is 0, The second level and the third level depend on the upper level respectively
*/
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
/**
* Get the icon logo
*
* @return category_icon - Icon logo
*/
public String getCategoryIcon() {
return categoryIcon;
}
/**
* Set icon logo
*
* @param categoryIcon Icon logo
*/
public void setCategoryIcon(String categoryIcon) {
this.categoryIcon = categoryIcon;
}
/**
* Get slogans
*
* @return category_slogan - slogan
*/
public String getCategorySlogan() {
return categorySlogan;
}
/**
* Set up slogans
*
* @param categorySlogan slogan
*/
public void setCategorySlogan(String categorySlogan) {
this.categorySlogan = categorySlogan;
}
/**
* Get the classification map
*
* @return category_pic - Classification chart
*/
public String getCategoryPic() {
return categoryPic;
}
/**
* Set the classification chart
*
* @param categoryPic Classification chart
*/
public void setCategoryPic(String categoryPic) {
this.categoryPic = categoryPic;
}
/**
* Get the background color
*
* @return category_bg_color - The background color
*/
public String getCategoryBgColor() {
return categoryBgColor;
}
/**
* Set the background color
*
* @param categoryBgColor The background color
*/
public void setCategoryBgColor(String categoryBgColor) {
this.categoryBgColor = categoryBgColor;
}
}
Link query :( Demonstrate three-level query )
categoryMapper Interface :
package com.qfedu.fmmall.dao;
import com.qfedu.fmmall.entity.Category;
import com.qfedu.fmmall.entity.CategoryVO;
import com.qfedu.fmmall.general.GeneralDAO;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CategoryMapper extends GeneralDAO<Category> {
//1. Use connection query to realize classification query :3 Level query
public List<CategoryVO> selectAllCategories();
//2. Subquery : according to parentId Query subcategory
public List<CategoryVO> selectAllCategories2(int parentId);
}Mapper file :
<resultMap id="categoryVOMap" type="com.qfedu.fmmall.entity.CategoryVO">
<result column="category_id1" jdbcType="INTEGER" property="categoryId" />
<result column="category_name1" jdbcType="VARCHAR" property="categoryName" />
<result column="category_level1" jdbcType="INTEGER" property="categoryLevel" />
<result column="parent_id1" jdbcType="INTEGER" property="parentId" />
<result column="category_icon1" jdbcType="VARCHAR" property="categoryIcon" />
<result column="category_slogan1" jdbcType="VARCHAR" property="categorySlogan" />
<result column="category_pic1" jdbcType="VARCHAR" property="categoryPic" />
<result column="category_bg_color1" jdbcType="VARCHAR" property="categoryBgColor" />
<collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVO">
<result column="category_id2" jdbcType="INTEGER" property="categoryId" />
<result column="category_name2" jdbcType="VARCHAR" property="categoryName" />
<result column="category_level2" jdbcType="INTEGER" property="categoryLevel" />
<result column="parent_id2" jdbcType="INTEGER" property="parentId" />
<collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVO">
<result column="category_id3" jdbcType="INTEGER" property="categoryId" />
<result column="category_name3" jdbcType="VARCHAR" property="categoryName" />
<result column="category_level3" jdbcType="INTEGER" property="categoryLevel" />
<result column="parent_id3" jdbcType="INTEGER" property="parentId" />
</collection>
</collection>
</resultMap>
<select id="selectAllCategories" resultMap="categoryVOMap">
select
c1.category_id 'category_id1',
c1.category_name 'category_name1',
c1.category_level 'category_id1',
c1.parent_id 'parent_id1',
c1.category_icon 'category_icon1',
c1.category_slogan 'category_slogan1',
c1.category_pic 'category_pic1',
c1.category_bg_color 'category_bg_color1',
c2.category_id 'category_id2',
c2.category_name 'category_name2',
c2.category_level 'category_level2',
c2.parent_id 'parent_id2',
c3.category_id 'category_id3',
c3.category_name 'category_name3',
c3.category_level 'category_level3',
c3.parent_id 'parent_id3'
from category c1
left join category c2 on c2.parent_id=c1.category_id
left join category c3 on c3.parent_id=c2.category_id
where c1.category_level=1
</select>test :
package com.qfedu;
import com.qfedu.fmmall.dao.CategoryMapper;
import com.qfedu.fmmall.entity.CategoryVO;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class)
class ApiApplicationTests {
@Autowired
private CategoryMapper categoryMapper;
@Test
void contextLoads() {
List<CategoryVO> categoryVOS = categoryMapper.selectAllCategories();
for(CategoryVO c1 : categoryVOS){
System.out.println(c1);
for(CategoryVO c2 : c1.getCategories()){
System.out.println("\t" + c2);
for(CategoryVO c3 : c2.getCategories()){
System.out.println("\t\t" + c3);
}
}
}
}
}
test result :
Subquery :
categoryMapper Interface :
package com.qfedu.fmmall.dao;
import com.qfedu.fmmall.entity.Category;
import com.qfedu.fmmall.entity.CategoryVO;
import com.qfedu.fmmall.general.GeneralDAO;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CategoryMapper extends GeneralDAO<Category> {
//1. Use connection query to realize classification query :3 Level query
public List<CategoryVO> selectAllCategories();
//2. Subquery : according to parentId Query subcategory
public List<CategoryVO> selectAllCategories2(int parentId);
}Mapper file :
<resultMap id="categoryVOMap2" type="com.qfedu.fmmall.entity.CategoryVO">
<result column="category_id" jdbcType="INTEGER" property="categoryId" />
<result column="category_name" jdbcType="VARCHAR" property="categoryName" />
<result column="category_level" jdbcType="INTEGER" property="categoryLevel" />
<result column="parent_id" jdbcType="INTEGER" property="parentId" />
<result column="category_icon" jdbcType="VARCHAR" property="categoryIcon" />
<result column="category_slogan" jdbcType="VARCHAR" property="categorySlogan" />
<result column="category_pic" jdbcType="VARCHAR" property="categoryPic" />
<result column="category_bg_color" jdbcType="VARCHAR" property="categoryBgColor" />
<collection property="categories" column="category_id" select="com.qfedu.fmmall.dao.CategoryMapper.selectAllCategories2"/>
</resultMap>
<!-- Classified by parent id Query sub level classification -->
<select id="selectAllCategories2" resultMap="categoryVOMap2">
select
category_id,
category_name,
category_level,
parent_id,
category_icon,
category_slogan,
category_pic,
category_bg_color
from category
where parent_id=#{parentId}
</select>test :
package com.qfedu;
import com.qfedu.fmmall.dao.CategoryMapper;
import com.qfedu.fmmall.entity.CategoryVO;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class)
class ApiApplicationTests {
@Autowired
private CategoryMapper categoryMapper;
@Test
void contextLoads() {
List<CategoryVO> categoryVOS = categoryMapper.selectAllCategories2(0);
for(CategoryVO c1 : categoryVOS){
System.out.println(c1);
for(CategoryVO c2 : c1.getCategories()){
System.out.println("\t" + c2);
for(CategoryVO c3 : c2.getCategories()){
System.out.println("\t\t" + c3);
}
}
}
}
}
test result :

summary :
Sub query is better for large data , The query efficiency of the database is high ; It is better to use continuous query when there is less data , All data can be queried at one time .
Continuous queries and subqueries work the same , Start with the first level query , So as to query , But the actual combat effect is different , The two methods can be used for reference .
边栏推荐
- Canvas draws graphics according to coordinate points
- MySQL learns the relationship between Day2 Sorting Query / aggregation function / grouping query / paging query / constraint / multiple tables
- 全身多功能按摩仪芯片-DLTAP602SD
- 智能失眠治疗仪产品-DLT8P68SA-杰力科创
- The hero of the aircraft war comes out with bullets
- 面试官:你觉得你最大的缺点是什么?
- JS to realize simple form verification and select all functions
- 一个案例理解mysql视图
- Have you ever stumbled on MySQL's order by
- C basic concepts list description suggestions collection
猜你喜欢

"MySQL things" explains the indexing principle in detail

Knowledge map pyhanlp realizes named body recognition (with named body recognition code)

商品名称模糊搜索:

搭建一个简单的知识问答系统

Quick access to website media resources

Visual studio code installation tutorial (super detailed)

迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创

LED带风扇护眼学习台灯触摸芯片-DLT8S12A

How to realize the full-text content retrieval of word, PDF and txt files?

浴室带除雾化妆镜触摸芯片-DLT8T10S
随机推荐
I was forced to optimize the API gateway query interface
Uni app form submit button send request
Wechat applet wechat payment overview
Uni app traversal array rendering data vertical rendering
2 万字 + 30 张图 | 细聊 MySQL undo log、redo log、binlog 有什么用?
RuntimeError: output with shape [1, 256, 256] doesn‘t match the broadcast shape [3, 256, 256]【报错】
兆骑科创海内外引进高层次人才,创新创业项目对接
The combination of text and words perfectly explains the implementation process of MySQL logical backup
Solve the problem of JSP cascading
XML learning Day1: XML / jsup parser / selector /xpath selector
这样的API网关查询接口优化,我是被迫的
知识图谱 — jieba、pyhanlp、smoothnlp工具实现中文分词(词性表)
Machine learning -- error caused by only one kind of label data in SVM training set
Wechat applet wxacode.getunlimited generates applet code
全自动吸奶器芯片-DLTAP703SD
Mode= "widthfix" attribute in image tag
知识图谱 — pyhanlp实现命名体识别(附命名体识别代码)
2021.7.28 notes
机器学习分类任务效果评估指标大全(包含ROC和AUC)
Build a simple knowledge question and answer system