mybatis if標(biāo)簽使用總結(jié)
在項(xiàng)目開發(fā)中,mybatis <if> 標(biāo)簽使用廣泛,本文講解if標(biāo)簽的兩種使用方式
其一、使用 <if> 標(biāo)簽判斷某一字段是否為空
其二、使用 <if> 標(biāo)簽判斷傳入?yún)?shù)是否相等
具體代碼如下
數(shù)據(jù)庫表結(jié)構(gòu)和數(shù)據(jù)

實(shí)體類
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()方法 (上邊代碼中第二個(gè)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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
完美解決idea moudle沒有藍(lán)色的小方塊的問題
這篇文章主要介紹了完美解決idea moudle沒有藍(lán)色的小方塊的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
在java List中進(jìn)行模糊查詢的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄趈ava List中進(jìn)行模糊查詢的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
淺析對(duì)Java關(guān)鍵字final和static的理解
本文主要給大家談?wù)勑【帉?duì)java關(guān)鍵字final和static的理解,本文給大家介紹的較詳細(xì),需要的朋友參考參考下2017-04-04
Spring中的BeanFactory與FactoryBean區(qū)別詳解
這篇文章主要介紹了Spring中的BeanFactory與FactoryBean區(qū)別詳解,BeanFactory是一個(gè)接口,它是spring中的一個(gè)工廠,FactoryBean也是一個(gè)接口,實(shí)現(xiàn)了3個(gè)方法,通過重寫其中方法自定義生成bean,需要的朋友可以參考下2024-01-01
SpringBoot進(jìn)行參數(shù)校驗(yàn)的方法詳解
在日常的接口開發(fā)中,為了防止非法參數(shù)對(duì)業(yè)務(wù)造成影響,經(jīng)常需要對(duì)接口的參數(shù)進(jìn)行校驗(yàn)。本文通過示例詳細(xì)講解了SpringBoot如何進(jìn)行參數(shù)校驗(yàn)的,感興趣的可以學(xué)習(xí)一下2022-04-04
IntelliJ IDEA 部署 Web 項(xiàng)目,看這一篇夠了!
這篇文章主要介紹了IntelliJ IDEA 部署 Web 項(xiàng)目的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

