Ver código fonte

新增 bean map 互转工具类

hubin 7 anos atrás
pai
commit
55fe023abd

+ 4 - 3
build.gradle

@@ -18,9 +18,10 @@ ext {
         "spring-context-support": "org.springframework:spring-context-support:${springVersion}",
         "spring-context-support": "org.springframework:spring-context-support:${springVersion}",
         "spring-jdbc"           : "org.springframework:spring-jdbc:${springVersion}",
         "spring-jdbc"           : "org.springframework:spring-jdbc:${springVersion}",
         "spring-tx"             : "org.springframework:spring-tx:${springVersion}",
         "spring-tx"             : "org.springframework:spring-tx:${springVersion}",
-        "spring-web"             : "org.springframework:spring-web:${springVersion}",
+        "spring-web"            : "org.springframework:spring-web:${springVersion}",
+        "cglib"                 : "cglib:cglib:3.2.6",
 
 
-        "javax.servlet-api"         : "javax.servlet:javax.servlet-api:3.1.0",
+        "javax.servlet-api"     : "javax.servlet:javax.servlet-api:3.1.0",
         "aspectjweaver"         : "org.aspectj:aspectjweaver:1.8.9",
         "aspectjweaver"         : "org.aspectj:aspectjweaver:1.8.9",
         "mockito"               : "org.mockito:mockito-core:2.13.0",
         "mockito"               : "org.mockito:mockito-core:2.13.0",
         "mybatis-ehcache"       : "org.mybatis.caches:mybatis-ehcache:1.1.0",
         "mybatis-ehcache"       : "org.mybatis.caches:mybatis-ehcache:1.1.0",
@@ -29,7 +30,7 @@ ext {
         //test
         //test
         "spring-test"           : "org.springframework:spring-test:${springVersion}",
         "spring-test"           : "org.springframework:spring-test:${springVersion}",
         "junit"                 : "junit:junit:4.12",
         "junit"                 : "junit:junit:4.12",
-        "mockito-all"                 : "org.mockito:mockito-all:1.10.19",
+        "mockito-all"           : "org.mockito:mockito-all:1.10.19",
         "lombok"                : "org.projectlombok:lombok:1.16.20",
         "lombok"                : "org.projectlombok:lombok:1.16.20",
         "fastjson"              : "com.alibaba:fastjson:1.2.37",
         "fastjson"              : "com.alibaba:fastjson:1.2.37",
         "tomcatjdbc"            : "org.apache.tomcat:tomcat-jdbc:9.0.2",
         "tomcatjdbc"            : "org.apache.tomcat:tomcat-jdbc:9.0.2",

+ 2 - 0
mybatis-plus-core/build.gradle

@@ -7,6 +7,8 @@ dependencies {
     compile rootProject.ext.dependencies["mybatis"]
     compile rootProject.ext.dependencies["mybatis"]
     compile rootProject.ext.dependencies["jsqlparser"]
     compile rootProject.ext.dependencies["jsqlparser"]
 
 
+    provided rootProject.ext.dependencies["cglib"]
+
     testCompile rootProject.ext.dependencies["mybatis-ehcache"]
     testCompile rootProject.ext.dependencies["mybatis-ehcache"]
     testCompile rootProject.ext.dependencies["logback-classic"]
     testCompile rootProject.ext.dependencies["logback-classic"]
     testCompile(rootProject.ext.dependencies["commons-dbcp2"]) {
     testCompile(rootProject.ext.dependencies["commons-dbcp2"]) {

+ 16 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ArrayUtils.java

@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.toolkit;
 package com.baomidou.mybatisplus.core.toolkit;
 
 
 /**
 /**

+ 94 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/BeanUtils.java

@@ -0,0 +1,94 @@
+package com.baomidou.mybatisplus.core.toolkit;
+
+import static java.util.stream.Collectors.toList;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import net.sf.cglib.beans.BeanMap;
+
+/**
+ * <p>
+ * Bean 转换工具类<br/>
+ * 使用请依赖 cglib 包
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-12
+ */
+public class BeanUtils {
+
+    /**
+     * <p>
+     * 将对象装换为map
+     * </p>
+     *
+     * @param bean 转换对象
+     * @return
+     */
+    public static <T> Map<String, Object> beanToMap(T bean) {
+        if (bean != null) {
+            Map<String, Object> map = new HashMap<>(16);
+            BeanMap beanMap = BeanMap.create(bean);
+            for (Object key : beanMap.keySet()) {
+                map.put(String.valueOf(key), beanMap.get(key));
+            }
+            return map;
+        }
+        return null;
+    }
+
+    /**
+     * <p>
+     * map 装换为 java bean 对象
+     * </p>
+     *
+     * @param map   转换 MAP
+     * @param clazz 对象 Class
+     * @return
+     */
+    public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz) {
+        T bean = null;
+        try {
+            bean = clazz.newInstance();
+            BeanMap.create(bean).putAll(map);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return bean;
+    }
+
+    /**
+     * <p>
+     * List<T> 转换为 List<Map<String, Object>>
+     * </p>
+     *
+     * @param beanList 转换对象集合
+     * @return
+     */
+    public static <T> List<Map<String, Object>> beansToMaps(List<T> beanList) {
+        if (CollectionUtils.isEmpty(beanList)) {
+            return null;
+        }
+        return beanList.stream().map(e -> beanToMap(e)).collect(toList());
+    }
+
+    /**
+     * <p>
+     * List<Map<String,Object>> 转换为 List<T>
+     * </p>
+     *
+     * @param maps  转换 MAP 集合
+     * @param clazz 对象 Class
+     * @return
+     * @throws InstantiationException
+     * @throws IllegalAccessException
+     */
+    public static <T> List<T> mapsToBeans(List<Map<String, Object>> maps, Class<T> clazz) {
+        if (CollectionUtils.isNotEmpty(maps)) {
+            return null;
+        }
+        return maps.stream().map(e -> mapToBean(e, clazz)).collect(toList());
+    }
+}

+ 16 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ClassUtils.java

@@ -1,6 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.toolkit;
 package com.baomidou.mybatisplus.core.toolkit;
 
 
-
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 
 
 /**
 /**

+ 16 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/GlobalConfigUtils.java

@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.toolkit;
 package com.baomidou.mybatisplus.core.toolkit;
 
 
 import java.util.Map;
 import java.util.Map;

+ 0 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/IdWorker.java

@@ -17,7 +17,6 @@ package com.baomidou.mybatisplus.core.toolkit;
 
 
 import java.util.UUID;
 import java.util.UUID;
 
 
-
 /**
 /**
  * <p>
  * <p>
  * 高效GUID产生算法(sequence),基于Snowflake实现64位自增ID算法。 <br>
  * 高效GUID产生算法(sequence),基于Snowflake实现64位自增ID算法。 <br>

+ 19 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.toolkit;
 package com.baomidou.mybatisplus.core.toolkit;
 
 
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
@@ -12,10 +28,12 @@ import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentHashMap;
 
 
 /**
 /**
+ * <p>
  * Lambda 工具类
  * Lambda 工具类
+ * </p>
  *
  *
  * @author HCL
  * @author HCL
- * @since 2018/05/10
+ * @since 2018-05-10
  */
  */
 public final class LambdaUtils {
 public final class LambdaUtils {
 
 

+ 16 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/MapUtils.java

@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.toolkit;
 package com.baomidou.mybatisplus.core.toolkit;
 
 
 import java.util.Map;
 import java.util.Map;

+ 0 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java

@@ -25,7 +25,6 @@ import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.stream.Stream;
 
 
-
 /**
 /**
  * <p>
  * <p>
  * 反射工具类
  * 反射工具类

+ 16 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/SerializationUtils.java

@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.toolkit;
 package com.baomidou.mybatisplus.core.toolkit;
 
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayInputStream;