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

本文介绍了使用MyBatis进行时间段查询的两种方法,一种是通过传递Date类型的参数,另一种是通过传递String类型的参数,并提供了具体的XML配置示例及对应的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}


### MyBatis 中嵌套查询与嵌套结果的区别 #### 嵌套查询 嵌套查询是指在一个 SQL 查询中执行另一个独立的 SQL 查询来获取关联数据。这种方式通常用于处理复杂的一对多或多对多关系,其中每次都需要单独发起额外的数据库请求。 ```java @Test public void testOrderWithUser(){ // 获取会话对象并创建 Mapper 接口实例 try(SqlSession sqlSession = MybatisUtils.getSession()){ OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); // 调用带有嵌套查询的方法 List<Orders> orders = mapper.findAllWithUser1(); // 输出订单及其对应的用户信息 for (Orders order : orders) { System.out.println(order); } } } ``` 此代码片段展示了如何通过 `findAllWithUser1` 方法实现基于用户的订单列表查询[^4]。每当需要加载某个特定实体的相关联实体时,都会触发新的 SQL 请求去检索这些子项的数据。 然而需要注意的是,这种做法可能会导致 N+1 的问题——即初始查询加上针对每一行记录所发出的附加查询,从而增加了不必要的网络往返次数和整体延迟时间[^2]。 #### 嵌套结果 相比之下,嵌套结果则是指利用单一 SQL 语句完成所有必要的连接操作,并一次性返回完整的结构化数据集给应用程序层面上的应用逻辑进行解析。这有助于减少对外部资源(如 RDBMS)的压力以及提高响应速度。 ```xml <!-- resultMap 定义 --> <resultMap id="orderResultMap" type="com.example.Orders"> <!-- 主表字段映射 --> <id property="orderId" column="ORDER_ID"/> <!-- 关联对象映射 --> <association property="user" javaType="com.example.User"> <id property="userId" column="USER_ID"/> ... </association> </resultMap> <select id="findOrdersWithUsers" resultMap="orderResultMap"> SELECT o.*, u.* FROM Orders o JOIN Users u ON o.USER_ID = u.ID; </select> ``` 上述 XML 片段定义了一个名为 `orderResultMap` 的 Result Map 来描述怎样把来自两个不同表格 (`Orders`, `Users`) 的列组合成 Java 类型的对象属性。这里使用了 `<association>` 标签指定一对一的关系;而对于一对多的情形,则可以采用类似的 `<collection>` 元素[^3]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值