mybatis中if标签怎么用

发布时间:2021-09-23 14:25:19 作者:小新
来源:亿速云 阅读:188

这篇文章主要介绍了mybatis中if标签怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

在项目开发中,mybatis <if> 标签使用广泛,本文讲解if标签的两种使用方式

其一、使用 <if> 标签判断某一字段是否为空

其二、使用 <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;} @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

感谢你能够认真阅读完这篇文章,希望小编分享的“mybatis中if标签怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. 怎么在mybatis中使用if标签
  2. Mybatis怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis if

上一篇:SpringAOP中AspectJ怎么用

下一篇:HTTP与RPC有哪些区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》