if标签
用于判断条件是否成立。使用test属性进行条件判断,如果条件为true,则拼接sql。
<where>标签用于识别语句是否需要连接词and,识别sql语句。
package com.t0.maybatisc.mapper;
import com.t0.maybatisc.pojo.Emp;
import org.apache.ibatis.annotations.*;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
// 条件查询
public List<Emp> list( String name, Short gender, LocalDate begin, LocalDate end);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.t0.maybatisc.mapper.EmpMapper">
<select id="list" resultType="com.t0.maybatisc.pojo.Emp">
select * from emp
<where>
<if test="name != null">
name like concat('%',#{name},'%')
</if>
<if test="gender != null"&g