an oracle database
The database time type is DATE
TO_CHAR Convert the date or number to a string
TO_DATE Convert the string to the date type in the database TO_DATE(char, 'format')
TO_NUMBER Convert the string to a number TO_NUMBER(char, 'format')
1. The input parameter is String type data.
mybatis processing time range
Use the TO_DATE function to convert the String type time into the yyyy-MM-dd format time.
There will be problems with time range queries in this way. The = sign will be invalid at this time, because VOUCHER_TIME is specific to hours, minutes and seconds, and TO_DATE(#{jyzbLedger.voucherTimeFrom},'yyyy-MM-dd') is lost when it is converted into time. Hours, minutes and seconds cause the == judgment to be invalid. Eventually the equivalent data is lost
<if test="jyzbLedger.voucherTimeTo != null and jyzbLedger.voucherTimeTo != ''">
and VOUCHER_TIME <![CDATA[>= ]]> TO_DATE(#{jyzbLedger.voucherTimeTo},'yyyy-MM-dd')
</if>
<if test="jyzbLedger.voucherTimeFrom != null and jyzbLedger.voucherTimeFrom != ''">
and VOUCHER_TIME <![CDATA[<= ]]> TO_DATE(#{jyzbLedger.voucherTimeFrom},'yyyy-MM-dd')
</if>
correct way to write
First try TO_CHAR to convert the date into a yyyy-MM-dd format string, and then use TO_DATE to convert it into a date.
<if test="jyzbLedger.voucherTimeTo != null and jyzbLedger.voucherTimeTo != ''">
and TO_DATE( TO_CHAR( VOUCHER_TIME, 'yyyy-MM-dd' ), 'yyyy-MM-dd' ) <![CDATA[>= ]]> TO_DATE(#{jyzbLedger.voucherTimeTo},'yyyy-MM-dd')
</if>
<if test="jyzbLedger.voucherTimeFrom != null and jyzbLedger.voucherTimeFrom != ''">
and TO_DATE( TO_CHAR( VOUCHER_TIME, 'yyyy-MM-dd' ), 'yyyy-MM-dd' ) <![CDATA[<= ]]> TO_DATE(#{jyzbLedger.voucherTimeFrom},'yyyy-MM-dd')
</if>
Time range query in MybatisPlus
queryWrapper
.apply(ObjectUtils.isNotEmpty(jyzbLedger.getVoucherTimeTo()),
"TO_DATE( TO_CHAR( VOUCHER_TIME, 'yyyy-MM-dd' ), 'yyyy-MM-dd' ) >= TO_DATE ({0},'yyyy-MM-dd')", jyzbLedger.getVoucherTimeTo())
.apply(ObjectUtils.isNotEmpty(jyzbLedger.getVoucherTimeFrom()),
"TO_DATE( TO_CHAR( VOUCHER_TIME, 'yyyy-MM-dd' ), 'yyyy-MM-dd' ) <= TO_DATE ({0},'yyyy-MM-dd')", jyzbLedger.getVoucherTimeFrom());
TO_DATE( TO_CHAR( VOUCHER_TIME, ‘yyyy-MM-dd’ ), ‘yyyy-MM-dd’ )
2. The input parameter is time data of Date type.
Date类型
To receive parameters, you need to fix the parameter transfer format with the front end yyyy-MM-dd
, and then use it @DateTimeFormat格式化
. If you do not use @DateTimeFormat and then use the Date type to receive parameters, an error will be reported !
mybatis processing time range
TO_CHAR Convert date or number to string
Use the TO_CHAR function to convert the input date format into a specified format string, and convert the query parameters into a specified format string.
<!-- voucherTimeTo为Date类型时不能用 jyzbLedger.voucherTimeTo != '' 判空会报错的 -->
<if test="jyzbLedger.voucherTimeTo != null ">
and to_char(VOUCHER_TIME,'yyyy-MM-dd') <![CDATA[>= ]]> to_char(#{jyzbLedger.voucherTimeTo},'yyyy-MM-dd')
</if>
<if test="jyzbLedger.voucherTimeFrom != null ">
and to_char(VOUCHER_TIME,'yyyy-MM-dd') <![CDATA[<= ]]> to_char(#{jyzbLedger.voucherTimeFrom},'yyyy-MM-dd')
</if>
Time range query in MybatisPlus
Refer to the above
Two mysql database
The database time type is DATE
2. The input parameter is time data of Date type.
- Entering
achievementDateStart: 2023-04-26
achievementDateStartEnd: 2023-05-19
- Receive parameter type
/**
* 业绩时间-开始
*/
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@ApiModelProperty(value = "业绩时间-开始")
private Date achievementDateStart;
/**
* 业绩时间-结束
*/
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@ApiModelProperty(value = "业绩时间-结束")
private Date achievementDateStartEnd;
mybatis processing time range
date_format formats Date type parameters into the specified type of time
<if test="oppConditionVO.achievementDateStart != null and oppConditionVO.achievementDateStartEnd != null">
and date_format( achievement_date, '%Y-%m-%d' )
BETWEEN date_format( #{oppConditionVO.achievementDateStart}, '%Y-%m-%d' )
and date_format( #{oppConditionVO.achievementDateStartEnd}, '%Y-%m-%d' )
</if>
Time range query in MybatisPlus
queryWrapper.apply(oppConditionVO.getAchievementDateStart() != null,
"date_format (achievement_date,'%Y-%m-%d') >= date_format ({0},'%Y-%m-%d')", oppConditionVO.getAchievementDateStart())
.apply(oppConditionVO.getAchievementDateStartEnd() != null,
"date_format (achievement_date,'%Y-%m-%d') <= date_format ({0},'%Y-%m-%d')", oppConditionVO.getAchievementDateStartEnd());