Problema de consulta de intervalo de datas MyBatis + MyBatis Plus

Caso 1: a data do tipo de string é recebida do 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>

ou

 <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>

Caso 2: o tipo de data é recebido do 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>

Caso 3: a data do tipo de string é recebida do front end, combinada com 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')"
        ));

SQL impresso

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' )

Observe que todayString e tomorrowString devem ser tipos de string formatados
, como:yyyy-MM-dd hh:mm:ss

Cenários específicos podem ser selecionados para cenários específicos e geralmente é melhor passar strings.

Acho que você gosta

Origin blog.csdn.net/JAYU_37/article/details/109222058
Recomendado
Clasificación