当前位置:网站首页>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 .
边栏推荐
- Labels such as {@code}, {@link} and < P > in the notes
- PyGame aircraft war game background implementation
- 2021.8.6 notes jsoup
- 解决Jsp级联问题
- Machine learning -- error caused by only one kind of label data in SVM training set
- RuntimeError: output with shape [1, 256, 256] doesn‘t match the broadcast shape [3, 256, 256]【报错】
- I was forced to optimize the API gateway query interface
- 2021.8.7 note Servlet
- V-bind and V-for
- 家用静音驱蚊灯芯片-DLTAP703SD-杰力科创
猜你喜欢
随机推荐
Modify placeholder style in input
兆骑科创海内外引进高层次人才,创新创业项目对接
js实现简易表单验证与全选功能
微信小程序 wxacode.getUnlimited生成小程序码
飞机大战碰撞检测
uniapp H5跨域问题
2021.7.13 note sub query
Mode= "widthfix" attribute in image tag
你想得到想不到的MySQL面试题都在这了(2022最新版)
图文结合,完美解释MySQL逻辑备份的实现流程
JS中的数组与对象
I was forced to optimize the API gateway query interface
修改input中placeholder样式
Alibaba architects spent 280 hours sorting out 1015 pages of distributed full stack pamphlets to easily start the distributed system
你有没有在MySQL的order by上栽过跟头
Uniapp H5 cross domain problem
飞机大战英雄出场加子弹实现
家用静音驱蚊灯芯片-DLTAP703SD-杰力科创
JDBC learning day1:jdbc
订单超时取消 及 按类别查询商品









