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í)有所幫助,也希望大家多多支持我們。
上一篇:JAVA實(shí)現(xiàn)二維碼生成加背景圖代碼實(shí)例
欄 目:Java
下一篇:Java8 HashMap擴(kuò)容算法實(shí)例解析
本文標(biāo)題:mybatis if標(biāo)簽使用總結(jié)
本文地址:http://mengdiqiu.com.cn/a1/Java/8883.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10使用Swing繪制動(dòng)態(tài)時(shí)鐘
- 01-10JAVA8獨(dú)有的map遍歷方式(非常好用)
- 01-10mybatis分頁絕對(duì)路徑寫法過程詳解
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10Java使用Scanner類進(jìn)行控制臺(tái)輸入實(shí)現(xiàn)方法
- 01-10springboot2.0使用Hikari連接池的方法(替換druid)
- 01-10MyBatis執(zhí)行Sql的流程實(shí)例解析
- 01-10mybatis之foreach用法詳解
- 01-10Spring實(shí)戰(zhàn)之使用@POSTConstruct和@PreDestroy定制生命周期行為操作示例


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery