请求参数
package com.byx.cygnus.dm.request;
import com.byx.cygnus.common.request.BasePageRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 项目建设维护设备模糊查询请求参数
*
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class ConsAndMainDevicesRequest extends BasePageRequest {
/** 设备编号 */
// private String deviceSerialNumber;
/** 设备名称 */
private String deviceName;
/** 施工负责人 */
private String charger;
/** 项目名称 */
private String projectName;
/** 公司名称 */
// private String companyName;
}
DAO层
package com.byx.cygnus.dm.repository;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import com.byx.cygnus.common.entity.proj.ConstructDevices;
public interface ConstructDevicesRepo extends BaseRepository<ConstructDevices, Long> {
@Query(nativeQuery = true,value = "SELECT * FROM op_construct_devices c "
+ "WHERE if(?1 != '',c.device_name LIKE CONCAT('%',?1,'%'),1=1) "
+ "AND if(?2 != '',c.charger LIKE CONCAT('%',?2,'%'),1=1) "
+ "AND if(COALESCE(?3,NULL) IS NOT NULL,c.project_id IN ?3,1=1)")
//COALESCE(?3,NULL) 可以写成 COALESCE(?3,NULL) IS NOT NULL ,亦可写成 COALESCE(?3,NULL) !=''
//若查询条件为Integer类型,则写成 if(IFNULL(?3,'') != '',c.project_id IN ?3,1=1)
public Page<ConstructDevices> filterConstructDevices(String deviceName,String charger,List<Integer> projectId,Pageable pageable);
}
注: 此SQL语句适用于MySQL;
参数为string,Integer类型,必须 ' ' 不能写成null
service层
//需要用到的分页相关的包
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
//service方法
public Page<ConstructDevices> filterConstructDevices(ConsAndMainDevicesRequest request) {
//根据项目名模糊查询项目集合
List<Project> projects = projectRepo.findByProjectNameLike("%" + request.getProjectName() + "%");
List<Integer> projectIds = new ArrayList<Integer>();
if (projects != null && projects.size() > 0) {
for (Project project : projects) {
projectIds.add(project.getId());
}
} else {
//若项目为null,则将projectId设置为-1
projectIds.add(WebConstants.PROJECT_NAME_NOT_FIND);
}
Pageable of = PageRequest.of(request.getPageNum() - 1, request.getPageSize());
Page<ConstructDevices> ConstructDevices = constructDevicesRepo.filterConstructDevices(request.getDeviceName(),
request.getCharger(), projectIds, of);
return ConstructDevices;
}
controller层
@PostMapping("/select")
public PageResponse<ConstructDevices> select(@RequestBody ConsAndMainDevicesRequest request){
Page<ConstructDevices> page = constructDevicesService.filterConstructDevices(request);
AppPage<ConstructDevices> appConstructDevices = new JpaPageImpl<ConstructDevices>(page);
return PageResponse.success(appConstructDevices);
}
分页查询
package com.byx.cygnus.common.entity;
import java.io.Serializable;
import java.util.List;
/**
* 分页Page,各个分页实现框架返回的分页最终二次封装至此接口的实现类。
* <p>以下使用mybatis-plus为例子:
* <pre>{@code
public class MyBatisPlusPageImpl<T> implements AppPage<T> {
private IPage<T> ipage;
public MyBatisPlusPageImpl(IPage<T> ipage) {
this.ipage = ipage;
}
@Override
public int getPageNum() {
return (int) ipage.getCurrent();
}
@Override
public int getPageSize() {
return (int) ipage.getSize();
}
@Override
public long getTotal() {
return ipage.getTotal();
}
@Override
public int getTotalPages() {
return (int) ipage.getPages();
}
@Override
public List<T> getItems() {
return ipage.getRecords();
}
}
}</pre>
*/
public interface AppPage<T> extends Serializable {
/** 当前页页码*/
public int getPageNum();
/** 每页条数*/
public int getPageSize();
/** 总条数*/
public long getTotal();
/** 总页数*/
public int getTotalPages();
/** 分页对象记录*/
public List<T> getItems();
}
package com.byx.cygnus.context.helper;
import java.util.List;
import org.springframework.data.domain.Page;
import com.byx.cygnus.common.entity.AppPage;
import lombok.Data;
/**
* 封裝jpa分页查询
*
* @param <T>
*/
@Data
public class JpaPageImpl<T> implements AppPage<T> {
private static final long serialVersionUID = 1L;
private Page<T> springPage;
public JpaPageImpl(){
}
public JpaPageImpl(Page<T> jpaPage){
this.springPage = jpaPage;
}
@Override
public int getPageNum() {
if (springPage == null) return 0;
return springPage.getNumber() + 1;
}
@Override
public int getPageSize() {
if (springPage == null) return 0;
return springPage.getSize();
}
@Override
public long getTotal() {
if (springPage == null) return 0;
return springPage.getTotalElements();
}
@Override
public int getTotalPages() {
if (springPage == null) return 0;
return springPage.getTotalPages();
}
@Override
public List<T> getItems() {
return springPage.getContent();
}
}
附:
COALESCE()函数
sql 中的COALESCE()函数
对于Oracle数据库,一般经常对空值处理的函数为NVL,而mysql中常用到的是ifnull,这两个函数相似,其实都是由一个函数衍生而来,那就是COALESCE()函数。
COALESCE()函数 定义:返回列表中第一个非null表达式的值。如果所有表达式求值为null,则返回null
COALESCE()函数有两种用法:
1.COALESCE ( expression1, expression2 );
2.COALESCE ( expression1, expression2, ... expression-n ); 依次参考各参数表达式,遇到非null值即停止并返回该值。如果所有的表达式都是空值,最终将返回一个空值。使用COALESCE在于大部分包含空值的表达式最终将返回空值。
其中第一种就相当于Oracle中的nvl或者mysql中的ifnull,写成表达式的形式为:
CASE WHEN expression1 IS NOT NULL THEN expression1 ELSE
expression2 END;
第二种可以包含n个表达式,表示如果第一个不为空取第一个,否则判断下一个,以此类推,如果全部为空,则返回null值。
需注意:vertica中空字符串跟null值不一样
MySQL IFNULL() 函数
IFNULL() 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。
IFNULL() 函数语法格式为:
IFNULL(expression, alt_value)
如果第一个参数的表达式 expression 为 NULL,则返回第二个参数的备用值。
参数 | 描述 |
---|---|
expression | 必须,要测试的值 |
alt_value | 必须,expression 表达式为 NULL 时返回的值 |