From bc9f706372230513c201e334e285b6e45f662e11 Mon Sep 17 00:00:00 2001 From: shikeyin <pxzsky@163.com> Date: 星期二, 16 一月 2024 17:57:02 +0800 Subject: [PATCH] 添加mybatis支持 1 --- iplatform-base-admin/src/main/java/com/iplatform/base/controller/GenController.java | 221 ++ iplatform-core/src/main/java/com/iplatform/core/util/Convert.java | 1001 ++++++++++++ iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTableColumn.java | 373 ++++ iplatform-base/pom.xml | 6 pom.xml | 65 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableMapper.java | 84 + iplatform-support-mybatis/src/test/java/com/iplatform/AppTest.java | 20 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/GenConstants.java | 117 + iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableColumnServiceImpl.java | 69 iplatform-core/src/main/java/com/iplatform/core/util/CharsetKit.java | 87 + iplatform-support-mybatis/pom.xml | 75 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableColumnService.java | 45 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/BaseEntity.java | 118 + iplatform-core/pom.xml | 5 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableServiceImpl.java | 521 ++++++ iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/IBaseService.java | 72 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityUtils.java | 419 +++++ iplatform-core/src/main/java/com/iplatform/core/util/StringUtils.java | 623 +++++++ iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/config/GenConfig.java | 73 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableColumnMapper.java | 61 iplatform-support-mybatis/src/main/java/com/iplatform/App.java | 13 iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTable.java | 372 ++++ iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableService.java | 122 + iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/GenUtils.java | 258 +++ iplatform-core/src/main/java/com/iplatform/core/util/StrFormatter.java | 103 + iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityInitializer.java | 35 26 files changed, 4,958 insertions(+), 0 deletions(-) diff --git a/iplatform-base-admin/src/main/java/com/iplatform/base/controller/GenController.java b/iplatform-base-admin/src/main/java/com/iplatform/base/controller/GenController.java new file mode 100644 index 0000000..9d59fed --- /dev/null +++ b/iplatform-base-admin/src/main/java/com/iplatform/base/controller/GenController.java @@ -0,0 +1,221 @@ +package com.iplatform.base.controller; + +import com.insurance.common.annotation.Log; +import com.insurance.common.core.domain.AjaxResult; +import com.insurance.common.core.page.TableDataInfo; +import com.insurance.common.enums.BusinessType; +import com.iplatform.base.SystemController; +import com.iplatform.core.util.Convert; +import com.iplatform.mybatis.domain.GenTable; +import com.iplatform.mybatis.domain.GenTableColumn; +import com.iplatform.mybatis.service.IGenTableColumnService; +import com.iplatform.mybatis.service.IGenTableService; +import org.apache.commons.io.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 浠g爜鐢熸垚 鎿嶄綔澶勭悊銆� + * <pre> + * 1) 鍙傝�冪涓夋柟浠g爜锛岀晫闈㈠彲浠ヨ皟鐢ㄨ鎺ュ彛鐢熸垚浠g爜銆� + * 2) 鐩墠浠呬娇鐢ㄧ敓鎴愭暟鎹簱瀹炰綋锛屼互鍙妔ervice瀹炵幇锛屽墠绔痸ue鏆備笉鐢熸垚銆� + * </pre> + * + * @author manton + * @author 鏃跺厠鑻� + * @date 2024-01-16 淇敼 + */ +//@Api(value = "浠g爜鐢熸垚绠$悊", tags = {"浠g爜鐢熸垚绠$悊"}) +@RestController +@RequestMapping("/tool/gen") +public class GenController extends SystemController +{ + @Autowired + private IGenTableService genTableService; + + @Autowired + private IGenTableColumnService genTableColumnService; + + /** + * 鏌ヨ浠g爜鐢熸垚鍒楄〃 + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:list')") + @GetMapping("/list") + public TableDataInfo genList(GenTable genTable) + { +// startPage(); + List<GenTable> list = genTableService.selectGenTableList(genTable); + return getDataTable(list); + } + + /** + * 淇敼浠g爜鐢熸垚涓氬姟 + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:query')") + @GetMapping(value = "/{tableId}") + public AjaxResult getInfo(@PathVariable Long tableId) + { + GenTable table = genTableService.selectGenTableById(tableId); + List<GenTable> tables = genTableService.selectGenTableAll(); + List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId); + Map<String, Object> map = new HashMap<String, Object>(); + map.put("info", table); + map.put("rows", list); + map.put("tables", tables); + return success(map); + } + + /** + * 鏌ヨ鏁版嵁搴撳垪琛� + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:list')") + @GetMapping("/db/list") + public TableDataInfo dataList(GenTable genTable) + { +// startPage(); + List<GenTable> list = genTableService.selectDbTableList(genTable); + return getDataTable(list); + } + + /** + * 鏌ヨ鏁版嵁琛ㄥ瓧娈靛垪琛� + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:list')") + @GetMapping(value = "/column/{tableId}") + public TableDataInfo columnList(Long tableId) + { + TableDataInfo dataInfo = new TableDataInfo(); + List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId); + dataInfo.setRows(list); + dataInfo.setTotal(list.size()); + return dataInfo; + } + + /** + * 瀵煎叆琛ㄧ粨鏋勶紙淇濆瓨锛� + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:import')") +// @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.IMPORT) + @PostMapping("/importTable") + public AjaxResult importTableSave(String tables) + { + String[] tableNames = Convert.toStrArray(tables); + // 鏌ヨ琛ㄤ俊鎭� + List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames); + genTableService.importGenTable(tableList); + return success(); + } + + /** + * 淇敼淇濆瓨浠g爜鐢熸垚涓氬姟 + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:edit')") +// @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.UPDATE) + @PutMapping + public AjaxResult editSave(@Validated @RequestBody GenTable genTable) + { + genTableService.validateEdit(genTable); + genTableService.updateGenTable(genTable); + return success(); + } + + /** + * 鍒犻櫎浠g爜鐢熸垚 + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:remove')") +// @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.DELETE) + @DeleteMapping("/{tableIds}") + public AjaxResult remove(@PathVariable Long[] tableIds) + { + genTableService.deleteGenTableByIds(tableIds); + return success(); + } + + /** + * 棰勮浠g爜 + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:preview')") + @GetMapping("/preview/{tableId}") + public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException + { + Map<String, String> dataMap = genTableService.previewCode(tableId); + return success(dataMap); + } + + /** + * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:code')") + @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.GENCODE) + @GetMapping("/download/{tableName}") + public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException + { + byte[] data = genTableService.downloadCode(tableName); + genCode(response, data); + } + + /** + * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛� + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:code')") +// @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.GENCODE) + @GetMapping("/genCode/{tableName}") + public AjaxResult genCode(@PathVariable("tableName") String tableName) + { + genTableService.generatorCode(tableName); + return success(); + } + + /** + * 鍚屾鏁版嵁搴� + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:edit')") +// @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.UPDATE) + @GetMapping("/synchDb/{tableName}") + public AjaxResult synchDb(@PathVariable("tableName") String tableName) + { + genTableService.synchDb(tableName); + return success(); + } + + /** + * 鎵归噺鐢熸垚浠g爜 + */ +// @PreAuthorize("@ss.hasPermi('tool:gen:code')") +// @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.GENCODE) + @GetMapping("/batchGenCode") + public void batchGenCode(HttpServletResponse response, String tables) throws IOException + { + String[] tableNames = Convert.toStrArray(tables); + byte[] data = genTableService.downloadCode(tableNames); + genCode(response, data); + } + + /** + * 鐢熸垚zip鏂囦欢 + */ + private void genCode(HttpServletResponse response, byte[] data) throws IOException + { + response.reset(); + response.addHeader("Access-Control-Allow-Origin", "*"); + response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); + response.setHeader("Content-Disposition", "attachment; filename=\"manton.zip\""); + response.addHeader("Content-Length", "" + data.length); + response.setContentType("application/octet-stream; charset=UTF-8"); + IOUtils.write(data, response.getOutputStream()); + } +} diff --git a/iplatform-base/pom.xml b/iplatform-base/pom.xml index d815d74..19ba529 100644 --- a/iplatform-base/pom.xml +++ b/iplatform-base/pom.xml @@ -133,6 +133,12 @@ <optional>true</optional> </dependency> + <!-- 寮曞叆mybatis鏀寔妯″潡锛�2024-01-16 --> + <dependency> + <groupId>com.iplatform</groupId> + <artifactId>iplatform-support-mybatis</artifactId> + </dependency> + </dependencies> </project> diff --git a/iplatform-core/pom.xml b/iplatform-core/pom.xml index 815cb99..cf126a5 100644 --- a/iplatform-core/pom.xml +++ b/iplatform-core/pom.xml @@ -64,6 +64,11 @@ <artifactId>bcprov-jdk15on</artifactId> </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + </dependency> + </dependencies> </project> diff --git a/iplatform-core/src/main/java/com/iplatform/core/util/CharsetKit.java b/iplatform-core/src/main/java/com/iplatform/core/util/CharsetKit.java new file mode 100644 index 0000000..325d781 --- /dev/null +++ b/iplatform-core/src/main/java/com/iplatform/core/util/CharsetKit.java @@ -0,0 +1,87 @@ +package com.iplatform.core.util; + +import com.walker.infrastructure.utils.StringUtils; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * 瀛楃闆嗗伐鍏风被 + * + * @author insurance + */ +public class CharsetKit +{ + /** ISO-8859-1 */ + public static final String ISO_8859_1 = "ISO-8859-1"; + /** UTF-8 */ + public static final String UTF_8 = "UTF-8"; + /** GBK */ + public static final String GBK = "GBK"; + + /** ISO-8859-1 */ + public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1); + /** UTF-8 */ + public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8); + /** GBK */ + public static final Charset CHARSET_GBK = Charset.forName(GBK); + + /** + * 杞崲涓篊harset瀵硅薄 + * + * @param charset 瀛楃闆嗭紝涓虹┖鍒欒繑鍥為粯璁ゅ瓧绗﹂泦 + * @return Charset + */ + public static Charset charset(String charset) + { + return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset); + } + + /** + * 杞崲瀛楃涓茬殑瀛楃闆嗙紪鐮� + * + * @param source 瀛楃涓� + * @param srcCharset 婧愬瓧绗﹂泦锛岄粯璁SO-8859-1 + * @param destCharset 鐩爣瀛楃闆嗭紝榛樿UTF-8 + * @return 杞崲鍚庣殑瀛楃闆� + */ + public static String convert(String source, String srcCharset, String destCharset) + { + return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset)); + } + + /** + * 杞崲瀛楃涓茬殑瀛楃闆嗙紪鐮� + * + * @param source 瀛楃涓� + * @param srcCharset 婧愬瓧绗﹂泦锛岄粯璁SO-8859-1 + * @param destCharset 鐩爣瀛楃闆嗭紝榛樿UTF-8 + * @return 杞崲鍚庣殑瀛楃闆� + */ + public static String convert(String source, Charset srcCharset, Charset destCharset) + { + if (null == srcCharset) + { + srcCharset = StandardCharsets.ISO_8859_1; + } + + if (null == destCharset) + { + destCharset = StandardCharsets.UTF_8; + } + + if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset)) + { + return source; + } + return new String(source.getBytes(srcCharset), destCharset); + } + + /** + * @return 绯荤粺瀛楃闆嗙紪鐮� + */ + public static String systemCharset() + { + return Charset.defaultCharset().name(); + } +} diff --git a/iplatform-core/src/main/java/com/iplatform/core/util/Convert.java b/iplatform-core/src/main/java/com/iplatform/core/util/Convert.java new file mode 100644 index 0000000..dce1b6d --- /dev/null +++ b/iplatform-core/src/main/java/com/iplatform/core/util/Convert.java @@ -0,0 +1,1001 @@ +package com.iplatform.core.util; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.text.NumberFormat; +import java.util.Set; + +import com.walker.infrastructure.utils.StringUtils; +import org.apache.commons.lang3.ArrayUtils; + +/** + * 绫诲瀷杞崲鍣� + * + * @author insurance + */ +public class Convert +{ + /** + * 杞崲涓哄瓧绗︿覆<br> + * 濡傛灉缁欏畾鐨勫�间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static String toStr(Object value, String defaultValue) + { + if (null == value) + { + return defaultValue; + } + if (value instanceof String) + { + return (String) value; + } + return value.toString(); + } + + /** + * 杞崲涓哄瓧绗︿覆<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static String toStr(Object value) + { + return toStr(value, null); + } + + /** + * 杞崲涓哄瓧绗�<br> + * 濡傛灉缁欏畾鐨勫�间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Character toChar(Object value, Character defaultValue) + { + if (null == value) + { + return defaultValue; + } + if (value instanceof Character) + { + return (Character) value; + } + + final String valueStr = toStr(value, null); + return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0); + } + + /** + * 杞崲涓哄瓧绗�<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Character toChar(Object value) + { + return toChar(value, null); + } + + /** + * 杞崲涓篵yte<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Byte toByte(Object value, Byte defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Byte) + { + return (Byte) value; + } + if (value instanceof Number) + { + return ((Number) value).byteValue(); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Byte.parseByte(valueStr); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓篵yte<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Byte toByte(Object value) + { + return toByte(value, null); + } + + /** + * 杞崲涓篠hort<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Short toShort(Object value, Short defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Short) + { + return (Short) value; + } + if (value instanceof Number) + { + return ((Number) value).shortValue(); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Short.parseShort(valueStr.trim()); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓篠hort<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Short toShort(Object value) + { + return toShort(value, null); + } + + /** + * 杞崲涓篘umber<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Number toNumber(Object value, Number defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Number) + { + return (Number) value; + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return NumberFormat.getInstance().parse(valueStr); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓篘umber<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Number toNumber(Object value) + { + return toNumber(value, null); + } + + /** + * 杞崲涓篿nt<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Integer toInt(Object value, Integer defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Integer) + { + return (Integer) value; + } + if (value instanceof Number) + { + return ((Number) value).intValue(); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Integer.parseInt(valueStr.trim()); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓篿nt<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Integer toInt(Object value) + { + return toInt(value, null); + } + + /** + * 杞崲涓篒nteger鏁扮粍<br> + * + * @param str 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Integer[] toIntArray(String str) + { + return toIntArray(",", str); + } + + /** + * 杞崲涓篖ong鏁扮粍<br> + * + * @param str 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Long[] toLongArray(String str) + { + return toLongArray(",", str); + } + + /** + * 杞崲涓篒nteger鏁扮粍<br> + * + * @param split 鍒嗛殧绗� + * @param split 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Integer[] toIntArray(String split, String str) + { + if (StringUtils.isEmpty(str)) + { + return new Integer[] {}; + } + String[] arr = str.split(split); + final Integer[] ints = new Integer[arr.length]; + for (int i = 0; i < arr.length; i++) + { + final Integer v = toInt(arr[i], 0); + ints[i] = v; + } + return ints; + } + + /** + * 杞崲涓篖ong鏁扮粍<br> + * + * @param split 鍒嗛殧绗� + * @param str 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Long[] toLongArray(String split, String str) + { + if (StringUtils.isEmpty(str)) + { + return new Long[] {}; + } + String[] arr = str.split(split); + final Long[] longs = new Long[arr.length]; + for (int i = 0; i < arr.length; i++) + { + final Long v = toLong(arr[i], null); + longs[i] = v; + } + return longs; + } + + /** + * 杞崲涓篠tring鏁扮粍<br> + * + * @param str 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static String[] toStrArray(String str) + { + return toStrArray(",", str); + } + + /** + * 杞崲涓篠tring鏁扮粍<br> + * + * @param split 鍒嗛殧绗� + * @param split 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static String[] toStrArray(String split, String str) + { + return str.split(split); + } + + /** + * 杞崲涓簂ong<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Long toLong(Object value, Long defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Long) + { + return (Long) value; + } + if (value instanceof Number) + { + return ((Number) value).longValue(); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + // 鏀寔绉戝璁℃暟娉� + return new BigDecimal(valueStr.trim()).longValue(); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓簂ong<br> + * 濡傛灉缁欏畾鐨勫�间负<code>null</code>锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Long toLong(Object value) + { + return toLong(value, null); + } + + /** + * 杞崲涓篸ouble<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Double toDouble(Object value, Double defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Double) + { + return (Double) value; + } + if (value instanceof Number) + { + return ((Number) value).doubleValue(); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + // 鏀寔绉戝璁℃暟娉� + return new BigDecimal(valueStr.trim()).doubleValue(); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓篸ouble<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Double toDouble(Object value) + { + return toDouble(value, null); + } + + /** + * 杞崲涓篎loat<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Float toFloat(Object value, Float defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Float) + { + return (Float) value; + } + if (value instanceof Number) + { + return ((Number) value).floatValue(); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Float.parseFloat(valueStr.trim()); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓篎loat<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Float toFloat(Object value) + { + return toFloat(value, null); + } + + /** + * 杞崲涓篵oolean<br> + * String鏀寔鐨勫�间负锛歵rue銆乫alse銆亂es銆乷k銆乶o锛�1,0 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static Boolean toBool(Object value, Boolean defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Boolean) + { + return (Boolean) value; + } + String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + valueStr = valueStr.trim().toLowerCase(); + switch (valueStr) + { + case "true": + case "yes": + case "ok": + case "1": + return true; + case "false": + case "no": + case "0": + return false; + default: + return defaultValue; + } + } + + /** + * 杞崲涓篵oolean<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static Boolean toBool(Object value) + { + return toBool(value, null); + } + + /** + * 杞崲涓篍num瀵硅薄<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * + * @param clazz Enum鐨凜lass + * @param value 鍊� + * @param defaultValue 榛樿鍊� + * @return Enum + */ + public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (clazz.isAssignableFrom(value.getClass())) + { + @SuppressWarnings("unchecked") + E myE = (E) value; + return myE; + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Enum.valueOf(clazz, valueStr); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓篍num瀵硅薄<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * + * @param clazz Enum鐨凜lass + * @param value 鍊� + * @return Enum + */ + public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value) + { + return toEnum(clazz, value, null); + } + + /** + * 杞崲涓築igInteger<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static BigInteger toBigInteger(Object value, BigInteger defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof BigInteger) + { + return (BigInteger) value; + } + if (value instanceof Long) + { + return BigInteger.valueOf((Long) value); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return new BigInteger(valueStr); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓築igInteger<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<code>null</code><br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static BigInteger toBigInteger(Object value) + { + return toBigInteger(value, null); + } + + /** + * 杞崲涓築igDecimal<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @param defaultValue 杞崲閿欒鏃剁殑榛樿鍊� + * @return 缁撴灉 + */ + public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof BigDecimal) + { + return (BigDecimal) value; + } + if (value instanceof Long) + { + return new BigDecimal((Long) value); + } + if (value instanceof Double) + { + return BigDecimal.valueOf((Double) value); + } + if (value instanceof Integer) + { + return new BigDecimal((Integer) value); + } + final String valueStr = toStr(value, null); + if (StringUtils.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return new BigDecimal(valueStr); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 杞崲涓築igDecimal<br> + * 濡傛灉缁欏畾鐨勫�间负绌猴紝鎴栬�呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊�<br> + * 杞崲澶辫触涓嶄細鎶ラ敊 + * + * @param value 琚浆鎹㈢殑鍊� + * @return 缁撴灉 + */ + public static BigDecimal toBigDecimal(Object value) + { + return toBigDecimal(value, null); + } + + /** + * 灏嗗璞¤浆涓哄瓧绗︿覆<br> + * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁� 2銆佸璞℃暟缁勪細璋冪敤Arrays.toString鏂规硶 + * + * @param obj 瀵硅薄 + * @return 瀛楃涓� + */ + public static String utf8Str(Object obj) + { + return str(obj, CharsetKit.CHARSET_UTF_8); + } + + /** + * 灏嗗璞¤浆涓哄瓧绗︿覆<br> + * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁� 2銆佸璞℃暟缁勪細璋冪敤Arrays.toString鏂规硶 + * + * @param obj 瀵硅薄 + * @param charsetName 瀛楃闆� + * @return 瀛楃涓� + */ + public static String str(Object obj, String charsetName) + { + return str(obj, Charset.forName(charsetName)); + } + + /** + * 灏嗗璞¤浆涓哄瓧绗︿覆<br> + * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁� 2銆佸璞℃暟缁勪細璋冪敤Arrays.toString鏂规硶 + * + * @param obj 瀵硅薄 + * @param charset 瀛楃闆� + * @return 瀛楃涓� + */ + public static String str(Object obj, Charset charset) + { + if (null == obj) + { + return null; + } + + if (obj instanceof String) + { + return (String) obj; + } + else if (obj instanceof byte[]) + { + return str((byte[]) obj, charset); + } + else if (obj instanceof Byte[]) + { + byte[] bytes = ArrayUtils.toPrimitive((Byte[]) obj); + return str(bytes, charset); + } + else if (obj instanceof ByteBuffer) + { + return str((ByteBuffer) obj, charset); + } + return obj.toString(); + } + + /** + * 灏哹yte鏁扮粍杞负瀛楃涓� + * + * @param bytes byte鏁扮粍 + * @param charset 瀛楃闆� + * @return 瀛楃涓� + */ + public static String str(byte[] bytes, String charset) + { + return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset)); + } + + /** + * 瑙g爜瀛楄妭鐮� + * + * @param data 瀛楃涓� + * @param charset 瀛楃闆嗭紝濡傛灉姝ゅ瓧娈典负绌猴紝鍒欒В鐮佺殑缁撴灉鍙栧喅浜庡钩鍙� + * @return 瑙g爜鍚庣殑瀛楃涓� + */ + public static String str(byte[] data, Charset charset) + { + if (data == null) + { + return null; + } + + if (null == charset) + { + return new String(data); + } + return new String(data, charset); + } + + /** + * 灏嗙紪鐮佺殑byteBuffer鏁版嵁杞崲涓哄瓧绗︿覆 + * + * @param data 鏁版嵁 + * @param charset 瀛楃闆嗭紝濡傛灉涓虹┖浣跨敤褰撳墠绯荤粺瀛楃闆� + * @return 瀛楃涓� + */ + public static String str(ByteBuffer data, String charset) + { + if (data == null) + { + return null; + } + + return str(data, Charset.forName(charset)); + } + + /** + * 灏嗙紪鐮佺殑byteBuffer鏁版嵁杞崲涓哄瓧绗︿覆 + * + * @param data 鏁版嵁 + * @param charset 瀛楃闆嗭紝濡傛灉涓虹┖浣跨敤褰撳墠绯荤粺瀛楃闆� + * @return 瀛楃涓� + */ + public static String str(ByteBuffer data, Charset charset) + { + if (null == charset) + { + charset = Charset.defaultCharset(); + } + return charset.decode(data).toString(); + } + + // ----------------------------------------------------------------------- 鍏ㄨ鍗婅杞崲 + /** + * 鍗婅杞叏瑙� + * + * @param input String. + * @return 鍏ㄨ瀛楃涓�. + */ + public static String toSBC(String input) + { + return toSBC(input, null); + } + + /** + * 鍗婅杞叏瑙� + * + * @param input String + * @param notConvertSet 涓嶆浛鎹㈢殑瀛楃闆嗗悎 + * @return 鍏ㄨ瀛楃涓�. + */ + public static String toSBC(String input, Set<Character> notConvertSet) + { + char[] c = input.toCharArray(); + for (int i = 0; i < c.length; i++) + { + if (null != notConvertSet && notConvertSet.contains(c[i])) + { + // 璺宠繃涓嶆浛鎹㈢殑瀛楃 + continue; + } + + if (c[i] == ' ') + { + c[i] = '\u3000'; + } + else if (c[i] < '\177') + { + c[i] = (char) (c[i] + 65248); + + } + } + return new String(c); + } + + /** + * 鍏ㄨ杞崐瑙� + * + * @param input String. + * @return 鍗婅瀛楃涓� + */ + public static String toDBC(String input) + { + return toDBC(input, null); + } + + /** + * 鏇挎崲鍏ㄨ涓哄崐瑙� + * + * @param text 鏂囨湰 + * @param notConvertSet 涓嶆浛鎹㈢殑瀛楃闆嗗悎 + * @return 鏇挎崲鍚庣殑瀛楃 + */ + public static String toDBC(String text, Set<Character> notConvertSet) + { + char[] c = text.toCharArray(); + for (int i = 0; i < c.length; i++) + { + if (null != notConvertSet && notConvertSet.contains(c[i])) + { + // 璺宠繃涓嶆浛鎹㈢殑瀛楃 + continue; + } + + if (c[i] == '\u3000') + { + c[i] = ' '; + } + else if (c[i] > '\uFF00' && c[i] < '\uFF5F') + { + c[i] = (char) (c[i] - 65248); + } + } + String returnString = new String(c); + + return returnString; + } + + /** + * 鏁板瓧閲戦澶у啓杞崲 鍏堝啓涓畬鏁寸殑鐒跺悗灏嗗闆舵嬀鏇挎崲鎴愰浂 + * + * @param n 鏁板瓧 + * @return 涓枃澶у啓鏁板瓧 + */ + public static String digitUppercase(double n) + { + String[] fraction = { "瑙�", "鍒�" }; + String[] digit = { "闆�", "澹�", "璐�", "鍙�", "鑲�", "浼�", "闄�", "鏌�", "鎹�", "鐜�" }; + String[][] unit = { { "鍏�", "涓�", "浜�" }, { "", "鎷�", "浣�", "浠�" } }; + + String head = n < 0 ? "璐�" : ""; + n = Math.abs(n); + + String s = ""; + for (int i = 0; i < fraction.length; i++) + { + s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(闆�.)+", ""); + } + if (s.length() < 1) + { + s = "鏁�"; + } + int integerPart = (int) Math.floor(n); + + for (int i = 0; i < unit[0].length && integerPart > 0; i++) + { + String p = ""; + for (int j = 0; j < unit[1].length && n > 0; j++) + { + p = digit[integerPart % 10] + unit[1][j] + p; + integerPart = integerPart / 10; + } + s = p.replaceAll("(闆�.)*闆�$", "").replaceAll("^$", "闆�") + unit[0][i] + s; + } + return head + s.replaceAll("(闆�.)*闆跺厓", "鍏�").replaceFirst("(闆�.)+", "").replaceAll("(闆�.)+", "闆�").replaceAll("^鏁�$", "闆跺厓鏁�"); + } +} diff --git a/iplatform-core/src/main/java/com/iplatform/core/util/StrFormatter.java b/iplatform-core/src/main/java/com/iplatform/core/util/StrFormatter.java new file mode 100644 index 0000000..d72648b --- /dev/null +++ b/iplatform-core/src/main/java/com/iplatform/core/util/StrFormatter.java @@ -0,0 +1,103 @@ +package com.iplatform.core.util; + +import com.walker.infrastructure.utils.StringUtils; + +/** + * 瀛楃涓叉牸寮忓寲 + * + * @author insurance + */ +public class StrFormatter +{ + public static final String EMPTY_JSON = "{}"; + public static final char C_BACKSLASH = '\\'; + public static final char C_DELIM_START = '{'; + public static final char C_DELIM_END = '}'; + + /** + * * 鍒ゆ柇涓�涓璞℃暟缁勬槸鍚︿负绌� + * + * @param objects 瑕佸垽鏂殑瀵硅薄鏁扮粍 + ** @return true锛氫负绌� false锛氶潪绌� + */ + public static boolean isEmpty(Object[] objects) + { + return objects == null || (objects.length == 0); + } + + /** + * 鏍煎紡鍖栧瓧绗︿覆<br> + * 姝ゆ柟娉曞彧鏄畝鍗曞皢鍗犱綅绗� {} 鎸夌収椤哄簭鏇挎崲涓哄弬鏁�<br> + * 濡傛灉鎯宠緭鍑� {} 浣跨敤 \\杞箟 { 鍗冲彲锛屽鏋滄兂杈撳嚭 {} 涔嬪墠鐨� \ 浣跨敤鍙岃浆涔夌 \\\\ 鍗冲彲<br> + * 渚嬶細<br> + * 閫氬父浣跨敤锛歠ormat("this is {} for {}", "a", "b") -> this is a for b<br> + * 杞箟{}锛� format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> + * 杞箟\锛� format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> + * + * @param strPattern 瀛楃涓叉ā鏉� + * @param argArray 鍙傛暟鍒楄〃 + * @return 缁撴灉 + */ + public static String format(final String strPattern, final Object... argArray) + { + if (StringUtils.isEmpty(strPattern) || isEmpty(argArray)) + { + return strPattern; + } + final int strPatternLength = strPattern.length(); + + // 鍒濆鍖栧畾涔夊ソ鐨勯暱搴︿互鑾峰緱鏇村ソ鐨勬�ц兘 + StringBuilder sbuf = new StringBuilder(strPatternLength + 50); + + int handledPosition = 0; + int delimIndex;// 鍗犱綅绗︽墍鍦ㄤ綅缃� + for (int argIndex = 0; argIndex < argArray.length; argIndex++) + { + delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition); + if (delimIndex == -1) + { + if (handledPosition == 0) + { + return strPattern; + } + else + { // 瀛楃涓叉ā鏉垮墿浣欓儴鍒嗕笉鍐嶅寘鍚崰浣嶇锛屽姞鍏ュ墿浣欓儴鍒嗗悗杩斿洖缁撴灉 + sbuf.append(strPattern, handledPosition, strPatternLength); + return sbuf.toString(); + } + } + else + { + if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) + { + if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) + { + // 杞箟绗︿箣鍓嶈繕鏈変竴涓浆涔夌锛屽崰浣嶇渚濇棫鏈夋晥 + sbuf.append(strPattern, handledPosition, delimIndex - 1); + sbuf.append(Convert.utf8Str(argArray[argIndex])); + handledPosition = delimIndex + 2; + } + else + { + // 鍗犱綅绗﹁杞箟 + argIndex--; + sbuf.append(strPattern, handledPosition, delimIndex - 1); + sbuf.append(C_DELIM_START); + handledPosition = delimIndex + 1; + } + } + else + { + // 姝e父鍗犱綅绗� + sbuf.append(strPattern, handledPosition, delimIndex); + sbuf.append(Convert.utf8Str(argArray[argIndex])); + handledPosition = delimIndex + 2; + } + } + } + // 鍔犲叆鏈�鍚庝竴涓崰浣嶇鍚庢墍鏈夌殑瀛楃 + sbuf.append(strPattern, handledPosition, strPattern.length()); + + return sbuf.toString(); + } +} diff --git a/iplatform-core/src/main/java/com/iplatform/core/util/StringUtils.java b/iplatform-core/src/main/java/com/iplatform/core/util/StringUtils.java new file mode 100644 index 0000000..5fd87a7 --- /dev/null +++ b/iplatform-core/src/main/java/com/iplatform/core/util/StringUtils.java @@ -0,0 +1,623 @@ +package com.iplatform.core.util; + +import org.springframework.util.AntPathMatcher; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * 瀛楃涓插伐鍏风被 + * + * @author insurance + */ +public class StringUtils extends org.apache.commons.lang3.StringUtils +{ + /** 绌哄瓧绗︿覆 */ + private static final String NULLSTR = ""; + + /** 涓嬪垝绾� */ + private static final char SEPARATOR = '_'; + + /** + * 鑾峰彇鍙傛暟涓嶄负绌哄�� + * + * @param value defaultValue 瑕佸垽鏂殑value + * @return value 杩斿洖鍊� + */ + public static <T> T nvl(T value, T defaultValue) + { + return value != null ? value : defaultValue; + } + + /** + * * 鍒ゆ柇涓�涓狢ollection鏄惁涓虹┖锛� 鍖呭惈List锛孲et锛孮ueue + * + * @param coll 瑕佸垽鏂殑Collection + * @return true锛氫负绌� false锛氶潪绌� + */ + public static boolean isEmpty(Collection<?> coll) + { + return isNull(coll) || coll.isEmpty(); + } + + /** + * * 鍒ゆ柇涓�涓狢ollection鏄惁闈炵┖锛屽寘鍚獿ist锛孲et锛孮ueue + * + * @param coll 瑕佸垽鏂殑Collection + * @return true锛氶潪绌� false锛氱┖ + */ + public static boolean isNotEmpty(Collection<?> coll) + { + return !isEmpty(coll); + } + + /** + * * 鍒ゆ柇涓�涓璞℃暟缁勬槸鍚︿负绌� + * + * @param objects 瑕佸垽鏂殑瀵硅薄鏁扮粍 + ** @return true锛氫负绌� false锛氶潪绌� + */ + public static boolean isEmpty(Object[] objects) + { + return isNull(objects) || (objects.length == 0); + } + + /** + * * 鍒ゆ柇涓�涓璞℃暟缁勬槸鍚﹂潪绌� + * + * @param objects 瑕佸垽鏂殑瀵硅薄鏁扮粍 + * @return true锛氶潪绌� false锛氱┖ + */ + public static boolean isNotEmpty(Object[] objects) + { + return !isEmpty(objects); + } + + /** + * * 鍒ゆ柇涓�涓狹ap鏄惁涓虹┖ + * + * @param map 瑕佸垽鏂殑Map + * @return true锛氫负绌� false锛氶潪绌� + */ + public static boolean isEmpty(Map<?, ?> map) + { + return isNull(map) || map.isEmpty(); + } + + /** + * * 鍒ゆ柇涓�涓狹ap鏄惁涓虹┖ + * + * @param map 瑕佸垽鏂殑Map + * @return true锛氶潪绌� false锛氱┖ + */ + public static boolean isNotEmpty(Map<?, ?> map) + { + return !isEmpty(map); + } + + /** + * * 鍒ゆ柇涓�涓瓧绗︿覆鏄惁涓虹┖涓� + * + * @param str String + * @return true锛氫负绌� false锛氶潪绌� + */ + public static boolean isEmpty(String str) + { + return isNull(str) || NULLSTR.equals(str.trim()); + } + + /** + * * 鍒ゆ柇涓�涓瓧绗︿覆鏄惁涓洪潪绌轰覆 + * + * @param str String + * @return true锛氶潪绌轰覆 false锛氱┖涓� + */ + public static boolean isNotEmpty(String str) + { + return !isEmpty(str); + } + + /** + * * 鍒ゆ柇涓�涓璞℃槸鍚︿负绌� + * + * @param object Object + * @return true锛氫负绌� false锛氶潪绌� + */ + public static boolean isNull(Object object) + { + return object == null; + } + + /** + * * 鍒ゆ柇涓�涓璞℃槸鍚﹂潪绌� + * + * @param object Object + * @return true锛氶潪绌� false锛氱┖ + */ + public static boolean isNotNull(Object object) + { + return !isNull(object); + } + + /** + * * 鍒ゆ柇涓�涓璞℃槸鍚︽槸鏁扮粍绫诲瀷锛圝ava鍩烘湰鍨嬪埆鐨勬暟缁勶級 + * + * @param object 瀵硅薄 + * @return true锛氭槸鏁扮粍 false锛氫笉鏄暟缁� + */ + public static boolean isArray(Object object) + { + return isNotNull(object) && object.getClass().isArray(); + } + + /** + * 鍘荤┖鏍� + */ + public static String trim(String str) + { + return (str == null ? "" : str.trim()); + } + + /** + * 鎴彇瀛楃涓� + * + * @param str 瀛楃涓� + * @param start 寮�濮� + * @return 缁撴灉 + */ + public static String substring(final String str, int start) + { + if (str == null) + { + return NULLSTR; + } + + if (start < 0) + { + start = str.length() + start; + } + + if (start < 0) + { + start = 0; + } + if (start > str.length()) + { + return NULLSTR; + } + + return str.substring(start); + } + + /** + * 鎴彇瀛楃涓� + * + * @param str 瀛楃涓� + * @param start 寮�濮� + * @param end 缁撴潫 + * @return 缁撴灉 + */ + public static String substring(final String str, int start, int end) + { + if (str == null) + { + return NULLSTR; + } + + if (end < 0) + { + end = str.length() + end; + } + if (start < 0) + { + start = str.length() + start; + } + + if (end > str.length()) + { + end = str.length(); + } + + if (start > end) + { + return NULLSTR; + } + + if (start < 0) + { + start = 0; + } + if (end < 0) + { + end = 0; + } + + return str.substring(start, end); + } + + /** + * 鏍煎紡鍖栨枃鏈�, {} 琛ㄧず鍗犱綅绗�<br> + * 姝ゆ柟娉曞彧鏄畝鍗曞皢鍗犱綅绗� {} 鎸夌収椤哄簭鏇挎崲涓哄弬鏁�<br> + * 濡傛灉鎯宠緭鍑� {} 浣跨敤 \\杞箟 { 鍗冲彲锛屽鏋滄兂杈撳嚭 {} 涔嬪墠鐨� \ 浣跨敤鍙岃浆涔夌 \\\\ 鍗冲彲<br> + * 渚嬶細<br> + * 閫氬父浣跨敤锛歠ormat("this is {} for {}", "a", "b") -> this is a for b<br> + * 杞箟{}锛� format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> + * 杞箟\锛� format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> + * + * @param template 鏂囨湰妯℃澘锛岃鏇挎崲鐨勯儴鍒嗙敤 {} 琛ㄧず + * @param params 鍙傛暟鍊� + * @return 鏍煎紡鍖栧悗鐨勬枃鏈� + */ + public static String format(String template, Object... params) + { + if (isEmpty(params) || isEmpty(template)) + { + return template; + } + return StrFormatter.format(template, params); + } + + /** + * 鏄惁涓篽ttp(s)://寮�澶� + * + * @param link 閾炬帴 + * @return 缁撴灉 + */ + public static boolean ishttp(String link) + { + return StringUtils.startsWithAny(link, HTTP, HTTPS); + } + + /** + * http璇锋眰 + */ + public static final String HTTP = "http://"; + + /** + * https璇锋眰 + */ + public static final String HTTPS = "https://"; + + /** + * 瀛楃涓茶浆set + * + * @param str 瀛楃涓� + * @param sep 鍒嗛殧绗� + * @return set闆嗗悎 + */ + public static final Set<String> str2Set(String str, String sep) + { + return new HashSet<String>(str2List(str, sep, true, false)); + } + + /** + * 瀛楃涓茶浆list + * + * @param str 瀛楃涓� + * @param sep 鍒嗛殧绗� + * @param filterBlank 杩囨护绾┖鐧� + * @param trim 鍘绘帀棣栧熬绌虹櫧 + * @return list闆嗗悎 + */ + public static final List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) + { + List<String> list = new ArrayList<String>(); + if (StringUtils.isEmpty(str)) + { + return list; + } + + // 杩囨护绌虹櫧瀛楃涓� + if (filterBlank && StringUtils.isBlank(str)) + { + return list; + } + String[] split = str.split(sep); + for (String string : split) + { + if (filterBlank && StringUtils.isBlank(string)) + { + continue; + } + if (trim) + { + string = string.trim(); + } + list.add(string); + } + + return list; + } + + /** + * 鍒ゆ柇缁欏畾鐨剆et鍒楄〃涓槸鍚﹀寘鍚暟缁刟rray 鍒ゆ柇缁欏畾鐨勬暟缁刟rray涓槸鍚﹀寘鍚粰瀹氱殑鍏冪礌value + * + * @param collection 缁欏畾鐨勯泦鍚� + * @param array 缁欏畾鐨勬暟缁� + * @return boolean 缁撴灉 + */ + public static boolean containsAny(Collection<String> collection, String... array) + { + if (isEmpty(collection) || isEmpty(array)) + { + return false; + } + else + { + for (String str : array) + { + if (collection.contains(str)) + { + return true; + } + } + return false; + } + } + + /** + * 鏌ユ壘鎸囧畾瀛楃涓叉槸鍚﹀寘鍚寚瀹氬瓧绗︿覆鍒楄〃涓殑浠绘剰涓�涓瓧绗︿覆鍚屾椂涓插拷鐣ュぇ灏忓啓 + * + * @param cs 鎸囧畾瀛楃涓� + * @param searchCharSequences 闇�瑕佹鏌ョ殑瀛楃涓叉暟缁� + * @return 鏄惁鍖呭惈浠绘剰涓�涓瓧绗︿覆 + */ + public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences) + { + if (isEmpty(cs) || isEmpty(searchCharSequences)) + { + return false; + } + for (CharSequence testStr : searchCharSequences) + { + if (containsIgnoreCase(cs, testStr)) + { + return true; + } + } + return false; + } + + /** + * 椹煎嘲杞笅鍒掔嚎鍛藉悕 + */ + public static String toUnderScoreCase(String str) + { + if (str == null) + { + return null; + } + StringBuilder sb = new StringBuilder(); + // 鍓嶇疆瀛楃鏄惁澶у啓 + boolean preCharIsUpperCase = true; + // 褰撳墠瀛楃鏄惁澶у啓 + boolean curreCharIsUpperCase = true; + // 涓嬩竴瀛楃鏄惁澶у啓 + boolean nexteCharIsUpperCase = true; + for (int i = 0; i < str.length(); i++) + { + char c = str.charAt(i); + if (i > 0) + { + preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1)); + } + else + { + preCharIsUpperCase = false; + } + + curreCharIsUpperCase = Character.isUpperCase(c); + + if (i < (str.length() - 1)) + { + nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1)); + } + + if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) + { + sb.append(SEPARATOR); + } + else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) + { + sb.append(SEPARATOR); + } + sb.append(Character.toLowerCase(c)); + } + + return sb.toString(); + } + + /** + * 鏄惁鍖呭惈瀛楃涓� + * + * @param str 楠岃瘉瀛楃涓� + * @param strs 瀛楃涓茬粍 + * @return 鍖呭惈杩斿洖true + */ + public static boolean inStringIgnoreCase(String str, String... strs) + { + if (str != null && strs != null) + { + for (String s : strs) + { + if (str.equalsIgnoreCase(trim(s))) + { + return true; + } + } + } + return false; + } + + /** + * 灏嗕笅鍒掔嚎澶у啓鏂瑰紡鍛藉悕鐨勫瓧绗︿覆杞崲涓洪┘宄板紡銆傚鏋滆浆鎹㈠墠鐨勪笅鍒掔嚎澶у啓鏂瑰紡鍛藉悕鐨勫瓧绗︿覆涓虹┖锛屽垯杩斿洖绌哄瓧绗︿覆銆� 渚嬪锛欻ELLO_WORLD->HelloWorld + * + * @param name 杞崲鍓嶇殑涓嬪垝绾垮ぇ鍐欐柟寮忓懡鍚嶇殑瀛楃涓� + * @return 杞崲鍚庣殑椹煎嘲寮忓懡鍚嶇殑瀛楃涓� + */ + public static String convertToCamelCase(String name) + { + StringBuilder result = new StringBuilder(); + // 蹇�熸鏌� + if (name == null || name.isEmpty()) + { + // 娌″繀瑕佽浆鎹� + return ""; + } + else if (!name.contains("_")) + { + // 涓嶅惈涓嬪垝绾匡紝浠呭皢棣栧瓧姣嶅ぇ鍐� + return name.substring(0, 1).toUpperCase() + name.substring(1); + } + // 鐢ㄤ笅鍒掔嚎灏嗗師濮嬪瓧绗︿覆鍒嗗壊 + String[] camels = name.split("_"); + for (String camel : camels) + { + // 璺宠繃鍘熷瀛楃涓蹭腑寮�澶淬�佺粨灏剧殑涓嬫崲绾挎垨鍙岄噸涓嬪垝绾� + if (camel.isEmpty()) + { + continue; + } + // 棣栧瓧姣嶅ぇ鍐� + result.append(camel.substring(0, 1).toUpperCase()); + result.append(camel.substring(1).toLowerCase()); + } + return result.toString(); + } + + /** + * 椹煎嘲寮忓懡鍚嶆硶 + * 渚嬪锛歶ser_name->userName + */ + public static String toCamelCase(String s) + { + if (s == null) + { + return null; + } + if (s.indexOf(SEPARATOR) == -1) + { + return s; + } + s = s.toLowerCase(); + StringBuilder sb = new StringBuilder(s.length()); + boolean upperCase = false; + for (int i = 0; i < s.length(); i++) + { + char c = s.charAt(i); + + if (c == SEPARATOR) + { + upperCase = true; + } + else if (upperCase) + { + sb.append(Character.toUpperCase(c)); + upperCase = false; + } + else + { + sb.append(c); + } + } + return sb.toString(); + } + + /** + * 鏌ユ壘鎸囧畾瀛楃涓叉槸鍚﹀尮閰嶆寚瀹氬瓧绗︿覆鍒楄〃涓殑浠绘剰涓�涓瓧绗︿覆 + * + * @param str 鎸囧畾瀛楃涓� + * @param strs 闇�瑕佹鏌ョ殑瀛楃涓叉暟缁� + * @return 鏄惁鍖归厤 + */ + public static boolean matches(String str, List<String> strs) + { + if (isEmpty(str) || isEmpty(strs)) + { + return false; + } + for (String pattern : strs) + { + if (isMatch(pattern, str)) + { + return true; + } + } + return false; + } + + /** + * 鍒ゆ柇url鏄惁涓庤鍒欓厤缃�: + * ? 琛ㄧず鍗曚釜瀛楃; + * * 琛ㄧず涓�灞傝矾寰勫唴鐨勪换鎰忓瓧绗︿覆锛屼笉鍙法灞傜骇; + * ** 琛ㄧず浠绘剰灞傝矾寰�; + * + * @param pattern 鍖归厤瑙勫垯 + * @param url 闇�瑕佸尮閰嶇殑url + * @return + */ + public static boolean isMatch(String pattern, String url) + { + AntPathMatcher matcher = new AntPathMatcher(); + return matcher.match(pattern, url); + } + + @SuppressWarnings("unchecked") + public static <T> T cast(Object obj) + { + return (T) obj; + } + + /** + * 鏁板瓧宸﹁竟琛ラ綈0锛屼娇涔嬭揪鍒版寚瀹氶暱搴︺�傛敞鎰忥紝濡傛灉鏁板瓧杞崲涓哄瓧绗︿覆鍚庯紝闀垮害澶т簬size锛屽垯鍙繚鐣� 鏈�鍚巗ize涓瓧绗︺�� + * + * @param num 鏁板瓧瀵硅薄 + * @param size 瀛楃涓叉寚瀹氶暱搴� + * @return 杩斿洖鏁板瓧鐨勫瓧绗︿覆鏍煎紡锛岃瀛楃涓蹭负鎸囧畾闀垮害銆� + */ + public static final String padl(final Number num, final int size) + { + return padl(num.toString(), size, '0'); + } + + /** + * 瀛楃涓插乏琛ラ綈銆傚鏋滃師濮嬪瓧绗︿覆s闀垮害澶т簬size锛屽垯鍙繚鐣欐渶鍚巗ize涓瓧绗︺�� + * + * @param s 鍘熷瀛楃涓� + * @param size 瀛楃涓叉寚瀹氶暱搴� + * @param c 鐢ㄤ簬琛ラ綈鐨勫瓧绗� + * @return 杩斿洖鎸囧畾闀垮害鐨勫瓧绗︿覆锛岀敱鍘熷瓧绗︿覆宸﹁ˉ榻愭垨鎴彇寰楀埌銆� + */ + public static final String padl(final String s, final int size, final char c) + { + final StringBuilder sb = new StringBuilder(size); + if (s != null) + { + final int len = s.length(); + if (s.length() <= size) + { + for (int i = size - len; i > 0; i--) + { + sb.append(c); + } + sb.append(s); + } + else + { + return s.substring(len - size, len); + } + } + else + { + for (int i = size; i > 0; i--) + { + sb.append(c); + } + } + return sb.toString(); + } +} diff --git a/iplatform-support-mybatis/pom.xml b/iplatform-support-mybatis/pom.xml new file mode 100644 index 0000000..728bac1 --- /dev/null +++ b/iplatform-support-mybatis/pom.xml @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>iplatform</artifactId> + <groupId>com.iplatform</groupId> + <version>2.7.18</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>iplatform-support-mybatis</artifactId> + <name>iplatform-support-mybatis</name> + <packaging>jar</packaging> + + <properties> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <!-- mybatis-plus,2024-01-16--> + <dependency> + <groupId>com.baomidou</groupId> + <artifactId>mybatis-plus-boot-starter</artifactId> + </dependency> + + <dependency> + <groupId>javax.validation</groupId> + <artifactId>validation-api</artifactId> + </dependency> + +<!-- <dependency>--> +<!-- <groupId>org.apache.commons</groupId>--> +<!-- <artifactId>commons-lang3</artifactId>--> +<!-- </dependency>--> +<!-- <dependency>--> +<!-- <groupId>com.walkersoft</groupId>--> +<!-- <artifactId>walker-infrastructure</artifactId>--> +<!-- </dependency>--> + + <dependency> + <groupId>com.iplatform</groupId> + <artifactId>iplatform-core</artifactId> + </dependency> + + <!--velocity浠g爜鐢熸垚浣跨敤妯℃澘 --> +<!-- <dependency>--> +<!-- <groupId>org.apache.velocity</groupId>--> +<!-- <artifactId>velocity-engine-core</artifactId>--> +<!-- </dependency>--> + <!-- collections宸ュ叿绫� --> +<!-- <dependency>--> +<!-- <groupId>commons-collections</groupId>--> +<!-- <artifactId>commons-collections</artifactId>--> +<!-- </dependency>--> + + <!-- 闃块噷JSON瑙f瀽鍣� --> + <dependency> + <groupId>com.alibaba.fastjson2</groupId> + <artifactId>fastjson2</artifactId> + </dependency> + + <dependency> + <groupId>com.walkersoft</groupId> + <artifactId>walker-jdbc-generator</artifactId> + </dependency> + + </dependencies> + +</project> diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/App.java b/iplatform-support-mybatis/src/main/java/com/iplatform/App.java new file mode 100644 index 0000000..6b0d7ee --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/App.java @@ -0,0 +1,13 @@ +package com.iplatform; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/GenConstants.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/GenConstants.java new file mode 100644 index 0000000..ada8ca0 --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/GenConstants.java @@ -0,0 +1,117 @@ +package com.iplatform.mybatis; + +/** + * 浠g爜鐢熸垚閫氱敤甯搁噺 + * + * @author insurance + */ +public class GenConstants +{ + /** 鍗曡〃锛堝鍒犳敼鏌ワ級 */ + public static final String TPL_CRUD = "crud"; + + /** 鏍戣〃锛堝鍒犳敼鏌ワ級 */ + public static final String TPL_TREE = "tree"; + + /** 涓诲瓙琛紙澧炲垹鏀规煡锛� */ + public static final String TPL_SUB = "sub"; + + /** 鏍戠紪鐮佸瓧娈� */ + public static final String TREE_CODE = "treeCode"; + + /** 鏍戠埗缂栫爜瀛楁 */ + public static final String TREE_PARENT_CODE = "treeParentCode"; + + /** 鏍戝悕绉板瓧娈� */ + public static final String TREE_NAME = "treeName"; + + /** 涓婄骇鑿滃崟ID瀛楁 */ + public static final String PARENT_MENU_ID = "parentMenuId"; + + /** 涓婄骇鑿滃崟鍚嶇О瀛楁 */ + public static final String PARENT_MENU_NAME = "parentMenuName"; + + /** 鏁版嵁搴撳瓧绗︿覆绫诲瀷 */ + public static final String[] COLUMNTYPE_STR = { "char", "varchar", "nvarchar", "varchar2" }; + + /** 鏁版嵁搴撴枃鏈被鍨� */ + public static final String[] COLUMNTYPE_TEXT = { "tinytext", "text", "mediumtext", "longtext" }; + + /** 鏁版嵁搴撴椂闂寸被鍨� */ + public static final String[] COLUMNTYPE_TIME = { "datetime", "time", "date", "timestamp" }; + + /** 鏁版嵁搴撴暟瀛楃被鍨� */ + public static final String[] COLUMNTYPE_NUMBER = { "tinyint", "smallint", "mediumint", "int", "number", "integer", + "bit", "bigint", "float", "double", "decimal" }; + + /** 椤甸潰涓嶉渶瑕佺紪杈戝瓧娈� */ + public static final String[] COLUMNNAME_NOT_EDIT = { "id", "create_by", "create_time", "del_flag" }; + + /** 椤甸潰涓嶉渶瑕佹樉绀虹殑鍒楄〃瀛楁 */ + public static final String[] COLUMNNAME_NOT_LIST = { "id", "create_by", "create_time", "del_flag", "update_by", + "update_time" }; + + /** 椤甸潰涓嶉渶瑕佹煡璇㈠瓧娈� */ + public static final String[] COLUMNNAME_NOT_QUERY = { "id", "create_by", "create_time", "del_flag", "update_by", + "update_time", "remark" }; + + /** Entity鍩虹被瀛楁 */ + public static final String[] BASE_ENTITY = { "createBy", "createTime", "updateBy", "updateTime", "remark" }; + + /** Tree鍩虹被瀛楁 */ + public static final String[] TREE_ENTITY = { "parentName", "parentId", "orderNum", "ancestors", "children" }; + + /** 鏂囨湰妗� */ + public static final String HTML_INPUT = "input"; + + /** 鏂囨湰鍩� */ + public static final String HTML_TEXTAREA = "textarea"; + + /** 涓嬫媺妗� */ + public static final String HTML_SELECT = "select"; + + /** 鍗曢�夋 */ + public static final String HTML_RADIO = "radio"; + + /** 澶嶉�夋 */ + public static final String HTML_CHECKBOX = "checkbox"; + + /** 鏃ユ湡鎺т欢 */ + public static final String HTML_DATETIME = "datetime"; + + /** 鍥剧墖涓婁紶鎺т欢 */ + public static final String HTML_IMAGE_UPLOAD = "imageUpload"; + + /** 鏂囦欢涓婁紶鎺т欢 */ + public static final String HTML_FILE_UPLOAD = "fileUpload"; + + /** 瀵屾枃鏈帶浠� */ + public static final String HTML_EDITOR = "editor"; + + /** 瀛楃涓茬被鍨� */ + public static final String TYPE_STRING = "String"; + + /** 鏁村瀷 */ + public static final String TYPE_INTEGER = "Integer"; + + /** 闀挎暣鍨� */ + public static final String TYPE_LONG = "Long"; + + /** 娴偣鍨� */ + public static final String TYPE_DOUBLE = "Double"; + + /** 楂樼簿搴﹁绠楃被鍨� */ + public static final String TYPE_BIGDECIMAL = "BigDecimal"; + + /** 鏃堕棿绫诲瀷 */ + public static final String TYPE_DATE = "Date"; + + /** 妯$硦鏌ヨ */ + public static final String QUERY_LIKE = "LIKE"; + + /** 鐩哥瓑鏌ヨ */ + public static final String QUERY_EQ = "EQ"; + + /** 闇�瑕� */ + public static final String REQUIRE = "1"; +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/IBaseService.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/IBaseService.java new file mode 100644 index 0000000..1614a1a --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/IBaseService.java @@ -0,0 +1,72 @@ +package com.iplatform.mybatis; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.function.Function; +import java.util.function.Supplier; + +public interface IBaseService<T> extends IService<T> { + + default long count(SFunction<T, ?> sFunction, Object value) { + return this.count(Wrappers.<T>lambdaQuery().eq(sFunction, value)); + } + + default long count(String colunm, Object value) { + return this.count(Wrappers.<T>query().eq(colunm, value)); + } + + /** + * 鍏敤鐨� 鏁堥獙骞剁敓鎴愪富閿槸鍚﹀瓨鍦� + * + * @param primarySupplier 鐢熸垚涓婚敭鐨勫�肩殑 鍑芥暟寮忕紪绋� + * @param countFunction 鏁堥獙涓婚敭鏄惁瀛樺湪鐨� 鍑芥暟寮忕紪绋� + * <p> + * 鍙傝�冪敤娉曪細 this.primaryGenerate(() -> RandomUtil.randomNumbers(9), (primary) -> this.count(UserSign.USER_NUMBER, primary)) + * this 鍗曟寚褰撳墠 IService + * </p> + */ + default <R> R primaryGenerate(Supplier<R> primarySupplier, Function<R, Integer> countFunction) { + R primary = primarySupplier.get(); + while (countFunction.apply(primary) > 0) { + primary = primarySupplier.get(); + } + return primary; + } + + /** + * <p> + * {@linkplain #primaryGenerate(Supplier, Function)} 鐨勭簿绠�鐗� + * 鍙傝�冪敤娉曪細 this.primaryGenerate(() -> RandomUtil.randomNumbers(9),UserSign.USER_NUMBER) + * </p> + */ + default <R> R primaryGenerate(Supplier<R> primarySupplier, String colunm) { + return primaryGenerate(primarySupplier, (primary) -> (int)count(colunm, primary)); + } + + default <R> R getObj(String colunm, Object value, String getColunm, Function<Object, R> function) { + return getObj(q().eq(colunm, value).select(getColunm), function::apply); + } + + default QueryWrapper<T> q() { + return Wrappers.query(); + } + + default UpdateWrapper<T> u() { + return Wrappers.update(); + } + + default LambdaQueryWrapper<T> lq() { + return Wrappers.lambdaQuery(); + } + + default LambdaUpdateWrapper<T> lu() { + return Wrappers.lambdaUpdate(); + } + +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/config/GenConfig.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/config/GenConfig.java new file mode 100644 index 0000000..5c6363a --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/config/GenConfig.java @@ -0,0 +1,73 @@ +package com.iplatform.mybatis.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +/** + * 璇诲彇浠g爜鐢熸垚鐩稿叧閰嶇疆 + * + * @author manton + */ +@Component +@ConfigurationProperties(prefix = "gen") +@PropertySource(value = { "classpath:generator.yml" }) +public class GenConfig +{ + /** 浣滆�� */ + public static String author; + + /** 鐢熸垚鍖呰矾寰� */ + public static String packageName; + + /** 鑷姩鍘婚櫎琛ㄥ墠缂�锛岄粯璁ゆ槸false */ + public static boolean autoRemovePre; + + /** 琛ㄥ墠缂�(绫诲悕涓嶄細鍖呭惈琛ㄥ墠缂�) */ + public static String tablePrefix; + + public static String getAuthor() + { + return author; + } + + @Value("${author}") + public void setAuthor(String author) + { + GenConfig.author = author; + } + + public static String getPackageName() + { + return packageName; + } + + @Value("${packageName}") + public void setPackageName(String packageName) + { + GenConfig.packageName = packageName; + } + + public static boolean getAutoRemovePre() + { + return autoRemovePre; + } + + @Value("${autoRemovePre}") + public void setAutoRemovePre(boolean autoRemovePre) + { + GenConfig.autoRemovePre = autoRemovePre; + } + + public static String getTablePrefix() + { + return tablePrefix; + } + + @Value("${tablePrefix}") + public void setTablePrefix(String tablePrefix) + { + GenConfig.tablePrefix = tablePrefix; + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/BaseEntity.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/BaseEntity.java new file mode 100644 index 0000000..ad0595e --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/BaseEntity.java @@ -0,0 +1,118 @@ +package com.iplatform.mybatis.domain; + +import java.io.Serializable; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; + +/** + * Entity鍩虹被 + * + * @author insurance + */ +public class BaseEntity implements Serializable +{ + private static final long serialVersionUID = 1L; + + /** 鎼滅储鍊� */ + @JsonIgnore + private String searchValue; + + /** 鍒涘缓鑰� */ + private String createBy; + + /** 鍒涘缓鏃堕棿 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date createTime; + + /** 鏇存柊鑰� */ + private String updateBy; + + /** 鏇存柊鏃堕棿 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date updateTime; + + /** 澶囨敞 */ + private String remark; + + /** 璇锋眰鍙傛暟 */ + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private Map<String, Object> params; + + public String getSearchValue() + { + return searchValue; + } + + public void setSearchValue(String searchValue) + { + this.searchValue = searchValue; + } + + public String getCreateBy() + { + return createBy; + } + + public void setCreateBy(String createBy) + { + this.createBy = createBy; + } + + public Date getCreateTime() + { + return createTime; + } + + public void setCreateTime(Date createTime) + { + this.createTime = createTime; + } + + public String getUpdateBy() + { + return updateBy; + } + + public void setUpdateBy(String updateBy) + { + this.updateBy = updateBy; + } + + public Date getUpdateTime() + { + return updateTime; + } + + public void setUpdateTime(Date updateTime) + { + this.updateTime = updateTime; + } + + public String getRemark() + { + return remark; + } + + public void setRemark(String remark) + { + this.remark = remark; + } + + public Map<String, Object> getParams() + { + if (params == null) + { + params = new HashMap<>(); + } + return params; + } + + public void setParams(Map<String, Object> params) + { + this.params = params; + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTable.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTable.java new file mode 100644 index 0000000..3f217fd --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTable.java @@ -0,0 +1,372 @@ +package com.iplatform.mybatis.domain; + +import com.iplatform.core.util.StringUtils; +import com.iplatform.mybatis.GenConstants; +import org.apache.commons.lang3.ArrayUtils; + +import javax.validation.Valid; +import javax.validation.constraints.NotBlank; +import java.util.List; + +/** + * 涓氬姟琛� gen_table + * + * @author manton + */ +public class GenTable extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 缂栧彿 */ + private Long tableId; + + /** 琛ㄥ悕绉� */ + @NotBlank(message = "琛ㄥ悕绉颁笉鑳戒负绌�") + private String tableName; + + /** 琛ㄦ弿杩� */ + @NotBlank(message = "琛ㄦ弿杩颁笉鑳戒负绌�") + private String tableComment; + + /** 鍏宠仈鐖惰〃鐨勮〃鍚� */ + private String subTableName; + + /** 鏈〃鍏宠仈鐖惰〃鐨勫閿悕 */ + private String subTableFkName; + + /** 瀹炰綋绫诲悕绉�(棣栧瓧姣嶅ぇ鍐�) */ + @NotBlank(message = "瀹炰綋绫诲悕绉颁笉鑳戒负绌�") + private String className; + + /** 浣跨敤鐨勬ā鏉匡紙crud鍗曡〃鎿嶄綔 tree鏍戣〃鎿嶄綔 sub涓诲瓙琛ㄦ搷浣滐級 */ + private String tplCategory; + + /** 鐢熸垚鍖呰矾寰� */ + @NotBlank(message = "鐢熸垚鍖呰矾寰勪笉鑳戒负绌�") + private String packageName; + + /** 鐢熸垚妯″潡鍚� */ + @NotBlank(message = "鐢熸垚妯″潡鍚嶄笉鑳戒负绌�") + private String moduleName; + + /** 鐢熸垚涓氬姟鍚� */ + @NotBlank(message = "鐢熸垚涓氬姟鍚嶄笉鑳戒负绌�") + private String businessName; + + /** 鐢熸垚鍔熻兘鍚� */ + @NotBlank(message = "鐢熸垚鍔熻兘鍚嶄笉鑳戒负绌�") + private String functionName; + + /** 鐢熸垚浣滆�� */ + @NotBlank(message = "浣滆�呬笉鑳戒负绌�") + private String functionAuthor; + + /** 鐢熸垚浠g爜鏂瑰紡锛�0zip鍘嬬缉鍖� 1鑷畾涔夎矾寰勶級 */ + private String genType; + + /** 鐢熸垚璺緞锛堜笉濉粯璁ら」鐩矾寰勶級 */ + private String genPath; + + /** 涓婚敭淇℃伅 */ + private GenTableColumn pkColumn; + + /** 瀛愯〃淇℃伅 */ + private GenTable subTable; + + /** 琛ㄥ垪淇℃伅 */ + @Valid + private List<GenTableColumn> columns; + + /** 鍏跺畠鐢熸垚閫夐」 */ + private String options; + + /** 鏍戠紪鐮佸瓧娈� */ + private String treeCode; + + /** 鏍戠埗缂栫爜瀛楁 */ + private String treeParentCode; + + /** 鏍戝悕绉板瓧娈� */ + private String treeName; + + /** 涓婄骇鑿滃崟ID瀛楁 */ + private String parentMenuId; + + /** 涓婄骇鑿滃崟鍚嶇О瀛楁 */ + private String parentMenuName; + + public Long getTableId() + { + return tableId; + } + + public void setTableId(Long tableId) + { + this.tableId = tableId; + } + + public String getTableName() + { + return tableName; + } + + public void setTableName(String tableName) + { + this.tableName = tableName; + } + + public String getTableComment() + { + return tableComment; + } + + public void setTableComment(String tableComment) + { + this.tableComment = tableComment; + } + + public String getSubTableName() + { + return subTableName; + } + + public void setSubTableName(String subTableName) + { + this.subTableName = subTableName; + } + + public String getSubTableFkName() + { + return subTableFkName; + } + + public void setSubTableFkName(String subTableFkName) + { + this.subTableFkName = subTableFkName; + } + + public String getClassName() + { + return className; + } + + public void setClassName(String className) + { + this.className = className; + } + + public String getTplCategory() + { + return tplCategory; + } + + public void setTplCategory(String tplCategory) + { + this.tplCategory = tplCategory; + } + + public String getPackageName() + { + return packageName; + } + + public void setPackageName(String packageName) + { + this.packageName = packageName; + } + + public String getModuleName() + { + return moduleName; + } + + public void setModuleName(String moduleName) + { + this.moduleName = moduleName; + } + + public String getBusinessName() + { + return businessName; + } + + public void setBusinessName(String businessName) + { + this.businessName = businessName; + } + + public String getFunctionName() + { + return functionName; + } + + public void setFunctionName(String functionName) + { + this.functionName = functionName; + } + + public String getFunctionAuthor() + { + return functionAuthor; + } + + public void setFunctionAuthor(String functionAuthor) + { + this.functionAuthor = functionAuthor; + } + + public String getGenType() + { + return genType; + } + + public void setGenType(String genType) + { + this.genType = genType; + } + + public String getGenPath() + { + return genPath; + } + + public void setGenPath(String genPath) + { + this.genPath = genPath; + } + + public GenTableColumn getPkColumn() + { + return pkColumn; + } + + public void setPkColumn(GenTableColumn pkColumn) + { + this.pkColumn = pkColumn; + } + + public GenTable getSubTable() + { + return subTable; + } + + public void setSubTable(GenTable subTable) + { + this.subTable = subTable; + } + + public List<GenTableColumn> getColumns() + { + return columns; + } + + public void setColumns(List<GenTableColumn> columns) + { + this.columns = columns; + } + + public String getOptions() + { + return options; + } + + public void setOptions(String options) + { + this.options = options; + } + + public String getTreeCode() + { + return treeCode; + } + + public void setTreeCode(String treeCode) + { + this.treeCode = treeCode; + } + + public String getTreeParentCode() + { + return treeParentCode; + } + + public void setTreeParentCode(String treeParentCode) + { + this.treeParentCode = treeParentCode; + } + + public String getTreeName() + { + return treeName; + } + + public void setTreeName(String treeName) + { + this.treeName = treeName; + } + + public String getParentMenuId() + { + return parentMenuId; + } + + public void setParentMenuId(String parentMenuId) + { + this.parentMenuId = parentMenuId; + } + + public String getParentMenuName() + { + return parentMenuName; + } + + public void setParentMenuName(String parentMenuName) + { + this.parentMenuName = parentMenuName; + } + + public boolean isSub() + { + return isSub(this.tplCategory); + } + + public static boolean isSub(String tplCategory) + { + return tplCategory != null && StringUtils.equals(GenConstants.TPL_SUB, tplCategory); + } + + public boolean isTree() + { + return isTree(this.tplCategory); + } + + public static boolean isTree(String tplCategory) + { + return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory); + } + + public boolean isCrud() + { + return isCrud(this.tplCategory); + } + + public static boolean isCrud(String tplCategory) + { + return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory); + } + + public boolean isSuperColumn(String javaField) + { + return isSuperColumn(this.tplCategory, javaField); + } + + public static boolean isSuperColumn(String tplCategory, String javaField) + { + if (isTree(tplCategory)) + { + return StringUtils.equalsAnyIgnoreCase(javaField, + ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY)); + } + return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY); + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTableColumn.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTableColumn.java new file mode 100644 index 0000000..2bd851b --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/domain/GenTableColumn.java @@ -0,0 +1,373 @@ +package com.iplatform.mybatis.domain; + +import com.iplatform.core.util.StringUtils; + +import javax.validation.constraints.NotBlank; + +/** + * 浠g爜鐢熸垚涓氬姟瀛楁琛� gen_table_column + * + * @author manton + */ +public class GenTableColumn extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 缂栧彿 */ + private Long columnId; + + /** 褰掑睘琛ㄧ紪鍙� */ + private Long tableId; + + /** 鍒楀悕绉� */ + private String columnName; + + /** 鍒楁弿杩� */ + private String columnComment; + + /** 鍒楃被鍨� */ + private String columnType; + + /** JAVA绫诲瀷 */ + private String javaType; + + /** JAVA瀛楁鍚� */ + @NotBlank(message = "Java灞炴�т笉鑳戒负绌�") + private String javaField; + + /** 鏄惁涓婚敭锛�1鏄級 */ + private String isPk; + + /** 鏄惁鑷锛�1鏄級 */ + private String isIncrement; + + /** 鏄惁蹇呭~锛�1鏄級 */ + private String isRequired; + + /** 鏄惁涓烘彃鍏ュ瓧娈碉紙1鏄級 */ + private String isInsert; + + /** 鏄惁缂栬緫瀛楁锛�1鏄級 */ + private String isEdit; + + /** 鏄惁鍒楄〃瀛楁锛�1鏄級 */ + private String isList; + + /** 鏄惁鏌ヨ瀛楁锛�1鏄級 */ + private String isQuery; + + /** 鏌ヨ鏂瑰紡锛圗Q绛変簬銆丯E涓嶇瓑浜庛�丟T澶т簬銆丩T灏忎簬銆丩IKE妯$硦銆丅ETWEEN鑼冨洿锛� */ + private String queryType; + + /** 鏄剧ず绫诲瀷锛坕nput鏂囨湰妗嗐�乼extarea鏂囨湰鍩熴�乻elect涓嬫媺妗嗐�乧heckbox澶嶉�夋銆乺adio鍗曢�夋銆乨atetime鏃ユ湡鎺т欢銆乮mage鍥剧墖涓婁紶鎺т欢銆乽pload鏂囦欢涓婁紶鎺т欢銆乪ditor瀵屾枃鏈帶浠讹級 */ + private String htmlType; + + /** 瀛楀吀绫诲瀷 */ + private String dictType; + + /** 鎺掑簭 */ + private Integer sort; + + public void setColumnId(Long columnId) + { + this.columnId = columnId; + } + + public Long getColumnId() + { + return columnId; + } + + public void setTableId(Long tableId) + { + this.tableId = tableId; + } + + public Long getTableId() + { + return tableId; + } + + public void setColumnName(String columnName) + { + this.columnName = columnName; + } + + public String getColumnName() + { + return columnName; + } + + public void setColumnComment(String columnComment) + { + this.columnComment = columnComment; + } + + public String getColumnComment() + { + return columnComment; + } + + public void setColumnType(String columnType) + { + this.columnType = columnType; + } + + public String getColumnType() + { + return columnType; + } + + public void setJavaType(String javaType) + { + this.javaType = javaType; + } + + public String getJavaType() + { + return javaType; + } + + public void setJavaField(String javaField) + { + this.javaField = javaField; + } + + public String getJavaField() + { + return javaField; + } + + public String getCapJavaField() + { + return StringUtils.capitalize(javaField); + } + + public void setIsPk(String isPk) + { + this.isPk = isPk; + } + + public String getIsPk() + { + return isPk; + } + + public boolean isPk() + { + return isPk(this.isPk); + } + + public boolean isPk(String isPk) + { + return isPk != null && StringUtils.equals("1", isPk); + } + + public String getIsIncrement() + { + return isIncrement; + } + + public void setIsIncrement(String isIncrement) + { + this.isIncrement = isIncrement; + } + + public boolean isIncrement() + { + return isIncrement(this.isIncrement); + } + + public boolean isIncrement(String isIncrement) + { + return isIncrement != null && StringUtils.equals("1", isIncrement); + } + + public void setIsRequired(String isRequired) + { + this.isRequired = isRequired; + } + + public String getIsRequired() + { + return isRequired; + } + + public boolean isRequired() + { + return isRequired(this.isRequired); + } + + public boolean isRequired(String isRequired) + { + return isRequired != null && StringUtils.equals("1", isRequired); + } + + public void setIsInsert(String isInsert) + { + this.isInsert = isInsert; + } + + public String getIsInsert() + { + return isInsert; + } + + public boolean isInsert() + { + return isInsert(this.isInsert); + } + + public boolean isInsert(String isInsert) + { + return isInsert != null && StringUtils.equals("1", isInsert); + } + + public void setIsEdit(String isEdit) + { + this.isEdit = isEdit; + } + + public String getIsEdit() + { + return isEdit; + } + + public boolean isEdit() + { + return isInsert(this.isEdit); + } + + public boolean isEdit(String isEdit) + { + return isEdit != null && StringUtils.equals("1", isEdit); + } + + public void setIsList(String isList) + { + this.isList = isList; + } + + public String getIsList() + { + return isList; + } + + public boolean isList() + { + return isList(this.isList); + } + + public boolean isList(String isList) + { + return isList != null && StringUtils.equals("1", isList); + } + + public void setIsQuery(String isQuery) + { + this.isQuery = isQuery; + } + + public String getIsQuery() + { + return isQuery; + } + + public boolean isQuery() + { + return isQuery(this.isQuery); + } + + public boolean isQuery(String isQuery) + { + return isQuery != null && StringUtils.equals("1", isQuery); + } + + public void setQueryType(String queryType) + { + this.queryType = queryType; + } + + public String getQueryType() + { + return queryType; + } + + public String getHtmlType() + { + return htmlType; + } + + public void setHtmlType(String htmlType) + { + this.htmlType = htmlType; + } + + public void setDictType(String dictType) + { + this.dictType = dictType; + } + + public String getDictType() + { + return dictType; + } + + public void setSort(Integer sort) + { + this.sort = sort; + } + + public Integer getSort() + { + return sort; + } + + public boolean isSuperColumn() + { + return isSuperColumn(this.javaField); + } + + public static boolean isSuperColumn(String javaField) + { + return StringUtils.equalsAnyIgnoreCase(javaField, + // BaseEntity + "createBy", "createTime", "updateBy", "updateTime", "remark", + // TreeEntity + "parentName", "parentId", "orderNum", "ancestors"); + } + + public boolean isUsableColumn() + { + return isUsableColumn(javaField); + } + + public static boolean isUsableColumn(String javaField) + { + // isSuperColumn()涓殑鍚嶅崟鐢ㄤ簬閬垮厤鐢熸垚澶氫綑Domain灞炴�э紝鑻ユ煇浜涘睘鎬у湪鐢熸垚椤甸潰鏃堕渶瑕佺敤鍒颁笉鑳藉拷鐣ワ紝鍒欐斁鍦ㄦ澶勭櫧鍚嶅崟 + return StringUtils.equalsAnyIgnoreCase(javaField, "parentId", "orderNum", "remark"); + } + + public String readConverterExp() + { + String remarks = StringUtils.substringBetween(this.columnComment, "锛�", "锛�"); + StringBuffer sb = new StringBuffer(); + if (StringUtils.isNotEmpty(remarks)) + { + for (String value : remarks.split(" ")) + { + if (StringUtils.isNotEmpty(value)) + { + Object startStr = value.subSequence(0, 1); + String endStr = value.substring(1); + sb.append("").append(startStr).append("=").append(endStr).append(","); + } + } + return sb.deleteCharAt(sb.length() - 1).toString(); + } + else + { + return this.columnComment; + } + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableColumnMapper.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableColumnMapper.java new file mode 100644 index 0000000..4001214 --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableColumnMapper.java @@ -0,0 +1,61 @@ +package com.iplatform.mybatis.mapper; + +import com.iplatform.mybatis.domain.GenTableColumn; + +import java.util.List; + +/** + * 涓氬姟瀛楁 鏁版嵁灞� + * + * @author manton + */ +public interface GenTableColumnMapper +{ + /** + * 鏍规嵁琛ㄥ悕绉版煡璇㈠垪淇℃伅 + * + * @param tableName 琛ㄥ悕绉� + * @return 鍒椾俊鎭� + */ + public List<GenTableColumn> selectDbTableColumnsByName(String tableName); + + /** + * 鏌ヨ涓氬姟瀛楁鍒楄〃 + * + * @param tableId 涓氬姟瀛楁缂栧彿 + * @return 涓氬姟瀛楁闆嗗悎 + */ + public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId); + + /** + * 鏂板涓氬姟瀛楁 + * + * @param genTableColumn 涓氬姟瀛楁淇℃伅 + * @return 缁撴灉 + */ + public int insertGenTableColumn(GenTableColumn genTableColumn); + + /** + * 淇敼涓氬姟瀛楁 + * + * @param genTableColumn 涓氬姟瀛楁淇℃伅 + * @return 缁撴灉 + */ + public int updateGenTableColumn(GenTableColumn genTableColumn); + + /** + * 鍒犻櫎涓氬姟瀛楁 + * + * @param genTableColumns 鍒楁暟鎹� + * @return 缁撴灉 + */ + public int deleteGenTableColumns(List<GenTableColumn> genTableColumns); + + /** + * 鎵归噺鍒犻櫎涓氬姟瀛楁 + * + * @param ids 闇�瑕佸垹闄ょ殑鏁版嵁ID + * @return 缁撴灉 + */ + public int deleteGenTableColumnByIds(Long[] ids); +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableMapper.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableMapper.java new file mode 100644 index 0000000..0e4b377 --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/mapper/GenTableMapper.java @@ -0,0 +1,84 @@ +package com.iplatform.mybatis.mapper; + +import com.iplatform.mybatis.domain.GenTable; + +import java.util.List; + +/** + * 涓氬姟 鏁版嵁灞� + * + * @author manton + */ +public interface GenTableMapper +{ + /** + * 鏌ヨ涓氬姟鍒楄〃 + * + * @param genTable 涓氬姟淇℃伅 + * @return 涓氬姟闆嗗悎 + */ + public List<GenTable> selectGenTableList(GenTable genTable); + + /** + * 鏌ヨ鎹簱鍒楄〃 + * + * @param genTable 涓氬姟淇℃伅 + * @return 鏁版嵁搴撹〃闆嗗悎 + */ + public List<GenTable> selectDbTableList(GenTable genTable); + + /** + * 鏌ヨ鎹簱鍒楄〃 + * + * @param tableNames 琛ㄥ悕绉扮粍 + * @return 鏁版嵁搴撹〃闆嗗悎 + */ + public List<GenTable> selectDbTableListByNames(String[] tableNames); + + /** + * 鏌ヨ鎵�鏈夎〃淇℃伅 + * + * @return 琛ㄤ俊鎭泦鍚� + */ + public List<GenTable> selectGenTableAll(); + + /** + * 鏌ヨ琛↖D涓氬姟淇℃伅 + * + * @param id 涓氬姟ID + * @return 涓氬姟淇℃伅 + */ + public GenTable selectGenTableById(Long id); + + /** + * 鏌ヨ琛ㄥ悕绉颁笟鍔′俊鎭� + * + * @param tableName 琛ㄥ悕绉� + * @return 涓氬姟淇℃伅 + */ + public GenTable selectGenTableByName(String tableName); + + /** + * 鏂板涓氬姟 + * + * @param genTable 涓氬姟淇℃伅 + * @return 缁撴灉 + */ + public int insertGenTable(GenTable genTable); + + /** + * 淇敼涓氬姟 + * + * @param genTable 涓氬姟淇℃伅 + * @return 缁撴灉 + */ + public int updateGenTable(GenTable genTable); + + /** + * 鎵归噺鍒犻櫎涓氬姟 + * + * @param ids 闇�瑕佸垹闄ょ殑鏁版嵁ID + * @return 缁撴灉 + */ + public int deleteGenTableByIds(Long[] ids); +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableColumnServiceImpl.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableColumnServiceImpl.java new file mode 100644 index 0000000..b84c805 --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableColumnServiceImpl.java @@ -0,0 +1,69 @@ +package com.iplatform.mybatis.service; + +import com.iplatform.core.util.Convert; +import com.iplatform.mybatis.domain.GenTableColumn; +import com.iplatform.mybatis.mapper.GenTableColumnMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 涓氬姟瀛楁 鏈嶅姟灞傚疄鐜� + * + * @author manton + */ +@Service +public class GenTableColumnServiceImpl implements IGenTableColumnService +{ + @Autowired + private GenTableColumnMapper genTableColumnMapper; + + /** + * 鏌ヨ涓氬姟瀛楁鍒楄〃 + * + * @param tableId 涓氬姟瀛楁缂栧彿 + * @return 涓氬姟瀛楁闆嗗悎 + */ + @Override + public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId) + { + return genTableColumnMapper.selectGenTableColumnListByTableId(tableId); + } + + /** + * 鏂板涓氬姟瀛楁 + * + * @param genTableColumn 涓氬姟瀛楁淇℃伅 + * @return 缁撴灉 + */ + @Override + public int insertGenTableColumn(GenTableColumn genTableColumn) + { + return genTableColumnMapper.insertGenTableColumn(genTableColumn); + } + + /** + * 淇敼涓氬姟瀛楁 + * + * @param genTableColumn 涓氬姟瀛楁淇℃伅 + * @return 缁撴灉 + */ + @Override + public int updateGenTableColumn(GenTableColumn genTableColumn) + { + return genTableColumnMapper.updateGenTableColumn(genTableColumn); + } + + /** + * 鍒犻櫎涓氬姟瀛楁瀵硅薄 + * + * @param ids 闇�瑕佸垹闄ょ殑鏁版嵁ID + * @return 缁撴灉 + */ + @Override + public int deleteGenTableColumnByIds(String ids) + { + return genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids)); + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableServiceImpl.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableServiceImpl.java new file mode 100644 index 0000000..1dee90f --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/GenTableServiceImpl.java @@ -0,0 +1,521 @@ +package com.iplatform.mybatis.service; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.iplatform.core.util.CharsetKit; +import com.iplatform.core.util.StringUtils; +import com.iplatform.mybatis.GenConstants; +import com.iplatform.mybatis.domain.GenTable; +import com.iplatform.mybatis.domain.GenTableColumn; +import com.iplatform.mybatis.mapper.GenTableColumnMapper; +import com.iplatform.mybatis.mapper.GenTableMapper; +import com.iplatform.mybatis.util.GenUtils; +import com.iplatform.mybatis.util.VelocityInitializer; +import com.iplatform.mybatis.util.VelocityUtils; +import com.walker.web.WebRuntimeException; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * 涓氬姟 鏈嶅姟灞傚疄鐜� + * + * @author manton + */ +@Service +public class GenTableServiceImpl implements IGenTableService +{ + private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class); + + @Autowired + private GenTableMapper genTableMapper; + + @Autowired + private GenTableColumnMapper genTableColumnMapper; + + /** + * 鏌ヨ涓氬姟淇℃伅 + * + * @param id 涓氬姟ID + * @return 涓氬姟淇℃伅 + */ + @Override + public GenTable selectGenTableById(Long id) + { + GenTable genTable = genTableMapper.selectGenTableById(id); + setTableFromOptions(genTable); + return genTable; + } + + /** + * 鏌ヨ涓氬姟鍒楄〃 + * + * @param genTable 涓氬姟淇℃伅 + * @return 涓氬姟闆嗗悎 + */ + @Override + public List<GenTable> selectGenTableList(GenTable genTable) + { + return genTableMapper.selectGenTableList(genTable); + } + + /** + * 鏌ヨ鎹簱鍒楄〃 + * + * @param genTable 涓氬姟淇℃伅 + * @return 鏁版嵁搴撹〃闆嗗悎 + */ + @Override + public List<GenTable> selectDbTableList(GenTable genTable) + { + return genTableMapper.selectDbTableList(genTable); + } + + /** + * 鏌ヨ鎹簱鍒楄〃 + * + * @param tableNames 琛ㄥ悕绉扮粍 + * @return 鏁版嵁搴撹〃闆嗗悎 + */ + @Override + public List<GenTable> selectDbTableListByNames(String[] tableNames) + { + return genTableMapper.selectDbTableListByNames(tableNames); + } + + /** + * 鏌ヨ鎵�鏈夎〃淇℃伅 + * + * @return 琛ㄤ俊鎭泦鍚� + */ + @Override + public List<GenTable> selectGenTableAll() + { + return genTableMapper.selectGenTableAll(); + } + + /** + * 淇敼涓氬姟 + * + * @param genTable 涓氬姟淇℃伅 + * @return 缁撴灉 + */ + @Override + @Transactional + public void updateGenTable(GenTable genTable) + { + String options = JSON.toJSONString(genTable.getParams()); + genTable.setOptions(options); + int row = genTableMapper.updateGenTable(genTable); + if (row > 0) + { + for (GenTableColumn cenTableColumn : genTable.getColumns()) + { + genTableColumnMapper.updateGenTableColumn(cenTableColumn); + } + } + } + + /** + * 鍒犻櫎涓氬姟瀵硅薄 + * + * @param tableIds 闇�瑕佸垹闄ょ殑鏁版嵁ID + * @return 缁撴灉 + */ + @Override + @Transactional + public void deleteGenTableByIds(Long[] tableIds) + { + genTableMapper.deleteGenTableByIds(tableIds); + genTableColumnMapper.deleteGenTableColumnByIds(tableIds); + } + + /** + * 瀵煎叆琛ㄧ粨鏋� + * + * @param tableList 瀵煎叆琛ㄥ垪琛� + */ + @Override + @Transactional + public void importGenTable(List<GenTable> tableList) + { +// String operName = SecurityUtils.getUsername(); + String operName = "admin"; + try + { + for (GenTable table : tableList) + { + String tableName = table.getTableName(); + GenUtils.initTable(table, operName); + int row = genTableMapper.insertGenTable(table); + if (row > 0) + { + // 淇濆瓨鍒椾俊鎭� + List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); + for (GenTableColumn column : genTableColumns) + { + GenUtils.initColumnField(column, table); + genTableColumnMapper.insertGenTableColumn(column); + } + } + } + } + catch (Exception e) + { + throw new WebRuntimeException("瀵煎叆澶辫触锛�" + e.getMessage()); + } + } + + /** + * 棰勮浠g爜 + * + * @param tableId 琛ㄧ紪鍙� + * @return 棰勮鏁版嵁鍒楄〃 + */ + @Override + public Map<String, String> previewCode(Long tableId) + { + Map<String, String> dataMap = new LinkedHashMap<>(); + // 鏌ヨ琛ㄤ俊鎭� + GenTable table = genTableMapper.selectGenTableById(tableId); + // 璁剧疆涓诲瓙琛ㄤ俊鎭� + setSubTable(table); + // 璁剧疆涓婚敭鍒椾俊鎭� + setPkColumn(table); + VelocityInitializer.initVelocity(); + + VelocityContext context = VelocityUtils.prepareContext(table); + + // 鑾峰彇妯℃澘鍒楄〃 + List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); + for (String template : templates) + { + // 娓叉煋妯℃澘 + StringWriter sw = new StringWriter(); + Template tpl = Velocity.getTemplate(template, com.walker.infrastructure.utils.StringUtils.DEFAULT_CHARSET_UTF8); + tpl.merge(context, sw); + dataMap.put(template, sw.toString()); + } + return dataMap; + } + + /** + * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 + * + * @param tableName 琛ㄥ悕绉� + * @return 鏁版嵁 + */ + @Override + public byte[] downloadCode(String tableName) + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ZipOutputStream zip = new ZipOutputStream(outputStream); + generatorCode(tableName, zip); + IOUtils.closeQuietly(zip); + return outputStream.toByteArray(); + } + + /** + * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛� + * + * @param tableName 琛ㄥ悕绉� + */ + @Override + public void generatorCode(String tableName) + { + // 鏌ヨ琛ㄤ俊鎭� + GenTable table = genTableMapper.selectGenTableByName(tableName); + // 璁剧疆涓诲瓙琛ㄤ俊鎭� + setSubTable(table); + // 璁剧疆涓婚敭鍒椾俊鎭� + setPkColumn(table); + + VelocityInitializer.initVelocity(); + + VelocityContext context = VelocityUtils.prepareContext(table); + + // 鑾峰彇妯℃澘鍒楄〃 + List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); + for (String template : templates) + { + if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) + { + // 娓叉煋妯℃澘 + StringWriter sw = new StringWriter(); + Template tpl = Velocity.getTemplate(template, CharsetKit.UTF_8); + tpl.merge(context, sw); + try + { + String path = getGenPath(table, template); + FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8); + } + catch (IOException e) + { + throw new WebRuntimeException("娓叉煋妯℃澘澶辫触锛岃〃鍚嶏細" + table.getTableName()); + } + } + } + } + + /** + * 鍚屾鏁版嵁搴� + * + * @param tableName 琛ㄥ悕绉� + */ + @Override + @Transactional + public void synchDb(String tableName) + { + GenTable table = genTableMapper.selectGenTableByName(tableName); + List<GenTableColumn> tableColumns = table.getColumns(); + Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity())); + + List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); + if (StringUtils.isEmpty(dbTableColumns)) + { + throw new WebRuntimeException("鍚屾鏁版嵁澶辫触锛屽師琛ㄧ粨鏋勪笉瀛樺湪"); + } + List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); + + dbTableColumns.forEach(column -> { + GenUtils.initColumnField(column, table); + if (tableColumnMap.containsKey(column.getColumnName())) + { + GenTableColumn prevColumn = tableColumnMap.get(column.getColumnName()); + column.setColumnId(prevColumn.getColumnId()); + if (column.isList()) + { + // 濡傛灉鏄垪琛紝缁х画淇濈暀鏌ヨ鏂瑰紡/瀛楀吀绫诲瀷閫夐」 + column.setDictType(prevColumn.getDictType()); + column.setQueryType(prevColumn.getQueryType()); + } + if (StringUtils.isNotEmpty(prevColumn.getIsRequired()) && !column.isPk() + && (column.isInsert() || column.isEdit()) + && ((column.isUsableColumn()) || (!column.isSuperColumn()))) + { + // 濡傛灉鏄�(鏂板/淇敼&闈炰富閿�/闈炲拷鐣ュ強鐖跺睘鎬�)锛岀户缁繚鐣欏繀濉�/鏄剧ず绫诲瀷閫夐」 + column.setIsRequired(prevColumn.getIsRequired()); + column.setHtmlType(prevColumn.getHtmlType()); + } + genTableColumnMapper.updateGenTableColumn(column); + } + else + { + genTableColumnMapper.insertGenTableColumn(column); + } + }); + + List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList()); + if (StringUtils.isNotEmpty(delColumns)) + { + genTableColumnMapper.deleteGenTableColumns(delColumns); + } + } + + /** + * 鎵归噺鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 + * + * @param tableNames 琛ㄦ暟缁� + * @return 鏁版嵁 + */ + @Override + public byte[] downloadCode(String[] tableNames) + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ZipOutputStream zip = new ZipOutputStream(outputStream); + for (String tableName : tableNames) + { + generatorCode(tableName, zip); + } + IOUtils.closeQuietly(zip); + return outputStream.toByteArray(); + } + + /** + * 鏌ヨ琛ㄤ俊鎭苟鐢熸垚浠g爜 + */ + private void generatorCode(String tableName, ZipOutputStream zip) + { + // 鏌ヨ琛ㄤ俊鎭� + GenTable table = genTableMapper.selectGenTableByName(tableName); + // 璁剧疆涓诲瓙琛ㄤ俊鎭� + setSubTable(table); + // 璁剧疆涓婚敭鍒椾俊鎭� + setPkColumn(table); + + VelocityInitializer.initVelocity(); + + VelocityContext context = VelocityUtils.prepareContext(table); + + // 鑾峰彇妯℃澘鍒楄〃 + List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); + for (String template : templates) + { + // 娓叉煋妯℃澘 + StringWriter sw = new StringWriter(); + Template tpl = Velocity.getTemplate(template, CharsetKit.UTF_8); + tpl.merge(context, sw); + try + { + // 娣诲姞鍒皕ip + zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table))); + IOUtils.write(sw.toString(), zip, CharsetKit.UTF_8); + IOUtils.closeQuietly(sw); + zip.flush(); + zip.closeEntry(); + } + catch (IOException e) + { + log.error("娓叉煋妯℃澘澶辫触锛岃〃鍚嶏細" + table.getTableName(), e); + } + } + } + + /** + * 淇敼淇濆瓨鍙傛暟鏍¢獙 + * + * @param genTable 涓氬姟淇℃伅 + */ + @Override + public void validateEdit(GenTable genTable) + { + if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) + { + String options = JSON.toJSONString(genTable.getParams()); + JSONObject paramsObj = JSON.parseObject(options); + if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) + { + throw new WebRuntimeException("鏍戠紪鐮佸瓧娈典笉鑳戒负绌�"); + } + else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) + { + throw new WebRuntimeException("鏍戠埗缂栫爜瀛楁涓嶈兘涓虹┖"); + } + else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) + { + throw new WebRuntimeException("鏍戝悕绉板瓧娈典笉鑳戒负绌�"); + } + else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) + { + if (StringUtils.isEmpty(genTable.getSubTableName())) + { + throw new WebRuntimeException("鍏宠仈瀛愯〃鐨勮〃鍚嶄笉鑳戒负绌�"); + } + else if (StringUtils.isEmpty(genTable.getSubTableFkName())) + { + throw new WebRuntimeException("瀛愯〃鍏宠仈鐨勫閿悕涓嶈兘涓虹┖"); + } + } + } + } + + /** + * 璁剧疆涓婚敭鍒椾俊鎭� + * + * @param table 涓氬姟琛ㄤ俊鎭� + */ + public void setPkColumn(GenTable table) + { + for (GenTableColumn column : table.getColumns()) + { + if (column.isPk()) + { + table.setPkColumn(column); + break; + } + } + if (StringUtils.isNull(table.getPkColumn())) + { + table.setPkColumn(table.getColumns().get(0)); + } + if (GenConstants.TPL_SUB.equals(table.getTplCategory())) + { + for (GenTableColumn column : table.getSubTable().getColumns()) + { + if (column.isPk()) + { + table.getSubTable().setPkColumn(column); + break; + } + } + if (StringUtils.isNull(table.getSubTable().getPkColumn())) + { + table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0)); + } + } + } + + /** + * 璁剧疆涓诲瓙琛ㄤ俊鎭� + * + * @param table 涓氬姟琛ㄤ俊鎭� + */ + public void setSubTable(GenTable table) + { + String subTableName = table.getSubTableName(); + if (StringUtils.isNotEmpty(subTableName)) + { + table.setSubTable(genTableMapper.selectGenTableByName(subTableName)); + } + } + + /** + * 璁剧疆浠g爜鐢熸垚鍏朵粬閫夐」鍊� + * + * @param genTable 璁剧疆鍚庣殑鐢熸垚瀵硅薄 + */ + public void setTableFromOptions(GenTable genTable) + { + JSONObject paramsObj = JSON.parseObject(genTable.getOptions()); + if (StringUtils.isNotNull(paramsObj)) + { + String treeCode = paramsObj.getString(GenConstants.TREE_CODE); + String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE); + String treeName = paramsObj.getString(GenConstants.TREE_NAME); + String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID); + String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME); + + genTable.setTreeCode(treeCode); + genTable.setTreeParentCode(treeParentCode); + genTable.setTreeName(treeName); + genTable.setParentMenuId(parentMenuId); + genTable.setParentMenuName(parentMenuName); + } + } + + /** + * 鑾峰彇浠g爜鐢熸垚鍦板潃 + * + * @param table 涓氬姟琛ㄤ俊鎭� + * @param template 妯℃澘鏂囦欢璺緞 + * @return 鐢熸垚鍦板潃 + */ + public static String getGenPath(GenTable table, String template) + { + String genPath = table.getGenPath(); + if (StringUtils.equals(genPath, "/")) + { + return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table); + } + return genPath + File.separator + VelocityUtils.getFileName(template, table); + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableColumnService.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableColumnService.java new file mode 100644 index 0000000..8bb2d49 --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableColumnService.java @@ -0,0 +1,45 @@ +package com.iplatform.mybatis.service; + +import com.iplatform.mybatis.domain.GenTableColumn; + +import java.util.List; + +/** + * 涓氬姟瀛楁 鏈嶅姟灞� + * + * @author manton + */ +public interface IGenTableColumnService +{ + /** + * 鏌ヨ涓氬姟瀛楁鍒楄〃 + * + * @param tableId 涓氬姟瀛楁缂栧彿 + * @return 涓氬姟瀛楁闆嗗悎 + */ + public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId); + + /** + * 鏂板涓氬姟瀛楁 + * + * @param genTableColumn 涓氬姟瀛楁淇℃伅 + * @return 缁撴灉 + */ + public int insertGenTableColumn(GenTableColumn genTableColumn); + + /** + * 淇敼涓氬姟瀛楁 + * + * @param genTableColumn 涓氬姟瀛楁淇℃伅 + * @return 缁撴灉 + */ + public int updateGenTableColumn(GenTableColumn genTableColumn); + + /** + * 鍒犻櫎涓氬姟瀛楁淇℃伅 + * + * @param ids 闇�瑕佸垹闄ょ殑鏁版嵁ID + * @return 缁撴灉 + */ + public int deleteGenTableColumnByIds(String ids); +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableService.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableService.java new file mode 100644 index 0000000..e59080d --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/service/IGenTableService.java @@ -0,0 +1,122 @@ +package com.iplatform.mybatis.service; + +import com.iplatform.mybatis.domain.GenTable; + +import java.util.List; +import java.util.Map; + +/** + * 涓氬姟 鏈嶅姟灞� + * + * @author manton + */ +public interface IGenTableService +{ + /** + * 鏌ヨ涓氬姟鍒楄〃 + * + * @param genTable 涓氬姟淇℃伅 + * @return 涓氬姟闆嗗悎 + */ + public List<GenTable> selectGenTableList(GenTable genTable); + + /** + * 鏌ヨ鎹簱鍒楄〃 + * + * @param genTable 涓氬姟淇℃伅 + * @return 鏁版嵁搴撹〃闆嗗悎 + */ + public List<GenTable> selectDbTableList(GenTable genTable); + + /** + * 鏌ヨ鎹簱鍒楄〃 + * + * @param tableNames 琛ㄥ悕绉扮粍 + * @return 鏁版嵁搴撹〃闆嗗悎 + */ + public List<GenTable> selectDbTableListByNames(String[] tableNames); + + /** + * 鏌ヨ鎵�鏈夎〃淇℃伅 + * + * @return 琛ㄤ俊鎭泦鍚� + */ + public List<GenTable> selectGenTableAll(); + + /** + * 鏌ヨ涓氬姟淇℃伅 + * + * @param id 涓氬姟ID + * @return 涓氬姟淇℃伅 + */ + public GenTable selectGenTableById(Long id); + + /** + * 淇敼涓氬姟 + * + * @param genTable 涓氬姟淇℃伅 + * @return 缁撴灉 + */ + public void updateGenTable(GenTable genTable); + + /** + * 鍒犻櫎涓氬姟淇℃伅 + * + * @param tableIds 闇�瑕佸垹闄ょ殑琛ㄦ暟鎹甀D + * @return 缁撴灉 + */ + public void deleteGenTableByIds(Long[] tableIds); + + /** + * 瀵煎叆琛ㄧ粨鏋� + * + * @param tableList 瀵煎叆琛ㄥ垪琛� + */ + public void importGenTable(List<GenTable> tableList); + + /** + * 棰勮浠g爜 + * + * @param tableId 琛ㄧ紪鍙� + * @return 棰勮鏁版嵁鍒楄〃 + */ + public Map<String, String> previewCode(Long tableId); + + /** + * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 + * + * @param tableName 琛ㄥ悕绉� + * @return 鏁版嵁 + */ + public byte[] downloadCode(String tableName); + + /** + * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛� + * + * @param tableName 琛ㄥ悕绉� + * @return 鏁版嵁 + */ + public void generatorCode(String tableName); + + /** + * 鍚屾鏁版嵁搴� + * + * @param tableName 琛ㄥ悕绉� + */ + public void synchDb(String tableName); + + /** + * 鎵归噺鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 + * + * @param tableNames 琛ㄦ暟缁� + * @return 鏁版嵁 + */ + public byte[] downloadCode(String[] tableNames); + + /** + * 淇敼淇濆瓨鍙傛暟鏍¢獙 + * + * @param genTable 涓氬姟淇℃伅 + */ + public void validateEdit(GenTable genTable); +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/GenUtils.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/GenUtils.java new file mode 100644 index 0000000..09107eb --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/GenUtils.java @@ -0,0 +1,258 @@ +package com.iplatform.mybatis.util; + +import com.iplatform.core.util.StringUtils; +import com.iplatform.mybatis.GenConstants; +import com.iplatform.mybatis.config.GenConfig; +import com.iplatform.mybatis.domain.GenTable; +import com.iplatform.mybatis.domain.GenTableColumn; +import org.apache.commons.lang3.RegExUtils; + +import java.util.Arrays; + +/** + * 浠g爜鐢熸垚鍣� 宸ュ叿绫� + * + * @author manton + */ +public class GenUtils +{ + /** + * 鍒濆鍖栬〃淇℃伅 + */ + public static void initTable(GenTable genTable, String operName) + { + genTable.setClassName(convertClassName(genTable.getTableName())); + genTable.setPackageName(GenConfig.getPackageName()); + genTable.setModuleName(getModuleName(GenConfig.getPackageName())); + genTable.setBusinessName(getBusinessName(genTable.getTableName())); + genTable.setFunctionName(replaceText(genTable.getTableComment())); + genTable.setFunctionAuthor(GenConfig.getAuthor()); + genTable.setCreateBy(operName); + } + + /** + * 鍒濆鍖栧垪灞炴�у瓧娈� + */ + public static void initColumnField(GenTableColumn column, GenTable table) + { + String dataType = getDbType(column.getColumnType()); + String columnName = column.getColumnName(); + column.setTableId(table.getTableId()); + column.setCreateBy(table.getCreateBy()); + // 璁剧疆java瀛楁鍚� + column.setJavaField(StringUtils.toCamelCase(columnName)); + // 璁剧疆榛樿绫诲瀷 + column.setJavaType(GenConstants.TYPE_STRING); + column.setQueryType(GenConstants.QUERY_EQ); + + if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType) || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType)) + { + // 瀛楃涓查暱搴﹁秴杩�500璁剧疆涓烘枃鏈煙 + Integer columnLength = getColumnLength(column.getColumnType()); + String htmlType = columnLength >= 500 || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType) ? GenConstants.HTML_TEXTAREA : GenConstants.HTML_INPUT; + column.setHtmlType(htmlType); + } + else if (arraysContains(GenConstants.COLUMNTYPE_TIME, dataType)) + { + column.setJavaType(GenConstants.TYPE_DATE); + column.setHtmlType(GenConstants.HTML_DATETIME); + } + else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType)) + { + column.setHtmlType(GenConstants.HTML_INPUT); + + // 濡傛灉鏄诞鐐瑰瀷 缁熶竴鐢˙igDecimal + String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), ","); + if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) + { + column.setJavaType(GenConstants.TYPE_BIGDECIMAL); + } + // 濡傛灉鏄暣褰� + else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) + { + column.setJavaType(GenConstants.TYPE_INTEGER); + } + // 闀挎暣褰� + else + { + column.setJavaType(GenConstants.TYPE_LONG); + } + } + + // 鎻掑叆瀛楁锛堥粯璁ゆ墍鏈夊瓧娈甸兘闇�瑕佹彃鍏ワ級 + column.setIsInsert(GenConstants.REQUIRE); + + // 缂栬緫瀛楁 + if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk()) + { + column.setIsEdit(GenConstants.REQUIRE); + } + // 鍒楄〃瀛楁 + if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk()) + { + column.setIsList(GenConstants.REQUIRE); + } + // 鏌ヨ瀛楁 + if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) + { + column.setIsQuery(GenConstants.REQUIRE); + } + + // 鏌ヨ瀛楁绫诲瀷 + if (StringUtils.endsWithIgnoreCase(columnName, "name")) + { + column.setQueryType(GenConstants.QUERY_LIKE); + } + // 鐘舵�佸瓧娈佃缃崟閫夋 + if (StringUtils.endsWithIgnoreCase(columnName, "status")) + { + column.setHtmlType(GenConstants.HTML_RADIO); + } + // 绫诲瀷&鎬у埆瀛楁璁剧疆涓嬫媺妗� + else if (StringUtils.endsWithIgnoreCase(columnName, "type") + || StringUtils.endsWithIgnoreCase(columnName, "sex")) + { + column.setHtmlType(GenConstants.HTML_SELECT); + } + // 鍥剧墖瀛楁璁剧疆鍥剧墖涓婁紶鎺т欢 + else if (StringUtils.endsWithIgnoreCase(columnName, "image")) + { + column.setHtmlType(GenConstants.HTML_IMAGE_UPLOAD); + } + // 鏂囦欢瀛楁璁剧疆鏂囦欢涓婁紶鎺т欢 + else if (StringUtils.endsWithIgnoreCase(columnName, "file")) + { + column.setHtmlType(GenConstants.HTML_FILE_UPLOAD); + } + // 鍐呭瀛楁璁剧疆瀵屾枃鏈帶浠� + else if (StringUtils.endsWithIgnoreCase(columnName, "content")) + { + column.setHtmlType(GenConstants.HTML_EDITOR); + } + } + + /** + * 鏍¢獙鏁扮粍鏄惁鍖呭惈鎸囧畾鍊� + * + * @param arr 鏁扮粍 + * @param targetValue 鍊� + * @return 鏄惁鍖呭惈 + */ + public static boolean arraysContains(String[] arr, String targetValue) + { + return Arrays.asList(arr).contains(targetValue); + } + + /** + * 鑾峰彇妯″潡鍚� + * + * @param packageName 鍖呭悕 + * @return 妯″潡鍚� + */ + public static String getModuleName(String packageName) + { + int lastIndex = packageName.lastIndexOf("."); + int nameLength = packageName.length(); + return StringUtils.substring(packageName, lastIndex + 1, nameLength); + } + + /** + * 鑾峰彇涓氬姟鍚� + * + * @param tableName 琛ㄥ悕 + * @return 涓氬姟鍚� + */ + public static String getBusinessName(String tableName) + { + int lastIndex = tableName.lastIndexOf("_"); + int nameLength = tableName.length(); + return StringUtils.substring(tableName, lastIndex + 1, nameLength); + } + + /** + * 琛ㄥ悕杞崲鎴怞ava绫诲悕 + * + * @param tableName 琛ㄥ悕绉� + * @return 绫诲悕 + */ + public static String convertClassName(String tableName) + { + boolean autoRemovePre = GenConfig.getAutoRemovePre(); + String tablePrefix = GenConfig.getTablePrefix(); + if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) + { + String[] searchList = StringUtils.split(tablePrefix, ","); + tableName = replaceFirst(tableName, searchList); + } + return StringUtils.convertToCamelCase(tableName); + } + + /** + * 鎵归噺鏇挎崲鍓嶇紑 + * + * @param replacementm 鏇挎崲鍊� + * @param searchList 鏇挎崲鍒楄〃 + * @return + */ + public static String replaceFirst(String replacementm, String[] searchList) + { + String text = replacementm; + for (String searchString : searchList) + { + if (replacementm.startsWith(searchString)) + { + text = replacementm.replaceFirst(searchString, ""); + break; + } + } + return text; + } + + /** + * 鍏抽敭瀛楁浛鎹� + * + * @param text 闇�瑕佽鏇挎崲鐨勫悕瀛� + * @return 鏇挎崲鍚庣殑鍚嶅瓧 + */ + public static String replaceText(String text) + { + return RegExUtils.replaceAll(text, "(?:琛▅鑻ヤ緷)", ""); + } + + /** + * 鑾峰彇鏁版嵁搴撶被鍨嬪瓧娈� + * + * @param columnType 鍒楃被鍨� + * @return 鎴彇鍚庣殑鍒楃被鍨� + */ + public static String getDbType(String columnType) + { + if (StringUtils.indexOf(columnType, "(") > 0) + { + return StringUtils.substringBefore(columnType, "("); + } + else + { + return columnType; + } + } + + /** + * 鑾峰彇瀛楁闀垮害 + * + * @param columnType 鍒楃被鍨� + * @return 鎴彇鍚庣殑鍒楃被鍨� + */ + public static Integer getColumnLength(String columnType) + { + if (StringUtils.indexOf(columnType, "(") > 0) + { + String length = StringUtils.substringBetween(columnType, "(", ")"); + return Integer.valueOf(length); + } + else + { + return 0; + } + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityInitializer.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityInitializer.java new file mode 100644 index 0000000..4aa3a3f --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityInitializer.java @@ -0,0 +1,35 @@ +package com.iplatform.mybatis.util; + +import com.walker.infrastructure.utils.StringUtils; +import org.apache.velocity.app.Velocity; + +import java.util.Properties; + +/** + * VelocityEngine宸ュ巶 + * + * @author manton + */ +public class VelocityInitializer +{ + /** + * 鍒濆鍖杤m鏂规硶 + */ + public static void initVelocity() + { + Properties p = new Properties(); + try + { + // 鍔犺浇classpath鐩綍涓嬬殑vm鏂囦欢 + p.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + // 瀹氫箟瀛楃闆� + p.setProperty(Velocity.INPUT_ENCODING, StringUtils.DEFAULT_CHARSET_UTF8); + // 鍒濆鍖朧elocity寮曟搸锛屾寚瀹氶厤缃甈roperties + Velocity.init(p); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } +} diff --git a/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityUtils.java b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityUtils.java new file mode 100644 index 0000000..bdffc34 --- /dev/null +++ b/iplatform-support-mybatis/src/main/java/com/iplatform/mybatis/util/VelocityUtils.java @@ -0,0 +1,419 @@ +package com.iplatform.mybatis.util; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.iplatform.core.util.StringUtils; +import com.iplatform.mybatis.GenConstants; +import com.iplatform.mybatis.domain.GenTable; +import com.iplatform.mybatis.domain.GenTableColumn; +import com.walker.infrastructure.utils.DateUtils; +import org.apache.velocity.VelocityContext; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * 妯℃澘澶勭悊宸ュ叿绫� + * + * @author manton + */ +public class VelocityUtils +{ + /** 椤圭洰绌洪棿璺緞 */ + private static final String PROJECT_PATH = "main/java"; + + /** mybatis绌洪棿璺緞 */ + private static final String MYBATIS_PATH = "main/resources/mapper"; + + /** 榛樿涓婄骇鑿滃崟锛岀郴缁熷伐鍏� */ + private static final String DEFAULT_PARENT_MENU_ID = "3"; + + /** + * 璁剧疆妯℃澘鍙橀噺淇℃伅 + * + * @return 妯℃澘鍒楄〃 + */ + public static VelocityContext prepareContext(GenTable genTable) + { + String moduleName = genTable.getModuleName(); + String businessName = genTable.getBusinessName(); + String packageName = genTable.getPackageName(); + String tplCategory = genTable.getTplCategory(); + String functionName = genTable.getFunctionName(); + + VelocityContext velocityContext = new VelocityContext(); + velocityContext.put("tplCategory", genTable.getTplCategory()); + velocityContext.put("tableName", genTable.getTableName()); + velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "銆愯濉啓鍔熻兘鍚嶇О銆�"); + velocityContext.put("ClassName", genTable.getClassName()); + velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName())); + velocityContext.put("moduleName", genTable.getModuleName()); + velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName())); + velocityContext.put("businessName", genTable.getBusinessName()); + velocityContext.put("basePackage", getPackagePrefix(packageName)); + velocityContext.put("packageName", packageName); + velocityContext.put("author", genTable.getFunctionAuthor()); + velocityContext.put("datetime", DateUtils.getTodayForHuman()); + velocityContext.put("pkColumn", genTable.getPkColumn()); + velocityContext.put("importList", getImportList(genTable)); + velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName)); + velocityContext.put("columns", genTable.getColumns()); + velocityContext.put("table", genTable); + velocityContext.put("dicts", getDicts(genTable)); + setMenuVelocityContext(velocityContext, genTable); + if (GenConstants.TPL_TREE.equals(tplCategory)) + { + setTreeVelocityContext(velocityContext, genTable); + } + if (GenConstants.TPL_SUB.equals(tplCategory)) + { + setSubVelocityContext(velocityContext, genTable); + } + return velocityContext; + } + + public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) + { + String options = genTable.getOptions(); + JSONObject paramsObj = JSON.parseObject(options); + String parentMenuId = getParentMenuId(paramsObj); + context.put("parentMenuId", parentMenuId); + } + + public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) + { + String options = genTable.getOptions(); + JSONObject paramsObj = JSON.parseObject(options); + String treeCode = getTreecode(paramsObj); + String treeParentCode = getTreeParentCode(paramsObj); + String treeName = getTreeName(paramsObj); + + context.put("treeCode", treeCode); + context.put("treeParentCode", treeParentCode); + context.put("treeName", treeName); + context.put("expandColumn", getExpandColumn(genTable)); + if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) + { + context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE)); + } + if (paramsObj.containsKey(GenConstants.TREE_NAME)) + { + context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME)); + } + } + + public static void setSubVelocityContext(VelocityContext context, GenTable genTable) + { + GenTable subTable = genTable.getSubTable(); + String subTableName = genTable.getSubTableName(); + String subTableFkName = genTable.getSubTableFkName(); + String subClassName = genTable.getSubTable().getClassName(); + String subTableFkClassName = StringUtils.convertToCamelCase(subTableFkName); + + context.put("subTable", subTable); + context.put("subTableName", subTableName); + context.put("subTableFkName", subTableFkName); + context.put("subTableFkClassName", subTableFkClassName); + context.put("subTableFkclassName", StringUtils.uncapitalize(subTableFkClassName)); + context.put("subClassName", subClassName); + context.put("subclassName", StringUtils.uncapitalize(subClassName)); + context.put("subImportList", getImportList(genTable.getSubTable())); + } + + /** + * 鑾峰彇妯℃澘淇℃伅 + * + * @return 妯℃澘鍒楄〃 + */ + public static List<String> getTemplateList(String tplCategory) + { + List<String> templates = new ArrayList<String>(); + templates.add("vm/java/bo.java.vm"); + templates.add("vm/java/queryBo.java.vm"); + templates.add("vm/java/vo.java.vm"); + templates.add("vm/java/domain.java.vm"); + templates.add("vm/java/mapper.java.vm"); + templates.add("vm/java/service.java.vm"); + templates.add("vm/java/serviceImpl.java.vm"); + templates.add("vm/java/controller.java.vm"); + templates.add("vm/xml/mapper.xml.vm"); + templates.add("vm/sql/sql.vm"); + templates.add("vm/js/api.js.vm"); + if (GenConstants.TPL_CRUD.equals(tplCategory)) + { + templates.add("vm/vue/index.vue.vm"); + } + else if (GenConstants.TPL_TREE.equals(tplCategory)) + { + templates.add("vm/vue/index-tree.vue.vm"); + } + else if (GenConstants.TPL_SUB.equals(tplCategory)) + { + templates.add("vm/vue/index.vue.vm"); + templates.add("vm/java/sub-domain.java.vm"); + } + return templates; + } + + /** + * 鑾峰彇鏂囦欢鍚� + */ + public static String getFileName(String template, GenTable genTable) + { + // 鏂囦欢鍚嶇О + String fileName = ""; + // 鍖呰矾寰� + String packageName = genTable.getPackageName(); + // 妯″潡鍚� + String moduleName = genTable.getModuleName(); + // 澶у啓绫诲悕 + String className = genTable.getClassName(); + // 涓氬姟鍚嶇О + String businessName = genTable.getBusinessName(); + + String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/"); + String mybatisPath = MYBATIS_PATH + "/" + moduleName; + String vuePath = "vue"; + + if (template.contains("bo.java.vm")) + { + fileName = StringUtils.format("{}/domain/bo/{}Bo.java", javaPath, className); + } + if (template.contains("queryBo.java.vm")) + { + fileName = StringUtils.format("{}/domain/bo/{}QueryBo.java", javaPath, className); + } + if (template.contains("vo.java.vm")) + { + fileName = StringUtils.format("{}/domain/vo/{}Vo.java", javaPath, className); + } + + if (template.contains("domain.java.vm")) + { + fileName = StringUtils.format("{}/domain/{}.java", javaPath, className); + } + if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory())) + { + fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName()); + } + else if (template.contains("mapper.java.vm")) + { + fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className); + } + else if (template.contains("service.java.vm")) + { + fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className); + } + else if (template.contains("serviceImpl.java.vm")) + { + fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className); + } + else if (template.contains("controller.java.vm")) + { + fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className); + } + else if (template.contains("mapper.xml.vm")) + { + fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className); + } + else if (template.contains("sql.vm")) + { + fileName = businessName + "Menu.sql"; + } + else if (template.contains("api.js.vm")) + { + fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName); + } + else if (template.contains("index.vue.vm")) + { + fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName); + } + else if (template.contains("index-tree.vue.vm")) + { + fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName); + } + return fileName; + } + + /** + * 鑾峰彇鍖呭墠缂� + * + * @param packageName 鍖呭悕绉� + * @return 鍖呭墠缂�鍚嶇О + */ + public static String getPackagePrefix(String packageName) + { + int lastIndex = packageName.lastIndexOf("."); + return StringUtils.substring(packageName, 0, lastIndex); + } + + /** + * 鏍规嵁鍒楃被鍨嬭幏鍙栧鍏ュ寘 + * + * @param genTable 涓氬姟琛ㄥ璞� + * @return 杩斿洖闇�瑕佸鍏ョ殑鍖呭垪琛� + */ + public static HashSet<String> getImportList(GenTable genTable) + { + List<GenTableColumn> columns = genTable.getColumns(); + GenTable subGenTable = genTable.getSubTable(); + HashSet<String> importList = new HashSet<String>(); + if (StringUtils.isNotNull(subGenTable)) + { + importList.add("java.util.List"); + } + for (GenTableColumn column : columns) + { + if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) + { + importList.add("java.util.Date"); + importList.add("com.fasterxml.jackson.annotation.JsonFormat"); + } + else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) + { + importList.add("java.math.BigDecimal"); + } + } + return importList; + } + + /** + * 鏍规嵁鍒楃被鍨嬭幏鍙栧瓧鍏哥粍 + * + * @param genTable 涓氬姟琛ㄥ璞� + * @return 杩斿洖瀛楀吀缁� + */ + public static String getDicts(GenTable genTable) + { + List<GenTableColumn> columns = genTable.getColumns(); + Set<String> dicts = new HashSet<String>(); + addDicts(dicts, columns); + if (StringUtils.isNotNull(genTable.getSubTable())) + { + List<GenTableColumn> subColumns = genTable.getSubTable().getColumns(); + addDicts(dicts, subColumns); + } + return StringUtils.join(dicts, ", "); + } + + /** + * 娣诲姞瀛楀吀鍒楄〃 + * + * @param dicts 瀛楀吀鍒楄〃 + * @param columns 鍒楅泦鍚� + */ + public static void addDicts(Set<String> dicts, List<GenTableColumn> columns) + { + for (GenTableColumn column : columns) + { + if (!column.isSuperColumn() && StringUtils.isNotEmpty(column.getDictType()) && StringUtils.equalsAny( + column.getHtmlType(), + new String[] { GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX })) + { + dicts.add("'" + column.getDictType() + "'"); + } + } + } + + /** + * 鑾峰彇鏉冮檺鍓嶇紑 + * + * @param moduleName 妯″潡鍚嶇О + * @param businessName 涓氬姟鍚嶇О + * @return 杩斿洖鏉冮檺鍓嶇紑 + */ + public static String getPermissionPrefix(String moduleName, String businessName) + { + return StringUtils.format("{}:{}", moduleName, businessName); + } + + /** + * 鑾峰彇涓婄骇鑿滃崟ID瀛楁 + * + * @param paramsObj 鐢熸垚鍏朵粬閫夐」 + * @return 涓婄骇鑿滃崟ID瀛楁 + */ + public static String getParentMenuId(JSONObject paramsObj) + { + if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID) + && StringUtils.isNotEmpty(paramsObj.getString(GenConstants.PARENT_MENU_ID))) + { + return paramsObj.getString(GenConstants.PARENT_MENU_ID); + } + return DEFAULT_PARENT_MENU_ID; + } + + /** + * 鑾峰彇鏍戠紪鐮� + * + * @param paramsObj 鐢熸垚鍏朵粬閫夐」 + * @return 鏍戠紪鐮� + */ + public static String getTreecode(JSONObject paramsObj) + { + if (paramsObj.containsKey(GenConstants.TREE_CODE)) + { + return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE)); + } + return StringUtils.EMPTY; + } + + /** + * 鑾峰彇鏍戠埗缂栫爜 + * + * @param paramsObj 鐢熸垚鍏朵粬閫夐」 + * @return 鏍戠埗缂栫爜 + */ + public static String getTreeParentCode(JSONObject paramsObj) + { + if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) + { + return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE)); + } + return StringUtils.EMPTY; + } + + /** + * 鑾峰彇鏍戝悕绉� + * + * @param paramsObj 鐢熸垚鍏朵粬閫夐」 + * @return 鏍戝悕绉� + */ + public static String getTreeName(JSONObject paramsObj) + { + if (paramsObj.containsKey(GenConstants.TREE_NAME)) + { + return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME)); + } + return StringUtils.EMPTY; + } + + /** + * 鑾峰彇闇�瑕佸湪鍝竴鍒椾笂闈㈡樉绀哄睍寮�鎸夐挳 + * + * @param genTable 涓氬姟琛ㄥ璞� + * @return 灞曞紑鎸夐挳鍒楀簭鍙� + */ + public static int getExpandColumn(GenTable genTable) + { + String options = genTable.getOptions(); + JSONObject paramsObj = JSON.parseObject(options); + String treeName = paramsObj.getString(GenConstants.TREE_NAME); + int num = 0; + for (GenTableColumn column : genTable.getColumns()) + { + if (column.isList()) + { + num++; + String columnName = column.getColumnName(); + if (columnName.equals(treeName)) + { + break; + } + } + } + return num; + } +} diff --git a/iplatform-support-mybatis/src/test/java/com/iplatform/AppTest.java b/iplatform-support-mybatis/src/test/java/com/iplatform/AppTest.java new file mode 100644 index 0000000..76e83ab --- /dev/null +++ b/iplatform-support-mybatis/src/test/java/com/iplatform/AppTest.java @@ -0,0 +1,20 @@ +package com.iplatform; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} diff --git a/pom.xml b/pom.xml index a966b7b..2ba981f 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,7 @@ <module>ishop-model-pojo</module> <module>ishop-mobile</module> <module>iplatform-base-tcp-client</module> + <module>iplatform-support-mybatis</module> </modules> <parent> @@ -73,6 +74,9 @@ <!-- <ishop-model-pojo.version>2.3.0-SNAPSHOT</ishop-model-pojo.version>--> <!-- <ishop-merchant.version>2.3.0-SNAPSHOT</ishop-merchant.version>--> <!-- <ishop-mobile.version>2.3.0-SNAPSHOT</ishop-mobile.version>--> + <mybatis-plus-boot-starter.version>3.5.3.1</mybatis-plus-boot-starter.version> + <mybatis-plus-annotation.version>3.5.5</mybatis-plus-annotation.version> + <fastjson.version>2.0.23</fastjson.version> </properties> <dependencyManagement> @@ -208,6 +212,13 @@ <version>${iplatform.version}</version> </dependency> + <!-- mybatis鏀寔妯″潡锛�2024-01-16 --> + <dependency> + <groupId>com.iplatform</groupId> + <artifactId>iplatform-support-mybatis</artifactId> + <version>${iplatform.version}</version> + </dependency> + <!-- 鎺ㄩ�佸疄鐜版ā鍧楋紝2023-04-24 --> <dependency> <groupId>com.walkersoft</groupId> @@ -274,6 +285,60 @@ <version>${iplatform.version}</version> </dependency> + <!-- mybatis-plus,2024-01-16--> + <dependency> + <groupId>com.baomidou</groupId> + <artifactId>mybatis-plus-boot-starter</artifactId> + <version>${mybatis-plus-boot-starter.version}</version> + <exclusions> + <!-- <exclusion>--> + <!-- <groupId>org.springframework.boot</groupId>--> + <!-- <artifactId>spring-boot-autoconfigure</artifactId>--> + <!-- </exclusion>--> + <!-- <exclusion>--> + <!-- <groupId>org.springframework.boot</groupId>--> + <!-- <artifactId>spring-boot-starter-jdbc</artifactId>--> + <!-- </exclusion>--> + <!-- <exclusion>--> + <!-- <groupId>org.springframework.boot</groupId>--> + <!-- <artifactId>spring-boot-configuration-processor</artifactId>--> + <!-- </exclusion>--> + <!-- <exclusion>--> + <!-- <groupId>org.springframework.boot</groupId>--> + <!-- <artifactId>spring-boot-autoconfigure-processor</artifactId>--> + <!-- </exclusion>--> + <exclusion> + <groupId>org.mybatis.scripting</groupId> + <artifactId>mybatis-thymeleaf</artifactId> + </exclusion> + <exclusion> + <groupId>org.mybatis.scripting</groupId> + <artifactId>mybatis-velocity</artifactId> + </exclusion> + <exclusion> + <groupId>org.mybatis.scripting</groupId> + <artifactId>mybatis-freemarker</artifactId> + </exclusion> + <exclusion> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-commons</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>com.baomidou</groupId> + <artifactId>mybatis-plus-annotation</artifactId> + <version>${mybatis-plus-annotation.version}</version> + <type>jar.sha256</type> + </dependency> + + <!-- 闃块噷JSON瑙f瀽鍣� --> + <dependency> + <groupId>com.alibaba.fastjson2</groupId> + <artifactId>fastjson2</artifactId> + <version>${fastjson.version}</version> + </dependency> + </dependencies> </dependencyManagement> -- Gitblit v1.9.1