mybatis if 标签使用总结

在项目开发中,mybatis <if> 标签使用广泛,本文讲解if标签的两种使用方式

其一、使用 <if> 标签判断某一字段是否为空

其二、使用 <if> 标签判断传入参数是否相等

 

具体代码如下

数据库表结构和数据

实体类

package com.demo.bean;

public class Commodity {
	
	private String name;
	
	private String date;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return "Com [name=" + name + ", date=" + date + "]";
	}
	
}

mapper层

package com.demo.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.demo.bean.Commodity;
@Mapper
public interface CommodityMapper {

	List<Commodity> getListByDate(Commodity commodity);
	
	List<Commodity> getListByStartDateAndEndDate(@Param("startDate")String startDate, @Param("endDate")String endDate);
}

mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.demo.mapper.CommodityMapper">
	
	<resultMap id="BaseResultMap" type="com.demo.bean.Commodity">
		<id column="name" property="name" jdbcType="VARCHAR" />
		<result column="date" property="date" jdbcType="VARCHAR" />
	</resultMap>
	
	<select id="getListByDate" resultMap="BaseResultMap">
	  select * from commodity where 1 = 1
	  <if test="date != null and date != ''">
	    and date = #{date}
	  </if> 
	</select>
	
	<select id="getListByStartDateAndEndDate" resultMap="BaseResultMap">
	  select * from commodity where 1 = 1
	  <if test="#{startDate}.toString() != #{endDate}.toString()">
	    and date between #{startDate} and #{endDate}
	  </if>
	</select>
	
</mapper>

注意:mybatis 等值判断的 tostring()方法 (上边代码中第二个select中的toString()方法)

controller层

package com.demo.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.demo.bean.Commodity;
import com.demo.mapper.CommodityMapper;

@RestController
public class DemoController {

	@Autowired
	private CommodityMapper comMapper;
	
	@RequestMapping(value = "/commodity")
	public Object commodity() {
		Map<String, Object> map = new HashMap<String, Object>();
		Commodity com =new Commodity();
		com.setDate("2018-10-12");
		map.put("res", comMapper.getListByDate(com));
		return map;
	}
	
	@RequestMapping(value = "/between")
	public Object commodityBetween() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("res", comMapper.getListByStartDateAndEndDate("2018-10-09", "2018-10-13"));
		return map;
	}
}

测试

1、访问  http://localhost:9000/commodity  

2、访问  http://localhost:9000/between

 

  • 17
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟世君子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值