多表联查,告别left join
表1:资料表,字段主要有:id,name,file_location_id(bigint),theme_name_id(bigint), labels(String类型,实际是ids,有',')
表2:目录表,字段主要有:id,file_location(String)
表3:主题表,字段主要有:id,theme_name(String)
表4:标签表,字段主要有:id,label(String)
VO层
@Data
public class BasicVO{
private Long id;
private String name;
private String fileLocation;
private String themeName;
private String label;
}
serviceImpl层
@Service
public class BasicServiceImpl implements BasicService{
@Autowired
private BasicMapper basicMapper;
@Autowired
private LabelMapper labelMapper;
@Autowired
private FileMapper fileMapper;
@Autowired
private ThemeMapper themeMapper;
/**
* 分页查询
*/
@Override
public Page<Basic> selectList(Integer page, Integer limit) {
Page pageData = new Page<>(page,limit);
IPage<Basic> basicIPage = basicMapper.selectPage(pageData, null);
List<Basic> records = basicIPage.getRecords();
List<BasicVO> basicVOList = new ArrayList<>();
for (Basic basic : records) {
BasicVO basicVO = new BasicVO();
BeanUtils.copyProperties(basic,basicVO);
//查询目录的条件构造器,'id'为目录表的id,basic.getFileLocationId()为资料表的关联id
QueryWrapper queryWrapper =new QueryWrapper();
queryWrapper.eq("id",basic.getFileLocationId());
basicVO.setFileLocation(fileMapper.selectOne(queryWrapper).getFileLocation());
//查询主题名称的条件构造器,'id'为主题表的id,basic.getThemeNameId()为资料表的关联id
QueryWrapper queryWrapper1 =new QueryWrapper();
queryWrapper1.eq("id",basic.getThemeNameId());
basicVO.setThemeName(themeMapper.selectOne(queryWrapper1).getName());
// 获取标签的ids
String labels = basic.getLabels();
String[] split = labels.split(",");
// 将String类型的数组 转换成Long 类型的List
Long[] longs =(Long[]) ConvertUtils.convert(split ,Long.class);
List<Long> idsLong = Arrays.asList(longs);
List<Label> labels = labelMapper.selectBatchIds(idsLong);
String str ="";
for (Label label : labels) {
String sName = label.getName();
str += ","+sName;
}
basicVO.setLabel(str.substring(1));