mybatis中if标签判断 属性!=’‘和属性.trim!=’'区别
1.准备测试环境
建表脚本
CREATE TABLE `tbl_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(255) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
-- 库中数据
select * from tbl_employee;
+----+-----------+--------+------------+
| id | last_name | gender | email |
+----+-----------+--------+------------+
| 1 | tom | 0 | tom@qq.com |
| 2 | tom | 0 | tom@qq.com |
| 3 | tom | 0 | tom@qq.com |
+----+-----------+--------+------------+
3 rows in set (0.000 sec)
说明:
除了主键,其余列默认可以为null,但是不建议这样,实际工作中一般字段都是非空
实体类
package com.shaoming.model.entity;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "employee实体类",description = "对应数据库的tbl_employee表")
public class Employee {
@ApiModelProperty("主键id")
private Integer id;
@ApiModelProperty("员工姓名")
private String lastName;
@ApiModelProperty("性别")
private Character gender;
@ApiModelProperty("邮箱")
private String email;
}
说明:
方便接口测试,引入swagger
mapper接口
@Mapper
public interface EmployeeMapper {
/**
* 测试where和if标签
*
* @param employee
* @return
*/
List<Employee> getEmpsByConditionIf(Employee employee);
}
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.shaoming.mapper.EmployeeMapper">
<select id="getEmpsByConditionIf" resultType="com.shaoming.model.entity.Employee"
parameterType="com.shaoming.model.entity.Employee">
select *
from tbl_employee
<where>
<if test="id != null">
and id=#{id}
</if>
<if test="lastName != null and lastName != ''">
and last_name like #{lastName}
</if>
<if test="gender == 0 or gender == 1">
and gender = #{gender}
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</where>
</select>
</mapper>
controller
package com.shaoming.controller;
@Api(tags = "测试controller")
@RestController
public class HelloController {
@Autowired
private EmployeeMapper employeeMapper;
@ApiOperation("get请求,方式为属性名=属性值")
@GetMapping("/gettrim")//get请求
public R gettrim(Employee employee) {
System.out.println("测试gettrim");
List<Employee> empList = employeeMapper.getEmpsByConditionIf(employee);
return R.ok("成功").put("data", empList);
}
@ApiOperation("post请求,参数为json格式")
@PostMapping("/posttrim")//get请求
public R posttrim(@RequestBody Employee employee) {
System.out.println("测试gettrim");
List<Employee> empList = employeeMapper.getEmpsByConditionIf(employee);
return R.ok("成功").put("data", empList);
}
}
测试的swagger界面
2.测试
参数中email为null或者为""
get请求
可以查出数据
post请求
可以查出数据
参数中email为" "(email为长度不为0的空串)
get请求
查询不到数据
post请求
查询不到数据
3.修改mapper.xml测试
修改后的mapper.xml
<select id="getEmpsByConditionIf" resultType="com.shaoming.model.entity.Employee"
parameterType="com.shaoming.model.entity.Employee">
select *
from tbl_employee
<where>
<if test="id != null">
and id=#{id}
</if>
<if test="lastName != null and lastName != ''">
and last_name like #{lastName}
</if>
<if test="gender == 0 or gender == 1">
and gender = #{gender}
</if>
<if test="email != null and email.trim() != ''">
and email = #{email}
</if>
</where>
</select>
修改的地方
把 email != '' 改为 email.trim() != ''
继续测试
参数中email为" "(email为长度不为0的空串)
按照以上请求可以查出数据
4.总结
mybatis判断属性字符窜不为空且不为空字符窜可以写成如下
<if test="email != null and email.trim() != ''">
and email = #{email}
</if>
il为" "(email为长度不为0的空串)
按照以上请求可以查出数据
4.总结
mybatis判断属性字符窜不为空且不为空字符窜可以写成如下
<if test="email != null and email.trim() != ''">
and email = #{email}
</if>
属性.trim()可以避免传来长度不为0的空串