MyBatis + MyBatis Plus date range query problem

Case 1: Date of string type is received from the front end

 <if test="startTime!=null and startTime.trim() neq ''">
    and date_format(p.create_time,'%Y-%m-%d %H:%i:%s') &gt;= str_to_date(#{
    
    startTime},'%Y-%m-%d %H:%i:%s')
  </if>
  <if test="endTime!=null and endTime.trim() neq ''">
    and date_format(p.create_time,'%Y-%m-%d %H:%i:%s') &lt;= str_to_date(#{
    
    endTime},'%Y-%m-%d %H:%i:%s')
  </if>

or

 <if test="beginTime != null and beginTime !='' ">
     and  <![CDATA[p.create_time >=DATE_ADD(#{
    
    beginTime },interval 1 day)]]>
 </if>
 <if test="endTime != null and endTime !=''">
     and  <![CDATA[p.create_time <=DATE_ADD(#{
    
    endTime},interval 1 day)]]>
 </if>

Case 2: Date type is received from the front end

 <if test="beginTime !=null">
     <![CDATA[and DATE_FORMAT(t1.create_Time,'%Y-%m-%d')<=DATE_FORMAT(#{
    
    beginTime },'%Y-%m-%d')]]>
 </if>
 <if test="endTime !=null">
     <![CDATA[and DATE_FORMAT(t1.create_Time,'%Y-%m-%d')>=DATE_FORMAT(#{
    
    endTime },'%Y-%m-%d')]]>
 </if>

Case 3: Date of string type is received from the front end, combined with MP

List<CustomerIntegral> customerIntegrals = customerIntegralService.list(new QueryWrapper<CustomerIntegral>().
        apply(StringUtils.isNotEmpty(todayString ),"date_format (create_time,'%Y-%m-%d') >= date_format('" + todayString + "','%Y-%m-%d')").
        apply(StringUtils.isNotEmpty(tomorrowString ),"date_format (create_time,'%Y-%m-%d') <= date_format('" + tomorrowString + "','%Y-%m-%d')"
        ));

Printed SQL

SELECT
	customer_integral_id,
	customer_data_id,
	integral,
	update_id,
	change_status,
	create_id,
	create_time,
	update_time,
	consumption_reasons 
FROM
	customer_integral 
WHERE
	date_format ( create_time, '%Y-%m-%d' ) >= date_format( 'yyyy-MM-dd', '%Y-%m-%d' ) 
	AND date_format ( create_time, '%Y-%m-%d' ) <= date_format( 'yyyy-MM-dd', '%Y-%m-%d' )

Note that todayString and tomorrowString must be formatted string types
such as:yyyy-MM-dd hh:mm:ss

Specific scenarios can be selected for specific scenarios, and it is generally better to pass strings.

Guess you like

Origin blog.csdn.net/JAYU_37/article/details/109222058