preface

Fleet Management similar SaaS platform, from 0 to 1, continues..

In the last article, we covered templates for code generation. Templates, or a more friendly and configurable way of code generation, have their pros and cons. This article continues the implementation logic behind templates.

(I am Java after midnight, in nuggets this share experience, those who rely on copy of the handling of the author, without permission, do not copy the article)

Train of thought

Obtain table information by table name and invoke template to generate corresponding code

implementation

I’m not going to do control, I’m just going to do List, but let’s do Service logic. This is a reference to everyone open source, currently open source projects or quite many. Returning byte bytes is convenient for front-end export download, so people you want to generate directly into the local path can also be

import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.ByteArrayOutputStream; import java.util.List; import java.util.Map; import java.util.zip.ZipOutputStream; @service Public class SysGeneratorDao {@autoWired Private GeneratorDao; /** * generate code from table name, Support multiple tables together * @param tableNames indicates collection * @return */ public byte[] generatorCode(String[] tableNames) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); For (String tableName: tableNames){// queryTable information Map<String, String> table = queryTable(tableName); List<Map<String, String>> columns = queryColumns(tableName); GeneratorCode (table, columns, zip); } IOUtils.closeQuietly(zip); return outputStream.toByteArray(); } @param tableName * @return */ public Map<String, String> queryTable(String tableName) { return generatorDao.queryTable(tableName); } @param tableName * @return */ public List<Map<String, String>> queryColumns(String tableName) { return generatorDao.queryColumns(tableName); }}Copy the code

Code generation

private static final String COLUMN_NAME = "columnName"; private static final String TABLE_NAME = "tableName"; private static final String TABLE_COMMENT = "tableComment"; private static final String TABLE_PREFIX = "tablePrefix"; private static final String DATA_TYPE = "dataType"; private static final String COLUMN_COMMENT = "columnComment"; private static final String EXTRA = "extra"; private static final String UNKNOW_TYPE = "unknowType"; private static final String BIGDECIMAL = "BigDecimal"; private static final String COLUMN_KEY = "columnKey"; private static final String PRI = "PRI"; Public static void generatorCode(Map<String, String> table, List<Map<String, String>> columns, ZipOutputStream zip){Configuration config = getConfig(); boolean hasBigDecimal = false; // TableEntity TableEntity = new TableEntity(); tableEntity.setTableName(table.get(TABLE_NAME)); tableEntity.setComments(table.get(TABLE_COMMENT)); String className = tableToJava(tableEntity.gettablename (), config.getString(TABLE_PREFIX)); tableEntity.setClassName(className); tableEntity.setClassname(StringUtils.uncapitalize(className)); List<ColumnEntity> columsList = new ArrayList<>(); for(Map<String, String> column : columns){ ColumnEntity entity = new ColumnEntity(); entity.setColumnName(column.get(COLUMN_NAME)); entity.setDataType(column.get(DATA_TYPE)); entity.setComments(column.get(COLUMN_COMMENT)); entity.setExtra(column.get(EXTRA)); String attrName = columnToJava(entity.getColumnName()); entity.setAttrName(attrName); entity.setAttrname(StringUtils.uncapitalize(attrName)); String attrType = config.getString(entity.getDataType(), UNKNOW_TYPE); entity.setAttrType(attrType); if (! hasBigDecimal && attrType.equals(BIGDECIMAL)) { hasBigDecimal = true; } if(pri.equalsignoRecase (column.get(COLUMN_KEY)) && tableEntity.getpk () == null){tableEntity.setpk (entity); } columsList.add(entity); } tableEntity.setColumns(columsList); If (tableEntity.getpk () == null){tableentity.setpk (tableentity.getColumns ().get(0)); }Copy the code

conclusion

Code generation, there are a lot of cases, none of which is the best, but I’ll leave it at that, because I think you probably have a couple of them.

SaaS system is built from 0 to 1, to be continued….