马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
好久没有更新了,这次更新一个当前端需要对运算符和运算值都需要前端转达给后端,动态拼接运算条件时的处理处罚方法。
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;
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |