半路接手一个项目,DAO用的是Mybatis-plus,多个模块都有多条件查询,先测试之前写的API,发现根本不是多条件查询,只能匹配其中一个,看了源代码,"这TM是什么?"
if(null != enterpriseFilterServiceDTO.getCompanyId() && !enterpriseFilterServiceDTO.getCompanyId().trim().isEmpty()) {
Supplier supplier = supplierService.getBaseMapper().selectOne(new QueryWrapper<Supplier>().lambda().eq(Supplier::getSupplierId, enterpriseFilterServiceDTO.getCompanyId()));
if(null == enterpriseFilterServiceDTO.getCompanyName())
lambdaQueryWrapper.eq(ServiceDetail::getName, supplier.getName());
}
else if(null != enterpriseFilterServiceDTO.getCreatedDate())
lambdaQueryWrapper.eq(ServiceDetail::getCreateTime, enterpriseFilterServiceDTO.getCreatedDate());
else if(null != enterpriseFilterServiceDTO.getCompanyName() && !enterpriseFilterServiceDTO.getCompanyName().trim().isEmpty())
lambdaQueryWrapper.eq(ServiceDetail::getName, enterpriseFilterServiceDTO.getCompanyName());
else if(null != enterpriseFilterServiceDTO.getStatus())
lambdaQueryWrapper.eq(ServiceDetail::getDeleted, enterpriseFilterServiceDTO.getStatus().equalsIgnoreCase("上架")?0:1);
else if(null != enterpriseFilterServiceDTO.getServiceType() && !enterpriseFilterServiceDTO.getServiceType().trim().isEmpty())
lambdaQueryWrapper.eq(ServiceDetail::getTypeId, typeIdMap.get(enterpriseFilterServiceDTO.getServiceType()));
else if(null == enterpriseFilterServiceDTO.getServiceType()) {
for(Integer typeId : typeIdMap.values()) {
lambdaQueryWrapper.or().eq(ServiceDetail::getTypeId, typeId);
}
}
改吧
1. 新建Mapper,
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zltz.univ.core.model.CourseFilter;
import com.zltz.univ.core.model.CourseForList;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CourseSelectMapper {
//`Page<T> page 分页用的
List<CourseForList> findCoursesInfo(Page<CourseForList> page,CourseFilter courseFilter);
}
2. resources/mapping/ 写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">
<mapper namespace="com.zltz.univ.core.mapper.CourseSelectMapper"> //mapper类
<resultMap id="ResultMap" type="com.zltz.univ.core.model.CourseForList"/> //返回类型
<select id="findCoursesInfo" resultMap="ResultMap"
parameterType="com.zltz.univ.core.model.CourseFilter"> //传参类型
SELECT
id,
category_id,
name,
course_status,
price,
course_capacity,
start_time
FROM zltz_courses
WHERE
deleted = 0
<if test="courseFilter.id !=null">
and id like CONCAT('%',#{courseFilter.id},'%')
</if>
<if test="courseFilter.categoryId!=null">
and category_id = #{courseFilter.categoryId}
</if>
<if test = "courseFilter.startDate!=null">
and date_format(start_time,'%Y-%m-%d') = str_to_date(#{courseFilter.startDate},'%Y-%m-%d')
</if>
<if test = "courseFilter.name !=null">
and name like CONCAT('%',#{courseFilter.name},'%')
</if>
<if test ="courseFilter.courseStatus!=null">
and course_status = #{courseFilter.courseStatus}
</if>
<if test = "courseFilter.minPrice!=null">
and price between #{courseFilter.minPrice} and #{courseFilter.maxPrice}
</if>
</select>
</mapper>
3. service 中调用
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@Autowired
private CourseSelectMapper courseSelectMapper;
public BaseResponse listAllCourses(CourseFilterDTO courseFilterDTO, Integer page, Integer limit) {
List<Category> categoryList = categoryService.listAllCategoryByType(courseParentType);
CourseFilter courseFilter = transformParameters(courseFilterDTO);
//查询分页
Page<CourseForList> p = new Page<>(page,limit);
p.setRecords(courseSelectMapper.findCoursesInfo(p,courseFilter));
Map<Integer, String> categoryMap = CategoryUtil.getIdTypeMap(categoryList);
Map<Integer, String> statusMap = CategoryUtil.getStatusMap(courseStatus);
return new CommonListPageResponse(true, null, CourseUtil.convertTo_2(p.getRecords(), categoryMap, statusMap), p.getTotal(), p.getSize(),p.getCurrent());
}
另外
一开始尝试使用pagehelper分页插件,直接报错.
通过一下方式尝试解决.
mybatis-plus分页和pagehelper冲突(java.lang.NoSuchMethodError: net.sf.jsqlparser.statement.select.Plain)
可以解决冲突,但是分页失效没解决,我太菜了,文末的interceptor设置,我没做到在yml文件中完成.所以最后还是去学了Mybatis-plus的分页.