首页 > 技术文章 > mybatis-plus解决时间范围查询同一天bug

hyy9527 原文

/**
 * 参数类
 */
@Data
public class T {
    private String startDate;
    private String endDate;
}
/**
 * test
 *实体类
 * @author
 */
@Data
@TableName("test")
public class Test implements Serializable {
    @TableId("id")
    private Integer id;
    @TableField("date")
    //出参
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8")
    //入参
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date date;

    private static final long serialVersionUID = 1L;
}
import java.util.List;

@Service
@Slf4j
public class TestService {
    @Autowired
    private TestDao testDao;

    public List<Test> getT(T t) {
        LambdaQueryWrapper<Test> te = new LambdaQueryWrapper<Test>();
        String startDate = t.getStartDate();
        String endDate = t.getEndDate();
        te.ge(Test::getDate, startDate).apply("DATE_FORMAT(date,'%Y-%m-%d') <= DATE_FORMAT({0},'%Y-%m-%d')", endDate);
        List<Test> tests = testDao.selectList(te);
        return tests;
    }

}

效果:

 

推荐阅读