diff --git a/README.md b/README.md index 20ad94c2c206b1fc5f24c5002990c23bcb8240d8..5629eed31814082b6a5087b44da1622d2a0d581e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# mybatis-enhance-actable-1.0.4 +# mybatis-enhance-actable-1.0.5 该项目是从之前写过的一个框架中抽取出来的,本身是对Mybatis做的增强功能,为了能够使习惯了hibernate框架的开发者能够快速的入手Mybatis,我给他取名叫做 “A.C.Table” 本意是自动建表的意思,A.C.Table是一个基于Spring和Mybatis的Maven项目,增强了Mybatis的功能,过配置model注解的方式来创建表,修改表结构,并且实现了共通的CUDR功能提升开发效率,目前仅支持Mysql,后续可能会扩展针对其他数据库的支持。 @@ -14,22 +14,25 @@ A.C.Table是采用了Spring、Mybatis技术的Maven结构,详细介绍如下 7. 修复了model属性名与表字段名不一致时公共的查询方法查不出数据的问题。(版本1.0.2) 8. 增加了对公共的CUDR方法的优化,保存成功会返回id,query查询时可以设置参数进行分页查询(pageSize:int类型标识每页数量,currentPage:int类型标识当前第几页,start:int类型从第几条开始,orderField:string类型排序字段,sortStr:string类型排序方式(desc,asc))(版本1.0.3) 9. 增加了对Mysql的longtext和mediumtext两种字段类型的支持,公共的CUDR方法的优化,原query方法更正为search,现query方法支持动态sql查询,原orderField字段只支持单个字段的排序,现修改为orderBy字段,支持数据类型为LinkedHashMap,有序,key为字段名,value为排序方式(版本1.0.4) +10.增加对mysql数据库(timestamp/time/date/float)四种数据类型的支持(版本1.0.5) +11.增加对springboot框架的支持(版本1.0.5) - **使用步骤方法** -1. 使用该功能的项目需要依赖mybatis-enhance-actable-1.0.3.jar + **基本使用规范** +1. 需要依赖mybatis-enhance-actable-1.0.5.jar - 已上传至maven中央仓库,使用时pom文件中增加如下依赖 +``` com.gitee.sunchenbin.mybatis.actable mybatis-enhance-actable - 1.0.4 + 1.0.5 -2. 在你的web项目上创建个目录比如config下面创建个文件autoCreateTable.properties文件的内容如下: +``` - mybatis.table.auto=update +2. 需要配置对于actable支持的配置 +``` + mybatis.table.auto=update mybatis.model.pack=com.sunchenbin.store.model - mybatis.database.type=mysql 本系统提供三种模式: @@ -43,10 +46,75 @@ A.C.Table是采用了Spring、Mybatis技术的Maven结构,详细介绍如下 2.4 mybatis.model.pack这个配置是用来配置要扫描的用于创建表的对象的包名 2.5 mybatis.database.type这个是用来区别数据库的,预计会支持这四种数据库mysql/oracle/sqlserver/postgresql,但目前仅支持mysql - + +``` + +3. 支持actable的mybatis配置 + +``` + + 1. classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml + 2. com.gitee.sunchenbin.mybatis.actable.dao.* +``` + + + +4. 扫描actable的包到spring容器中管理 + +``` + 1. com.gitee.sunchenbin.mybatis.actable.manager.* +``` + + **Springboot+Mybatis的项目使用步骤方法** + +1. 首先pom文件依赖actable框架 + +``` + + com.gitee.sunchenbin.mybatis.actable + mybatis-enhance-actable + 1.0.5 + +``` + +2. 项目的application.properties文件配置例如下面 + +``` + mybatis.table.auto=update + mybatis.model.pack=com.sunchenbin.store.model + mybatis.database.type=mysql + mybatis.mapperLocations=classpath*:xxxxxx/*.xml,classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml +``` + +3. springboot启动类需要做如下配置 + +``` + 1. 通过注解@ComponentScan配置,扫描actable要注册到spring的包路径"com.gitee.sunchenbin.mybatis.actable.manager.*" + 2. 通过注解@MapperScan配置,扫描mybatis的mapper,路径为"com.gitee.sunchenbin.mybatis.actable.dao.*" +``` + + **传统Spring+Mybatis的Web项目使用步骤方法** +1. 首先pom文件依赖actable框架 + +``` + + com.gitee.sunchenbin.mybatis.actable + mybatis-enhance-actable + 1.0.5 + +``` + +2. 在你的web项目上创建个目录比如config下面创建个文件autoCreateTable.properties文件的内容如下: + +``` + mybatis.table.auto=update + mybatis.model.pack=com.sunchenbin.store.model + mybatis.database.type=mysql +``` + 3. spring的配置文件中需要做如下配置: ``` - + diff --git a/mybatis-enhance-actable/LICENSE b/mybatis-enhance-actable/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64 --- /dev/null +++ b/mybatis-enhance-actable/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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 + + 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. diff --git a/mybatis-enhance-actable/pom.xml b/mybatis-enhance-actable/pom.xml index 6c579bfac27db83804b529e548076c1a3bd961ac..74dba0680f6fa5ff87944e06267bddf09dcebfb7 100644 --- a/mybatis-enhance-actable/pom.xml +++ b/mybatis-enhance-actable/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.gitee.sunchenbin.mybatis.actable mybatis-enhance-actable - 1.0.4 + 1.0.5 org.sonatype.oss diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/constants/Constants.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/constants/Constants.java new file mode 100644 index 0000000000000000000000000000000000000000..69e42ee4aeadac61d512635ae4661d8bffb2d1e9 --- /dev/null +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/constants/Constants.java @@ -0,0 +1,15 @@ +package com.gitee.sunchenbin.mybatis.actable.constants; + +public class Constants { + + public static final String DATABASE_TYPE_KEY = "mybatis.database.type"; + public static final String MODEL_PACK_KEY = "mybatis.model.pack"; + public static final String TABLE_AUTO_KEY = "mybatis.table.auto"; + + public static final String DATABASE_TYPE_KEY_VALUE = "${mybatis.database.type:NULL}"; + public static final String MODEL_PACK_KEY_VALUE = "${mybatis.model.pack:NULL}"; + public static final String TABLE_AUTO_KEY_VALUE = "${mybatis.table.auto:NULL}"; + + public static final String NULL = "NULL"; + +} diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/constants/MySqlTypeConstant.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/constants/MySqlTypeConstant.java index cea38abb5ba3b3c4f09348a133ab13ce3719089e..00f58be527ccf62cecdadd630301aa5e779e7c05 100644 --- a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/constants/MySqlTypeConstant.java +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/constants/MySqlTypeConstant.java @@ -42,9 +42,21 @@ public class MySqlTypeConstant { @LengthCount public static final String CHAR = "char"; - /** - * 等于java中的long - */ @LengthCount public static final String BIGINT = "bigint"; + + @LengthCount(LengthCount=0) + public static final String BIT = "bit"; + + @LengthCount(LengthCount=0) + public static final String TIMESTAMP = "timestamp"; + + @LengthCount(LengthCount=0) + public static final String DATE = "date"; + + @LengthCount(LengthCount=0) + public static final String TIME = "time"; + + @LengthCount(LengthCount=0) + public static final String FLOAT = "float"; } diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManager.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManager.java index 549d3db46c8264b77c13e0ecaf021e201d0e5628..9c3524195cc9179fb816d7631d9b6f4868bc4e0c 100644 --- a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManager.java +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManager.java @@ -35,6 +35,15 @@ public interface BaseMysqlCRUDManager{ */ PageResultCommand search(T t); + /** + * 废弃,请使用search + * 根据传入对象非空的条件进行查询返回值PageResultCommand,适用于返回结果集的场合 + * @param model类型 + * @param t 要查询的model类型数据 + */ + @Deprecated + PageResultCommand query(T t); + /** * 根据传入对象非空的条件进行查询返回对象,适用于返回单个对象的场合 * @param t diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManagerImpl.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManagerImpl.java index 3869a6931942ba07e667be58dd228ed9ae3d4d41..de2078fa79bc0dbb8f42d1029a2b7697f5bcb7e4 100644 --- a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManagerImpl.java +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/common/BaseMysqlCRUDManagerImpl.java @@ -2,12 +2,12 @@ package com.gitee.sunchenbin.mybatis.actable.manager.common; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.apache.commons.lang.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -228,6 +228,11 @@ public class BaseMysqlCRUDManagerImpl implements BaseMysqlCRUDManager{ return pageResultCommand; } + @Override + public PageResultCommand query(T t) { + return search(t); + } + /** * 递归扫描父类的fields * @param clas @@ -237,7 +242,15 @@ public class BaseMysqlCRUDManagerImpl implements BaseMysqlCRUDManager{ private Field[] recursionParents(Class clas, Field[] fields) { if(clas.getSuperclass()!=null){ Class clsSup = clas.getSuperclass(); - fields = (Field[]) ArrayUtils.addAll(fields,clsSup.getDeclaredFields()); + List fieldList = new ArrayList(); + fieldList.addAll(Arrays.asList(fields)); + fieldList.addAll(Arrays.asList(clsSup.getDeclaredFields())); + fields = new Field[fieldList.size()]; + int i = 0; + for (Object field : fieldList.toArray()) { + fields[i] = (Field) field; + i++; + } fields = recursionParents(clsSup, fields); } return fields; diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/handler/StartUpHandlerImpl.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/handler/StartUpHandlerImpl.java index 8a8dbab2899d87460c6bb835fd235e0928695e9b..7056b57ea34ed1a2e6ca0aa76cf699ddfcce827e 100644 --- a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/handler/StartUpHandlerImpl.java +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/handler/StartUpHandlerImpl.java @@ -8,7 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import com.gitee.sunchenbin.mybatis.actable.constants.Constants; import com.gitee.sunchenbin.mybatis.actable.manager.system.SysMysqlCreateTableManager; +import com.gitee.sunchenbin.mybatis.actable.manager.util.ConfigurationUtil; /** * 启动时进行处理的实现类 @@ -21,6 +23,9 @@ public class StartUpHandlerImpl implements StartUpHandler { private static final Logger log = LoggerFactory.getLogger(StartUpHandlerImpl.class); + @Autowired + private ConfigurationUtil springContextUtil; + /** 数据库类型:mysql */ public static String MYSQL = "mysql"; @@ -33,17 +38,16 @@ public class StartUpHandlerImpl implements StartUpHandler { /** 数据库类型:postgresql */ public static String POSTGRESQL = "postgresql"; - /** - * 数据库类型 - */ - @Value("#{configProperties['mybatis.database.type']}") - private String databaseType = MYSQL; + /** 数据库类型 */ + private static String databaseType = null; @Autowired private SysMysqlCreateTableManager sysMysqlCreateTableManager; @PostConstruct public void startHandler() { + // 获取配置信息 + databaseType = springContextUtil.getConfig(Constants.DATABASE_TYPE_KEY) == null ? MYSQL : springContextUtil.getConfig(Constants.DATABASE_TYPE_KEY); // 执行mysql的处理方法 if (MYSQL.equals(databaseType)) { diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysMysqlCreateTableManagerImpl.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysMysqlCreateTableManagerImpl.java index bee758284b45c71de244532c9b5af32130b24e64..d7a1b8b21755f749a235197492ed5ea6e3e45910 100644 --- a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysMysqlCreateTableManagerImpl.java +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysMysqlCreateTableManagerImpl.java @@ -2,13 +2,13 @@ package com.gitee.sunchenbin.mybatis.actable.manager.system; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import org.apache.commons.lang.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -21,8 +21,10 @@ import com.gitee.sunchenbin.mybatis.actable.annotation.LengthCount; import com.gitee.sunchenbin.mybatis.actable.annotation.Table; import com.gitee.sunchenbin.mybatis.actable.command.CreateTableParam; import com.gitee.sunchenbin.mybatis.actable.command.SysMysqlColumns; +import com.gitee.sunchenbin.mybatis.actable.constants.Constants; import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant; import com.gitee.sunchenbin.mybatis.actable.dao.system.CreateMysqlTablesMapper; +import com.gitee.sunchenbin.mybatis.actable.manager.util.ConfigurationUtil; import com.gitee.sunchenbin.mybatis.actable.utils.ClassTools; /** @@ -39,23 +41,27 @@ public class SysMysqlCreateTableManagerImpl implements SysMysqlCreateTableManage @Autowired private CreateMysqlTablesMapper createMysqlTablesMapper; + + @Autowired + private ConfigurationUtil springContextUtil; /** * 要扫描的model所在的pack */ - @Value("#{configProperties['mybatis.model.pack']}") - private String pack; + private static String pack = null; /** * 自动创建模式:update表示更新,create表示删除原表重新创建 */ - @Value("#{configProperties['mybatis.table.auto']}") - private String tableAuto; - + private static String tableAuto = null; + /** * 读取配置文件的三种状态(创建表、更新表、不做任何事情) */ public void createMysqlTable() { + // 读取配置信息 + pack = springContextUtil.getConfig(Constants.MODEL_PACK_KEY); + tableAuto = springContextUtil.getConfig(Constants.TABLE_AUTO_KEY); // 不做任何事情 if ("none".equals(tableAuto)) { @@ -482,7 +488,15 @@ public class SysMysqlCreateTableManagerImpl implements SysMysqlCreateTableManage private Field[] recursionParents(Class clas, Field[] fields) { if (clas.getSuperclass() != null) { Class clsSup = clas.getSuperclass(); - fields = (Field[]) ArrayUtils.addAll(fields, clsSup.getDeclaredFields()); + List fieldList = new ArrayList(); + fieldList.addAll(Arrays.asList(fields)); + fieldList.addAll(Arrays.asList(clsSup.getDeclaredFields())); + fields = new Field[fieldList.size()]; + int i = 0; + for (Object field : fieldList.toArray()) { + fields[i] = (Field) field; + i++; + } fields = recursionParents(clsSup, fields); } return fields; diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysOracleCreateTableManagerImpl.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysOracleCreateTableManagerImpl.java index 445a8d1b8e35b29d9f6a91f99da3b89dad4c26fc..8f8596ed908339cfe84c4b1b5073028b75da628c 100644 --- a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysOracleCreateTableManagerImpl.java +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/system/SysOracleCreateTableManagerImpl.java @@ -7,7 +7,9 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.gitee.sunchenbin.mybatis.actable.constants.Constants; import com.gitee.sunchenbin.mybatis.actable.dao.system.CreateMysqlTablesMapper; +import com.gitee.sunchenbin.mybatis.actable.manager.util.ConfigurationUtil; /** * 项目启动时自动扫描配置的目录中的model,根据配置的规则自动创建或更新表 @@ -24,23 +26,27 @@ public class SysOracleCreateTableManagerImpl implements SysOracleCreateTableMana @Autowired private CreateMysqlTablesMapper createMysqlTablesMapper; + + @Autowired + private ConfigurationUtil springContextUtil; /** * 要扫描的model所在的pack */ - @Value("#{configProperties['mybatis.model.pack']}") private String pack; /** * 自动创建模式:update表示更新,create表示删除原表重新创建 */ - @Value("#{configProperties['mybatis.table.auto']}") private String tableAuto; /** * 读取配置文件的三种状态(创建表、更新表、不做任何事情) */ public void createOracleTable(){ + // 读取配置信息 + pack = springContextUtil.getConfig(Constants.MODEL_PACK_KEY); + tableAuto = springContextUtil.getConfig(Constants.TABLE_AUTO_KEY); // 不做任何事情 if("none".equals(tableAuto)){ diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/util/ConfigurationUtil.java b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/util/ConfigurationUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..f8b65cee795256bb27e85fa109cc982ce5996ce5 --- /dev/null +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/manager/util/ConfigurationUtil.java @@ -0,0 +1,116 @@ +package com.gitee.sunchenbin.mybatis.actable.manager.util; + +import java.lang.reflect.Method; +import java.util.Properties; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.PropertyResourceConfigurer; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.core.io.support.PropertiesLoaderSupport; +import org.springframework.stereotype.Component; + +import com.gitee.sunchenbin.mybatis.actable.constants.Constants; + +@Component +public class ConfigurationUtil implements ApplicationContextAware { + + private static ApplicationContext applicationContext; + + private static Properties properties = null; + + @Value(Constants.TABLE_AUTO_KEY_VALUE) + private String tableAuto; + + @Value(Constants.MODEL_PACK_KEY_VALUE) + private String modelPack; + + @Value(Constants.DATABASE_TYPE_KEY_VALUE) + private String databaseType; + + /** + * 实现ApplicationContextAware接口的回调方法,设置上下文环境 + */ + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + ConfigurationUtil.applicationContext = applicationContext; + } + + /** + * 获得spring上下文 + * + * @return ApplicationContext spring上下文 + */ + public ApplicationContext getApplicationContext() { + return applicationContext; + } + + /** + * 获取bean + * + * @param name + * service注解方式name为小驼峰格式 + * @return Object bean的实例对象 + */ + public Object getBean(String name) throws BeansException { + return applicationContext.getBean(name); + } + + /** + * 获取配置文件的值,兼容传统spring项目和springboot项目 + * + * @param key + * properties + * @return + */ + public String getConfig(String key) { + if(Constants.DATABASE_TYPE_KEY.equals(key) && !Constants.NULL.equals(databaseType)) { + return databaseType; + } + if(Constants.MODEL_PACK_KEY.equals(key) && !Constants.NULL.equals(modelPack)) { + return modelPack; + } + if(Constants.TABLE_AUTO_KEY.equals(key) && !Constants.NULL.equals(tableAuto)) { + return tableAuto; + } + if(properties == null) { + initProperties(); + } + Object object = properties.get(key); + return object == null ? null : (String)object; + } + + private void initProperties() { + + properties = new Properties(); + try { + String[] postProcessorNames = applicationContext.getBeanNamesForType(BeanFactoryPostProcessor.class, true, + true); + for (String ppName : postProcessorNames) { + BeanFactoryPostProcessor beanProcessor = applicationContext.getBean(ppName, + BeanFactoryPostProcessor.class); + if (beanProcessor instanceof PropertyResourceConfigurer) { + PropertyResourceConfigurer propertyResourceConfigurer = (PropertyResourceConfigurer) beanProcessor; + Method mergeProperties = PropertiesLoaderSupport.class.getDeclaredMethod("mergeProperties"); + mergeProperties.setAccessible(true); + Properties props = (Properties) mergeProperties.invoke(propertyResourceConfigurer); + + // get the method convertProperties + // in class PropertyResourceConfigurer + Method convertProperties = PropertyResourceConfigurer.class.getDeclaredMethod("convertProperties", + Properties.class); + // convert properties + convertProperties.setAccessible(true); + convertProperties.invoke(propertyResourceConfigurer, props); + + properties.putAll(props); + } + } + + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/mapping/system/CreateMysqlTablesMapper.xml b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/mapping/system/CreateMysqlTablesMapper.xml index 2b66e3aaaad2247daee9881881766cee65b17ede..0781e8d76cf2c7ca1b9547cbe3e77a59fc7b12e5 100644 --- a/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/mapping/system/CreateMysqlTablesMapper.xml +++ b/mybatis-enhance-actable/src/main/java/com/gitee/sunchenbin/mybatis/actable/mapping/system/CreateMysqlTablesMapper.xml @@ -2,7 +2,7 @@ - +