新增MapBeanUtils工具类
diff --git a/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java b/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java
index ff1fd0b..6cef7b8 100644
--- a/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java
+++ b/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java
@@ -10,6 +10,7 @@
import com.supwisdom.leaveschool.common.repository.BaseJpaRepository;
import com.supwisdom.leaveschool.user.domain.User;
+import com.supwisdom.leaveschool.user.util.MapBeanUtils;
@Repository
public interface UserRepository extends BaseJpaRepository<User> {
@@ -17,9 +18,9 @@
public default Page<User> selectPageList(int pageIndex, int pageSize, Map<String, Object> mapBean) {
User probe = new User();
if (mapBean != null) {
- probe.setUsername(String.valueOf(mapBean.get("username")));
- probe.setName(String.valueOf(mapBean.get("name")));
- probe.setStatus(String.valueOf(mapBean.get("status")));
+ probe.setUsername(MapBeanUtils.getString(mapBean, "username"));
+ probe.setName(MapBeanUtils.getString(mapBean, "name"));
+ probe.setStatus(MapBeanUtils.getString(mapBean, "status"));
}
ExampleMatcher matcher = ExampleMatcher.matching()
diff --git a/samples/user/src/main/java/com/supwisdom/leaveschool/user/util/MapBeanUtils.java b/samples/user/src/main/java/com/supwisdom/leaveschool/user/util/MapBeanUtils.java
new file mode 100644
index 0000000..9643b11
--- /dev/null
+++ b/samples/user/src/main/java/com/supwisdom/leaveschool/user/util/MapBeanUtils.java
@@ -0,0 +1,150 @@
+package com.supwisdom.leaveschool.user.util;
+
+import java.util.Map;
+
+public class MapBeanUtils {
+
+ /**
+ * 判断 mapBean 中的 key 是否存在;若存在,则判断是否有值
+ *
+ * @param mapBean
+ * @param key
+ * @return
+ */
+ public static boolean containsValue(Map<String, Object> mapBean, String key) {
+
+ if (!mapBean.containsKey(key)) {
+ return false;
+ }
+
+ if (mapBean.get(key) == null) {
+ return false;
+ }
+
+ if (String.valueOf(mapBean.get(key)).isEmpty()) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 null
+ *
+ * @param mapBean
+ * @param key
+ * @return
+ */
+ public static String getString(Map<String, Object> mapBean, String key) {
+
+ return getString(mapBean, key, null);
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 defaultValue
+ *
+ * @param mapBean
+ * @param key
+ * @param defaultValue
+ * @return
+ */
+ public static String getString(Map<String, Object> mapBean, String key, String defaultValue) {
+
+ if (containsValue(mapBean, key)) {
+ return String.valueOf(mapBean.get(key));
+ }
+
+ return defaultValue;
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 false
+ *
+ * @param mapBean
+ * @param key
+ * @return
+ */
+ public static boolean getBoolean(Map<String, Object> mapBean, String key) {
+
+ return getBoolean(mapBean, key, false);
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 defaultValue
+ *
+ * @param mapBean
+ * @param key
+ * @param defaultValue
+ * @return
+ */
+ public static boolean getBoolean(Map<String, Object> mapBean, String key, Boolean defaultValue) {
+
+ if (containsValue(mapBean, key)) {
+ Boolean b = Boolean.valueOf(String.valueOf(mapBean.get(key)));
+ return b == null ? defaultValue : b.booleanValue();
+ }
+
+ return defaultValue;
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 -1
+ *
+ * @param mapBean
+ * @param key
+ * @return
+ */
+ public static int getInteger(Map<String, Object> mapBean, String key) {
+
+ return getInteger(mapBean, key, -1);
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 defaultValue
+ *
+ * @param mapBean
+ * @param key
+ * @param defaultValue
+ * @return
+ */
+ public static int getInteger(Map<String, Object> mapBean, String key, Integer defaultValue) {
+
+ if (containsValue(mapBean, key)) {
+ Integer i = Integer.valueOf(String.valueOf(mapBean.get(key)));
+ return i == null ? defaultValue : i.intValue();
+ }
+
+ return defaultValue;
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 -1L
+ *
+ * @param mapBean
+ * @param key
+ * @return
+ */
+ public static long getLong(Map<String, Object> mapBean, String key) {
+
+ return getLong(mapBean, key, -1L);
+ }
+
+ /**
+ * 获取 mapBean 中 key 的 value,若不存在,则返回 defaultValue
+ *
+ * @param mapBean
+ * @param key
+ * @param defaultValue
+ * @return
+ */
+ public static long getLong(Map<String, Object> mapBean, String key, Long defaultValue) {
+
+ if (containsValue(mapBean, key)) {
+ Long l = Long.valueOf(String.valueOf(mapBean.get(key)));
+ return l == null ? defaultValue : l.longValue();
+ }
+
+ return defaultValue;
+ }
+
+}