好久没有更新了,这次更新一个当前端需要对运算符和运算值都需要前端传递给后端,动态拼接运算条件时的处理方法。
1、踩雷
查询年龄 >=20,其中>=前端下拉框选择,20值前端下拉框选择
1)用户表:
create table `user` ( `id` bigint(20) not null comment '主键', `name` varchar(12) comment '用户名称', `hobby` varchar(12) default null comment '爱好', `age` int(11) default null comment '用户年龄', primary key (`id`) using btree ) engine=innodb default charset=utf8mb4 row_format=dynamic comment='用户表';
2)定义vo:
import lombok.data; @data public class uservo extends user { /** * 操作运算符:>=/<=/= */ private string operatestr; }
3)mapper内容
<select id="selectusers" resulttype="org.springboot.xg.vo.uservo"> select * from user <where> <if test="uservo.operatestr!= null and uservo.operatestr != '' and uservo.age!= null"> and avg_delay ${uservo.operatestr} #{uservo.age} </if> </where> </select>
这样写虽然接受参数没有问题,但是在进入mapper层查询时出报错,不能识别符号。
2、调整
1)定义枚举
package org.springboot.xg.enums; import lombok.allargsconstructor; import lombok.getter; @getter @allargsconstructor public enum operateenum { // 大于等于 greater_than_or_equal_to(1, ">="), // 等于 be_equal_to(2, "="), // 小于等于 less_than_or_equal_to(3, "<="); /** * 操作符对应整数值 */ private final integer operateintvalue; /** * 条件 */ private final string condition; /** * 根据值获取条件 * * @param value 值 * @return 条件 */ public static string getconditionbyintvalue(integer value) { for (operateenum item : operateenum.values()) { if (item.getoperateintvalue().equals(value)) { return item.getcondition(); } } return null; } }
到此这篇关于mybatisplus操作符和运算值的文章就介绍到这了,更多相关mybatisplus运算值内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论