It hasn't been updated for a long time. This time, the process of updating a current end requires the front-end to pass both operators and operation values to the back-end, which is a processing method when dynamically splicing operation conditions.
1. Step on the mine
Query age >=20, where >= front-end drop-down box is selected, and 20 value front-end drop-down box is selected
1) User table:
CREATE TABLE `user` ( `id` bigint(20) NOT NULL COMMENT 'Primary Key', `name` varchar(12) COMMENT 'User Name', `hobby` varchar(12) DEFAULT NULL COMMENT 'Hobby', `age` int(11) DEFAULT NULL COMMENT 'User age', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='User Table';
2) Define VO:
import ; @Data public class UserVO extends User { /** * Operation operator:>=/<=/= */ private String operateStr; }
3) Mapper content
<select resultType=""> select * from user <where> <if test="!= null and != '' and != null"> and avg_delay ${} #{} </if> </where> </select>
Although there is no problem with accepting parameters when writing this way, an error occurred when entering the Mapper layer to query, and the symbol cannot be recognized.
2. Adjustment
1) Define the enumeration
package ; import ; import ; @Getter @AllArgsConstructor public enum OperateEnum { // Greater than or equal to GREATER_THAN_OR_EQUAL_TO(1, ">="), // equal to BE_EQUAL_TO(2, "="), // Less than or equal to LESS_THAN_OR_EQUAL_TO(3, "<="); /** * Operator corresponds to integer value */ private final Integer operateIntValue; /** * condition */ private final String condition; /** * Get the conditions based on the value * * @param value * @return condition */ public static String getConditionByIntValue(Integer value) { for (OperateEnum item : ()) { if (().equals(value)) { return (); } } return null; } }
This is the end of this article about MybatisPlus operator and operation value. For more related content on MybatisPlus operation value, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!