LambdaQueryWrapper⽇期格式转换,匹配精确⽇期及为空⽇期/**
* ⽇期过滤
*/
private void dateFilter(String dateField, LambdaQueryWrapper<TmpOrderAuxiliaryPlanEntity> wrapper, String fieldName) {
List<String> dateArray;
String sqlDateField = DataConversionUtil.humpToUnderline(fieldName);
dateArray = Arrays.stream(dateField.split(Constant.SEP)).List());
StringBuilder sql = new StringBuilder();
for (int i = 0; i < dateArray.size(); i++) {
sql.append(Constant.SINGLE_QUOTE).(i)).append(Constant.SINGLE_QUOTE).append(Constant.SEP);
}
String stringSql = sql.deleteCharAt(sql.length() - 1).toString();
//查询⽇期满⾜条件且为空的值
//wrapper.apply("DATE_FORMAT(" + sqlDateField + ",'%Y-%m-%d') in (" + stringSql + ") or " + sqlDateField + " is null");
if (ains(Constant.NULL)) {
//wrapper.apply("" + sqlDateField + " is null");
if (sqlDateField.equals(DataConversionUtil.humpToUnderline(Constant.PLANNED_DELIVERY_DATE))) {
wrapper.apply("DATE_FORMAT(" + sqlDateField + ",'%Y-%m-%d') in (" + stringSql + ")").or().isNull(TmpOrderAuxiliaryPlanEntity::getPlannedDeliveryDate);
//wrapper.apply("DATE_FORMAT(" + sqlDateField + ",'%Y-%m-%d') in (" + stringSql + ")");
}
} else {
wrapper.apply("DATE_FORMAT(" + sqlDateField + ",'%Y-%m-%d') in (" + stringSql + ")");
string转date的方法}
}
注意事项:
查询为空不要这样写:
wrapper.apply("DATE_FORMAT(" + sqlDateField + ",'%Y-%m-%d') in (" + stringSql + ") or " + sqlDateField + " is null"); 会影响其他wrapper拼接的sql
正确写法:
wrapper.apply("DATE_FORMAT("( + sqlDateField + ",'%Y-%m-%d') in (" + stringSql + ") or " + sqlDateField + " is null)");
或:
wrapper.apply("DATE_FORMAT(" + sqlDateField + ",'%Y-%m-%d') in (" + stringSql + ")").or().isNull(TmpOrderAuxiliaryPlanEntity::getPlannedDeliveryDate);
说明:
wrapper.apply():sql⾃由拼接⽅法可能会有sql注⼊风险也可⽤于MySql 可⽤于.last()⽅法之前
LambdaQueryWrapper:可以直接从实体中获取值,避免硬编码
DATE_FORMAT() sql函数⽤于⽇期格式转换
Constant.SEP == “,” (逗号)
Constant.SINGLE_QUOTE == “'” (单引号)、
DataConversionUtil.humpToUnderline() :驼峰转下划线⽅法
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论