mybatis if标签使用总结

所属分类: 软件编程 / java 阅读数: 42
收藏 0 赞 0 分享

在项目开发中,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;
	}
 
	@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()方法 (上边代码中第二个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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

java 中maven pom.xml文件教程详解

这篇文章主要介绍了java 中maven pom.xml文件教程详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合netty的实现方法

这篇文章主要介绍了spring boot整合netty的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Netty与Spring Boot的整合实现

这篇文章主要介绍了Netty与Spring Boot的整合的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring动态加载bean后调用实现方法解析

这篇文章主要介绍了Spring动态加载bean后调用实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现画图板上画一条直线

这篇文章主要为大家详细介绍了java实现画图板上画一条直线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java通过python命令执行DataX任务的实例

今天小编就为大家分享一篇Java通过python命令执行DataX任务的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springBoot集成redis的key,value序列化的相关问题

这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现登录案例

这篇文章主要为大家详细介绍了java实现登录案例的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java解决请求跨域的两种方法

这篇文章主要为大家详细介绍了java解决请求跨域的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringBoot集成Beetl后统一处理页面异常的方法

这篇文章主要介绍了SpringBoot集成Beetl后统一处理页面异常的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多