Mybatis(2)——Mybatis 时间段查询

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));
	}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值