瀏覽代碼

新增通用枚举处理器,待完善

= 7 年之前
父節點
當前提交
66851cf3b7

+ 35 - 0
src/main/java/com/baomidou/mybatisplus/enums/IEnum.java

@@ -0,0 +1,35 @@
+/**
+ * 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.enums;
+
+import java.io.Serializable;
+
+/**
+ * <p>
+ * 自定义枚举接口
+ * </p>
+ *
+ * @author hubin
+ * @Date 2017-10-11
+ */
+public interface IEnum {
+
+    /**
+     * 枚举数据库存储值
+     */
+    Serializable getValue();
+
+}

+ 76 - 0
src/main/java/com/baomidou/mybatisplus/enums/IEnumTypeHandler.java

@@ -0,0 +1,76 @@
+/**
+ * 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.enums;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+
+import com.baomidou.mybatisplus.toolkit.EnumUtils;
+
+/**
+ * <p>
+ * 自定义枚举属性转换器
+ * </p>
+ *
+ * @author hubin
+ * @Date 2017-10-11
+ */
+public class IEnumTypeHandler<E extends Enum<?> & IEnum> extends BaseTypeHandler<IEnum> {
+
+    private Class<E> type;
+
+    public IEnumTypeHandler(Class<E> type) {
+        if (type == null) {
+            throw new IllegalArgumentException("Type argument cannot be null");
+        }
+        this.type = type;
+    }
+
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, IEnum parameter, JdbcType jdbcType)
+            throws SQLException {
+        ps.setObject(i, parameter.getValue());
+    }
+
+    @Override
+    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
+        if (rs.wasNull()) {
+            return null;
+        }
+        return EnumUtils.valueOf(type, rs.getObject(columnName));
+    }
+
+    @Override
+    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+        if (rs.wasNull()) {
+            return null;
+        }
+        return EnumUtils.valueOf(type, rs.getObject(columnIndex));
+    }
+
+    @Override
+    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+        if (cs.wasNull()) {
+            return null;
+        }
+        return EnumUtils.valueOf(type, cs.getObject(columnIndex));
+    }
+}

+ 1 - 1
src/main/java/com/baomidou/mybatisplus/enums/IdType.java

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * 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.

+ 4 - 0
src/main/java/com/baomidou/mybatisplus/plugins/PaginationInterceptor.java

@@ -177,6 +177,7 @@ public class PaginationInterceptor extends SqlParserHandler implements Intercept
     public void setProperties(Properties prop) {
         String dialectType = prop.getProperty("dialectType");
         String dialectClazz = prop.getProperty("dialectClazz");
+        String localPage = prop.getProperty("localPage");
 
         if (StringUtils.isNotEmpty(dialectType)) {
             this.dialectType = dialectType;
@@ -184,6 +185,9 @@ public class PaginationInterceptor extends SqlParserHandler implements Intercept
         if (StringUtils.isNotEmpty(dialectClazz)) {
             this.dialectClazz = dialectClazz;
         }
+        if (StringUtils.isNotEmpty(localPage)) {
+            this.localPage = Boolean.valueOf(localPage);
+        }
     }
 
     public PaginationInterceptor setDialectType(String dialectType) {

+ 29 - 0
src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java

@@ -43,6 +43,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 import org.apache.ibatis.transaction.TransactionFactory;
 import org.apache.ibatis.type.TypeHandler;
+import org.apache.ibatis.type.TypeHandlerRegistry;
 import org.mybatis.spring.SqlSessionFactoryBean;
 import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
 import org.springframework.beans.factory.FactoryBean;
@@ -107,6 +108,9 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 
     private String typeAliasesPackage;
 
+    // TODO 自定义枚举包
+    private String typeEnumsPackage;
+
     private Class<?> typeAliasesSuperType;
 
     //issue #19. No default provider.
@@ -204,6 +208,10 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
         this.typeAliasesPackage = typeAliasesPackage;
     }
 
+    public void setTypeEnumsPackage(String typeEnumsPackage) {
+        this.typeEnumsPackage = typeEnumsPackage;
+    }
+
     /**
      * Super class which domain objects have to extend to have a type alias created.
      * No effect if there is no package to scan configured.
@@ -445,6 +453,27 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
             }
         }
 
+        // TODO 自定义枚举包扫描处理
+        if (hasLength(this.typeEnumsPackage)) {
+            // 取得类型转换注册器
+            String[] typeEnumsPackageArray;
+            if (typeEnumsPackage.contains("*") && !typeEnumsPackage.contains(",")
+                    && !typeEnumsPackage.contains(";")) {
+                typeEnumsPackageArray = PackageHelper.convertTypeAliasesPackage(typeEnumsPackage);
+            } else {
+                typeEnumsPackageArray = tokenizeToStringArray(this.typeEnumsPackage,
+                        ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
+            }
+            if (typeEnumsPackageArray == null) {
+                throw new MybatisPlusException("not find typeEnumsPackage:" + typeEnumsPackage);
+            }
+            TypeHandlerRegistry typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
+            for (String packageToScan : typeEnumsPackageArray) {
+                // 注册 TODO
+//                typeHandlerRegistry.register(className, "com.baomidou.mybatisplus.enums.IEnumTypeHandler");
+            }
+        }
+
         if (!isEmpty(this.typeAliases)) {
             for (Class<?> typeAlias : this.typeAliases) {
                 configuration.getTypeAliasRegistry().registerAlias(typeAlias);

+ 53 - 0
src/main/java/com/baomidou/mybatisplus/toolkit/EnumUtils.java

@@ -0,0 +1,53 @@
+/**
+ * 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.toolkit;
+
+import java.io.Serializable;
+
+import com.baomidou.mybatisplus.enums.IEnum;
+
+/**
+ * <p>
+ * 枚举处理工具类
+ * </p>
+ *
+ * @author hubin
+ * @Date 2017-10-11
+ */
+public class EnumUtils {
+
+    /**
+     * <p>
+     * 值映射为枚举
+     * </p>
+     *
+     * @param enumClass 枚举类
+     * @param value     枚举值
+     * @param <E>       对应枚举
+     * @return
+     */
+    public static <E extends Enum<?> & IEnum> E valueOf(Class<E> enumClass, Object value) {
+        E[] es = enumClass.getEnumConstants();
+        for (E e : es) {
+            if ((value instanceof String && e.getValue().equals(value))
+                    || e.getValue() == value) {
+                return e;
+            }
+        }
+        return null;
+    }
+
+}

+ 0 - 1
src/main/java/com/baomidou/mybatisplus/toolkit/PackageHelper.java

@@ -54,7 +54,6 @@ public class PackageHelper {
         MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
         String pkg = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                 + ClassUtils.convertClassNameToResourcePath(typeAliasesPackage) + "/*.class";
-
 		/*
          * 将加载多个绝对匹配的所有Resource
 		 * 将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分,然后进行遍历模式匹配,排除重复包路径