فهرست منبع

add set 逻辑

hubin 7 سال پیش
والد
کامیت
ae3a289f90

+ 15 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractLambdaWrapper.java

@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
 package com.baomidou.mybatisplus.core.conditions;
 
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;

+ 0 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/select/EntityWrapper.java

@@ -15,7 +15,6 @@
  */
 package com.baomidou.mybatisplus.core.conditions.select;
 
-
 import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;

+ 36 - 8
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/LambdaUpdateWrapper.java

@@ -1,39 +1,67 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
 package com.baomidou.mybatisplus.core.conditions.update;
 
-import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
+import static java.util.stream.Collectors.joining;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.support.Property;
+
 /**
+ * <p>
+ * Lambda 更新封装
+ * </p>
+ *
  * @author hubin miemie HCL
  * @since 2018-05-30
  */
 public class LambdaUpdateWrapper<T> extends AbstractLambdaWrapper<T, LambdaUpdateWrapper<T>> {
+
     /**
      * SQL 更新字段内容,例如:name='1',age=2
      */
-    protected String sqlSet;
+    private List<String> expression = new ArrayList<>();
 
-    LambdaUpdateWrapper(T entity, String sqlSet, AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
+    LambdaUpdateWrapper(T entity, AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
         this.entity = entity;
-        this.sqlSet = sqlSet;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;
     }
 
     @Override
     public String getSqlSet() {
-        return sqlSet;
+        if (CollectionUtils.isEmpty(expression)) {
+            return null;
+        }
+        return expression.stream().collect(joining(","));
     }
 
-    public LambdaUpdateWrapper<T> setSqlSet(String sqlSet) {
-        this.sqlSet = sqlSet;
+    public LambdaUpdateWrapper<T> set(Property<T, ?> column, Object val) {
+        expression.add(String.format("%s=%s", columnToString(column), val));
         return typedThis();
     }
 
     @Override
     protected LambdaUpdateWrapper<T> instance(AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
-        return new LambdaUpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs);
+        return new LambdaUpdateWrapper<>(entity, paramNameSeq, paramNameValuePairs);
     }
 }

+ 34 - 15
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/UpdateWrapper.java

@@ -1,10 +1,30 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
 package com.baomidou.mybatisplus.core.conditions.update;
 
-import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
+import static java.util.stream.Collectors.joining;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+
 /**
  * <p>
  * Update 条件封装
@@ -18,40 +38,39 @@ public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T
     /**
      * SQL 更新字段内容,例如:name='1',age=2
      */
-    protected String sqlSet;
+    private List<String> expression = new ArrayList<>();
 
     public UpdateWrapper() {
-        this(null, null);
+        this(null);
     }
 
     public UpdateWrapper(T entity) {
-        this(entity, null);
-    }
-
-    public UpdateWrapper(T entity, String sqlSet) {
         this.entity = entity;
-        this.sqlSet = sqlSet;
         this.initNeed();
     }
 
-    private UpdateWrapper(T entity, String sqlSet, AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
+    private UpdateWrapper(T entity, AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
         this.entity = entity;
-        this.sqlSet = sqlSet;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;
     }
 
     public LambdaUpdateWrapper<T> stream() {
-        return new LambdaUpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs);
+        return new LambdaUpdateWrapper<>(entity, paramNameSeq, paramNameValuePairs);
     }
 
     @Override
     public String getSqlSet() {
-        return sqlSet;
+        if (CollectionUtils.isEmpty(expression)) {
+            return null;
+        }
+        return expression.stream().collect(joining(","));
     }
 
-    public void setSqlSet(String sqlSet) {
-        this.sqlSet = sqlSet;
+    public UpdateWrapper<T> set(String column, Object val) {
+        // todo 待优化
+        expression.add(String.format("%s=%s", column, val));
+        return typedThis();
     }
 
     @Override
@@ -61,6 +80,6 @@ public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T
 
     @Override
     protected UpdateWrapper<T> instance(AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
-        return new UpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs);
+        return new UpdateWrapper<>(entity, paramNameSeq, paramNameValuePairs);
     }
 }

+ 13 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/WrapperTest.java

@@ -2,6 +2,8 @@ package com.baomidou.mybatisplus.core.test;
 
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.select.EntityWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+
 import org.junit.Test;
 
 public class WrapperTest {
@@ -29,6 +31,17 @@ public class WrapperTest {
         ew.getParamNameValuePairs().forEach((k, v) -> System.out.println("key = " + k + " ; value = " + v));
     }
 
+    @Test
+    public void test2() {
+        UpdateWrapper<User> ew = new UpdateWrapper<User>()
+            .set("name", "三毛").set("id", 1)
+            .eq("xxx", 123)
+            .and(i -> i.eq("andx", 65444).and().le("ande", 66666))
+            .and().ne("xxx", 222);
+        log(ew.getSqlSet());
+        log(ew.getSqlSegment());
+    }
+
 //    public void test() {
 //        String sql = new QueryWrapper()
 //            .where("b.age > 18", condition ->