溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在mybatis中使用if標簽

發布時間:2021-03-22 17:12:56 來源:億速云 閱讀:817 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關怎么在mybatis中使用if標簽,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

其一、使用 <if> 標簽判斷某一字段是否為空

其二、使用 <if> 標簽判斷傳入參數是否相等

具體代碼如下

數據庫表結構和數據

怎么在mybatis中使用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

怎么在mybatis中使用if標簽

2、訪問 http://localhost:9000/between

怎么在mybatis中使用if標簽

看完上述內容,你們對怎么在mybatis中使用if標簽有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女