How to use if tag in mybatis
This article mainly introduces how to use the if tag in mybatis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand it.
Mybatis tags are widely used in project development. This article explains two ways of using if tags.
First, use the label to determine whether a field is empty or not
Second, use tags to determine whether the incoming parameters are equal.
The specific code is as follows
Database table structure and data
Entity class
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 layer
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 getListByDate (Commodity commodity); List getListByStartDateAndEndDate (@ Param ("startDate") String startDate, @ Param ("endDate") String endDate;}
Mapper.xml file
Select * from commodity where 1 = 1 and date = # {date} select * from commodity where 1 = 1 and date between # {startDate} and # {endDate}
Note: the tostring () method of mybatis equivalence judgment (the toString () method in the second select in the above code)
Controller layer
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 map = new HashMap () Commodity com = new Commodity (); com.setDate ("2018-10-12"); map.put ("res", comMapper.getListByDate (com)); return map;} @ RequestMapping (value = "/ between") public Object commodityBetween () {Map map = new HashMap (); map.put ("res", comMapper.getListByStartDateAndEndDate ("2018-10-09", "2018-10-13")); return map;}}
test
1. Visit http://localhost:9000/commodity
2. Visit http://localhost:9000/between
Thank you for reading this article carefully. I hope the article "how to use if tags in mybatis" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!