SpringBoot模糊查询加分页

@SpringBoot模糊查询加分页

实体

Student

@Entity
@Table(name = “Student”)
@Slf4j
@Audited
@Data
public class Student extends BaseModel implements Serializable {
private String name;//学生姓名
private String zy;//专业
private String xb;//系别
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “yyyy-MM-dd HH:mm:ss”)
private LocalDateTime rxsj; //入学时间
private Long xskh; //学生卡号
private String bj;//班级
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(id, student.id);
}
public int hashCode() {
return Objects.hash(id);
}

}

StudentDTO

@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class StudentDTO extends BaseDTO {
private String name;//学生姓名
private String zy;//专业
private String xb;//系别
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “yyyy-MM-dd HH:mm:ss”)
private LocalDateTime rxsj; //入学时间
private Long xskh; //学生卡号
private String bj;//班级
}

Page

@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class ZhjPage {
private Long total;
private List studentDTO;
}

BaseModel

@MappedSuperclass
@Data
@EntityListeners(AuditingEntityListener.class)
@Audited
public class BaseModel {
public static final String SOFT_DELETED_CLAUSE = “isDeleted = false”;

@Id
@GeneratedValue(generator = "uuid_short")
@GenericGenerator(name = "uuid_short",strategy = "com.zgy.handle.cardService.model.hibernate.UUIDGenerator")
@Column(name = "id",nullable = false)
@JSONField(serializeUsing = ToStringSerializer.class)
protected Long id;

@JsonIgnore
protected boolean isDeleted = false;
@CreatedBy
protected String creator;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@CreationTimestamp
protected Date createTime;
@LastModifiedBy
protected String updator;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@UpdateTimestamp
protected Date updateTime;

@ApiModelProperty("备注")
@Column(name = "note",columnDefinition = "text null")
protected String note;


private String createdId; // 创建人的id
private String belongId; // 归属id  部门或者企业

}

数据处理层

@Repository
public interface ZhjQueryRepository extends QueryRepository {
List findByNameLike(String Name);//先模糊查出数据
}

逻辑处理层

接口类

public interface ZhjQueryService extends QueryService {
Page getList(String Name,int page,int size);
}

接口实现类

@Service
@Slf4j
public class ZhjQueryServiceImpl extends QueryServiceImpl implements ZhjQueryService{
@Autowired
private ZhjMapper zhjMapper;
private ZhjQueryRepository zhjQueryRepository;
public ZhjQueryServiceImpl(ZhjQueryRepository zhjQueryRepository) {
super(zhjQueryRepository);
this.zhjQueryRepository = zhjQueryRepository;
}

@Override
public  Optional findByName(String name) {
    return Optional.empty();
}

@Override
public Specification querySpecification(StudentDTO dto) {
    return null;
}

//下面是分页逻辑
@Override
public Page getList(String name,int page,int size) {
List studentDTOList = new ArrayList<>();
Pageable pageable = PageRequest.of(page , size);//分页接口类参数可变。
List studentList = zhjQueryRepository.findByNameLike("%"+name+"%");
studentDTOList=zhjMapper.toStudentDTOList(studentList);
//当前页第一条数据在list中的位置
int start = (int) pageable.getOffset();
//当前页最后一条数据在list中的位置
int end = (start + pageable.getPageSize()) > studentDTOList.size() ? studentDTOList.size() : (start + pageable.getPageSize());
Page pagestudentdto = new PageImpl (studentDTOList.subList(start, end), pageable, studentDTOList.size());
return pagestudentdto;
}
}

Controller层

@RestController
@Slf4j
@RequestMapping(value = “student/query”)
public class ZhjQuaryController extends QueryController {
@Autowired
private ZhjMapper zhjMapper;
private ZhjUpdateService zhjUpdateService;
private ZhjQueryService zhjQueryService;
private ZhjQueryServiceImpl zhjQueryServiceImpl;
public ZhjQuaryController(ZhjUpdateService zhjUpdateService, ZhjQueryService zhjQueryService) {
super(zhjUpdateService, zhjQueryService);
this.zhjUpdateService=zhjUpdateService;
this.zhjQueryService=zhjQueryService;
}
@GetMapping(value = “mohu/{name}”, produces = { “application/json;charset=UTF-8”})
public ResponseCode getlistlikename(String name,int page,int size){
ResponseCode responseCode = ResponseCode.sucess();
responseCode.setDataInfo(zhjQueryService.getList(name,page,size));
return responseCode;
}
@Override
public List convertTtoSelectDTOList(List students) {
return null;
}
@Override
public List convertTtoU(List students) {
return zhjMapper.toStudentDTOList(students);
}
@Override
public StudentDTO convertTtoU(Student student) {
return zhjMapper.toStudentDTO(student);
}
@Override
public Student convertUtoT(StudentDTO studentDTO) {
return zhjMapper.toStudent(studentDTO);
}
}

你可能感兴趣的:(笔记,java,spring,boot)