這篇文章主要介紹了mybatis中if標(biāo)簽怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),翠屏企業(yè)網(wǎng)站建設(shè),翠屏品牌網(wǎng)站建設(shè),網(wǎng)站定制,翠屏網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,翠屏網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎ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ù)
實體類
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;} @Overridepublic 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;@Mapperpublic 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; @RestControllerpublic class DemoController { @Autowiredprivate 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
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“mybatis中if標(biāo)簽怎么用”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
網(wǎng)站題目:mybatis中if標(biāo)簽怎么用
瀏覽路徑:http://chinadenli.net/article48/jiijep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、面包屑導(dǎo)航、App開發(fā)、企業(yè)建站、自適應(yīng)網(wǎng)站、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)