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

Mybatis时间段查询有两种形式,主要根据Java中传递的参数类型

一、Java中传递Date类型参数

1、Mybatis中配置:

  
  

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 timeSlot = new HashMap();
		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 invoices = invoiceService.queryInvoiceByTime(timeSlot);		
		logger.info("查询结束!");
		logger.info(JSON.toJSONString(invoices));
	}

}

二、Java中传递String类型参数

1、Mybatis中配置:

  
  

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 timeS = new HashMap();
		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 invoices = invoiceService.selectByTime(timeS);
		logger.info("查询结束!");
		logger.info(JSON.toJSONString(invoices));
	}

}


你可能感兴趣的:(Mybatis)