Mybatis时间段查询有两种形式,主要根据Java中传递的参数类型
一、Java中传递Date类型参数
1、Mybatis中配置:
<!-- 时间段查询传参Date -->
<select id="queryInvoiceByTime" resultMap="BaseResultMap" parameterType="java.util.HashMap" >
select
<include refid="Base_Column_List" />
from WS_IPS_PURCHASEPLAN_IDOC_V
<where>
<if test="startTime!=null and startTime!=''">
CREATETIME >= #{startTime,jdbcType=TIMESTAMP}
</if>
<if test="endTime!=null and endTime!=''">
and CREATETIME <= #{endTime,jdbcType=TIMESTAMP}
</if>
</where>
</select>
2、Java代码:
package com.cah.xhy.service.impl;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.alibaba.fastjson.JSON;
import com.cah.xhy.model.Invoice;
import com.cah.xhy.service.IInvoiceService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:springContext.xml"})
public class InvoiceServiceImplTest {
private static Logger logger = Logger.getLogger(PurchasePlanServiceImplTest.class);
@Resource
private IInvoiceService invoiceService;
@Test
public void testQueryInvoiceByTime() {
Map<String,Date> timeSlot = new HashMap<String,Date>();
Calendar eTime = Calendar.getInstance();
Calendar sTime = Calendar.getInstance();
sTime.add(Calendar.DAY_OF_MONTH, -10);
Date startDate = sTime.getTime();
Date endDate = eTime.getTime();
timeSlot.put("startTime", startDate);
timeSlot.put("endTime", endDate);
logger.info("查询开始!");
List<Invoice> invoices = invoiceService.queryInvoiceByTime(timeSlot);
logger.info("查询结束!");
logger.info(JSON.toJSONString(invoices));
}
}
二、Java中传递String类型参数
1、Mybatis中配置:
<!-- 时间查询传参String -->
<select id="selectByTime" resultMap="BaseResultMap" parameterType="java.util.HashMap" >
select
<include refid="Base_Column_List" />
from WS_IPS_PURCHASEPLAN_IDOC_V
where CREATETIME >= to_date(#{createTime,jdbcType=VARCHAR},'yyyy-MM-dd hh24:mi:ss')
and SALESDATE >= to_date(#{salesDate,jdbcType=VARCHAR},'yyyy-MM-dd hh24:mi:ss')
</select>
2、Java代码:
package com.cah.xhy.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.alibaba.fastjson.JSON;
import com.cah.xhy.model.Invoice;
import com.cah.xhy.service.IInvoiceService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:springContext.xml"})
public class InvoiceServiceImplTest {
private static Logger logger = Logger.getLogger(PurchasePlanServiceImplTest.class);
@Resource
private IInvoiceService invoiceService;
@Test
public void testQueryInvoiceByTime() {
Map<String,String> timeS = new HashMap<String,String>();
String createTime = "2018/3/14 5:00:00";
String salesDate = "2018/3/13 5:00:00";
timeS.put("createTime", createTime);
timeS.put("salesDate", salesDate);
logger.info("查询开始!");
List<Invoice> invoices = invoiceService.selectByTime(timeS);
logger.info("查询结束!");
logger.info(JSON.toJSONString(invoices));
}
}