Fork me on GitHub

mybatis-plus解决时间范围查询同一天bug

/**
 * 参数类
 */
@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;
    }

}

效果:

 

 

 

posted @ 2020-11-12 16:02  亲爸爸  阅读(3633)  评论(0编辑  收藏  举报