介绍
今天就跟大家聊聊有关怎么在mybatis中使用如果标签,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
其一,使用& lt; if>标签判断某一字段是否为空
其二,使用& lt; if>标签判断传入参数是否相等
具体代码如下
数据库表结构和数据
实体类
package com.demo.bean; , public class  Commodity { private String 名称; private String 日期; , public String  getName (), { return 名称; } , public void  setName (String 名称),{ this.name =,名称; } , public String 获取当前日期(),{ return 日期; } , public void 设置当前日期(String 日期),{ 时间=this.date 日期; } , @Override public String  toString (), { return “Com  [name=? +, name +,,,,=叭掌?+,date +,“]”; } }
映射层
package com.demo.mapper; , import 并不知道; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.demo.bean.Commodity; @Mapper public interface  CommodityMapper { , List映射器。xml文件
& lt; ? xml version=?.0“,编码=癠TF-8", ?比; & lt; ! DOCTYPE mapper  PUBLIC “-//mybatis.org//DTD mapper 3.0//EN",“http://mybatis.org/dtd/mybatis-3-mapper.dtd",在=& lt; mapper 名称空间“com.demo.mapper.CommodityMapper"祝辞 & lt; resultMap id=癇aseResultMap",类型=癱om.demo.bean.Commodity"比; & lt; id 列=皀ame",财产=皀ame", jdbcType=癡ARCHAR",/比; & lt; result 列=癲ate",财产=癲ate", jdbcType=癡ARCHAR",/比; & lt;/resultMap> & lt; select id=癵etListByDate", resultMap=癇aseResultMap"比; ,select *,得到commodity where 1,=, 1 ,& lt; if 测试=癲ate !=, null 以及date !=, & # 39; & # 39;“比; 时间=date 害;#{日期} & lt;/if>大敌; & lt;/select> & lt; select id=癵etListByStartDateAndEndDate", resultMap=癇aseResultMap"比; ,select *,得到commodity where 1,=, 1 ,& lt; if 测试=? {startDate可以}.toString (), !=, # {endDate} .toString()“比; date 害;结构;# {startDate可以},以及# {endDate} ,& lt;/if> & lt;/select> & lt;/mapper>注意:mybatis等值判断的tostring()方法(上边代码中第二个选择中的tostring()方法)控制器层
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 商品(),{ 字符串,Map测试
怎么在mybatis中使用如果标签