mybatisPlus动态sql语句 ${}详解

时间:2025-03-19 07:32:58
package com.wl.cloud.monitor.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; import com.wl.cloud.monitor.dao.StudentTestMapper; import com.wl.cloud.monitor.domain.StudentTest; import com.wl.cloud.monitor.service.StudentTestService; import com.wl.cloud.monitor.support.dto.StudentTestDTO; import com.wl.cloud.monitor.support.dto.query.StudentTestQueryDTO; import com.wl.cloud.monitor.support.vo.StudentTestVO; import com.wl.cloud.core.dto.DataStoreDTO; import com.wl.cloud.core.utils.PageUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; /** * test信息 服务实现类 * * @author wanglin * @since 2024-03-21 */ @Service public class StudentTestServiceImpl implements StudentTestService { @Autowired private StudentTestMapper studentTestMapper; @Transactional(readOnly = true) @Override public DataStoreDTO<StudentTestVO> page(Pageable pageable, StudentTestQueryDTO queryDto) { QueryWrapper<StudentTest> queryWrapper = this.buildQuery(queryDto); Page<StudentTestVO> page = PageUtils.transferPage(pageable); IPage<StudentTestVO> result = this.studentTestMapper.getPage(page, queryWrapper); return new DataStoreDTO(result.getTotal(), result.getRecords()); } @Transactional(readOnly = true) @Override public List<StudentTestVO> list(Sort sort, StudentTestQueryDTO queryDto) { QueryWrapper<StudentTest> queryWrapper = this.buildQuery(queryDto); PageUtils.transferSort(queryWrapper, sort); return this.studentTestMapper.getList(queryWrapper); } @Transactional(rollbackFor = Exception.class) @Override public void save(StudentTestDTO dto) { // TODO 唯一性字段校验 dto.setId(null); studentTestMapper.insert(this.transferEntity(null, dto)); } @Transactional(rollbackFor = Exception.class) @Override public void update(StudentTestDTO dto) { Assert.hasText(dto.getId(), "id不能为空"); // TODO 唯一性字段校验 StudentTest entity = studentTestMapper.selectById(dto.getId()); Assert.notNull(entity, "找不到id为 " + dto.getId() + " 的记录"); studentTestMapper.updateById(this.transferEntity(entity, dto)); } @Transactional(rollbackFor = Exception.class) @Override public void delete(Set<String> ids) { if (CollectionUtils.isNotEmpty(ids)) { studentTestMapper.deleteBatchIds(ids); } } @Transactional(readOnly = true) @Override public StudentTestVO get(String id) { Assert.hasText(id, "id不能为空"); StudentTest entity = studentTestMapper.selectById(id); Assert.notNull(entity, "找不到id为 " + id + " 的记录"); return this.transferVo(entity); } private QueryWrapper<StudentTest> buildQuery(StudentTestQueryDTO queryDto) { QueryWrapper<StudentTest> queryWrapper = new QueryWrapper<>(); if (Objects.nonNull(queryDto)) { queryWrapper.lambda().eq(StringUtils.isNotBlank(queryDto.getId()), StudentTest::getId, queryDto.getId()); } return queryWrapper; } private StudentTest transferEntity(StudentTest entity, StudentTestDTO dto) { if (Objects.isNull(entity)) { entity = new StudentTest(); } BeanUtils.copyProperties(dto, entity); return entity; } private List<StudentTestVO> transferVo(List<StudentTest> entities) { if (CollectionUtils.isEmpty(entities)) { return Lists.newArrayList(); } List<StudentTestVO> voList = entities.stream().map(entity -> { StudentTestVO vo = new StudentTestVO(); BeanUtils.copyProperties(entity, vo); return vo; }).collect(Collectors.toList()); return voList; } private StudentTestVO transferVo(StudentTest entity) { if (Objects.isNull(entity)) { return null; } StudentTestVO vo = new StudentTestVO(); BeanUtils.copyProperties(entity, vo); return vo; } }