tanghc 3 years ago
parent
commit
9522662e52

+ 3 - 0
changelog.md

@@ -3,6 +3,9 @@
 ## 1.4.1
 
 - 修复数据库字段单个单词全部大写的情况下,实体字段会全部大写
+- 修复oracle的number类型映射问题
+- 生成代码页面自动选择上一次数据源
+- 修复新建模版template_config里group_name会丢失问题
 
 ## 1.4.0
 

+ 10 - 1
front/src/utils/global.js

@@ -84,6 +84,15 @@ Object.assign(Vue.prototype, {
     const frm = this.$refs[formName]
     frm && frm.resetFields()
   },
+  setAttr: function(key, val) {
+    if (val === undefined) {
+      val = ''
+    }
+    localStorage.setItem(key, val + '')
+  },
+  getAttr: function(key) {
+    return localStorage.getItem(key)
+  },
   logout: function() {
     const fullPath = this.$route.fullPath
     if (fullPath.indexOf('login?redirect') === -1) {
@@ -133,6 +142,6 @@ Object.assign(Vue.prototype, {
 
     element.click()
 
-    document.body.removeChild(element);
+    document.body.removeChild(element)
   }
 })

+ 58 - 43
front/src/views/generate/GenerateConfig/index.vue

@@ -63,18 +63,18 @@
       <el-col :span="12" id="templateSelect">
         <h4>选择模板</h4>
         <el-select
-          v-model="clientParam.groupName"
+          v-model="groupId"
           placeholder="选择模板所在组"
           size="mini"
-          @change="onDataGroupChange"
           style="margin-bottom: 10px; width: 100%;"
         >
           <el-option
             v-for="item in groupData"
             :key="item.id"
-            :label="`${item.groupName}`"
+            :label="item.groupName"
             :value="item.id"
           >
+            {{ item.groupName }}
           </el-option>
         </el-select>
         <el-table
@@ -150,18 +150,18 @@
         </el-form-item>
         <el-form-item label="代码生成器模板" prop="delPrefix">
           <el-select
-            v-model="datasourceFormData.groupName"
+            v-model="datasourceFormData.groupId"
             placeholder="选择模板所在组"
             size="mini"
-            @change="onDataGroupChange"
             style="margin-bottom: 10px; width: 100%;"
           >
             <el-option
               v-for="item in groupData"
               :key="item.id"
-              :label="`${item.groupName}`"
+              :label="item.groupName"
               :value="item.id"
             >
+              {{ item.groupName }}
             </el-option>
           </el-select>
         </el-form-item>
@@ -194,21 +194,21 @@
   }
 </style>
 <script>
+const current_datasource_id_key = "gen-datasource-id"
 export default {
   name: 'GenerateConfig',
   data() {
     return {
       groupId: '',
-      groupData: {},
+      groupData: [],
       showTable: false,
       clientParam: {
         datasourceConfigId: '',
         tableNames: [],
         templateConfigIdList: [],
-        packageName: null,
-        delPrefix: null,
-        groupId: '',
-        groupName: ''
+        packageName: '',
+        delPrefix: '',
+        groupId: ''
       },
       tableSearch: '',
       datasourceConfigList: [],
@@ -227,8 +227,7 @@ export default {
         dbName: '',
         packageName: '',
         delPrefix: '',
-        groupId: '',
-        groupName: ''
+        groupId: ''
       },
       dbTypeConfig: [],
       datasourceRule: {
@@ -250,6 +249,22 @@ export default {
       }
     }
   },
+  watch: {
+    // 监听属性变化
+    'clientParam.datasourceConfigId': function (id) {
+      for (const item of this.datasourceConfigList) {
+        if (item.id === id) {
+          this.groupId = item.groupId
+          Object.assign(this.clientParam, {
+            packageName: item.packageName,
+            delPrefix: item.delPrefix,
+            groupId: item.groupId
+          })
+          break
+        }
+      }
+    }
+  },
   created() {
     this.loadDataSource()
     this.loadTemplate()
@@ -289,8 +304,24 @@ export default {
       })
     },
     loadDataSource() {
+      const cacheId = this.getCurrentDataSourceId()
       this.post('/datasource/list', {}, resp => {
-        this.datasourceConfigList = resp.data
+        let id
+        const list = resp.data
+        this.datasourceConfigList = list
+        for (const item of list) {
+          // 缓存id是否有效
+          if (item.id === cacheId) {
+            id = item.id
+            break
+          }
+        }
+        if (!id && list.length > 0) {
+          id = list[0].id
+        }
+        if (id) {
+          this.onDataSourceChange(parseInt(id))
+        }
       })
     },
     loadTemplate() {
@@ -303,11 +334,23 @@ export default {
         this.dbTypeConfig = resp.data
       })
     },
+    setCurrentDataSourceId(id) {
+      this.setAttr(current_datasource_id_key, id)
+    },
+    getCurrentDataSourceId() {
+      const id = this.getAttr(current_datasource_id_key);
+      return parseInt(id) || ''
+    },
     onDataSourceAdd() {
       this.datasourceTitle = '新建连接'
       Object.keys(this.datasourceFormData).forEach(key=>{this.datasourceFormData[key]=''})
       this.datasourceFormData.id = 0
       this.datasourceDlgShow = true
+      this.$nextTick(() => {
+        if (this.groupData.length > 0) {
+          this.datasourceFormData.groupId = this.groupData[0].id
+        }
+      })
     },
     onTableListSelect(selectedRows) {
       this.clientParam.tableNames = selectedRows
@@ -320,23 +363,8 @@ export default {
         .map(row => row.id)
     },
     onDataSourceChange(datasourceConfigId) {
+      this.setCurrentDataSourceId(datasourceConfigId)
       this.clientParam.datasourceConfigId = datasourceConfigId
-      this.datasourceConfigList.find((item)=>{
-        if(item.id === datasourceConfigId){
-          this.clientParam.packageName = item.packageName;
-          this.clientParam.delPrefix = item.delPrefix;
-          this.groupId = item.groupId;
-          this.groupData.find((gitem)=>{
-            // console.log("gid="+gitem.id+",datasourceConfigGroupId="+item.groupId+","+(gitem.id == item.groupId))
-            if(gitem.id == item.groupId){
-              this.clientParam.groupName = gitem.groupName
-            }
-          });
-
-          console.log("this.clientParam.groupName="+this.clientParam.groupName)
-        }
-      });
-
       this.post(`/datasource/table/${datasourceConfigId}`, {}, resp => {
         this.showTable = true
         this.tableListData = resp.data
@@ -345,12 +373,6 @@ export default {
     onDataSourceUpdate(item) {
       this.datasourceTitle = '修改连接'
       Object.assign(this.datasourceFormData, item)
-      this.groupData.find((gitem)=>{
-        // console.log("gid="+gitem.id+",datasourceConfigGroupId="+item.groupId+","+(gitem.id == this.datasourceFormData.groupId))
-        if(gitem.id == item.groupId){
-          this.datasourceFormData.groupName = gitem.groupName
-        }
-      });
       this.datasourceDlgShow = true
     },
     onDataSourceDuplicate(item) {
@@ -395,13 +417,6 @@ export default {
         }
       })
     },
-    onDataGroupChange(groupId){
-      // console.log(groupId)
-      if(groupId != ''){
-        this.groupId = groupId
-        this.datasourceFormData.groupId = groupId
-      }
-    },
     onDatasourceSave() {
       this.$refs.datasourceForm.validate((valid) => {
         if (valid) {

+ 1 - 1
front/src/views/template/edit.vue

@@ -104,7 +104,7 @@ export default {
   components: { codemirror },
   data() {
     return {
-      groupData: {},
+      groupData: [],
       formData: {
         id: 0,
         groupId: '',

+ 17 - 0
gen/src/main/java/com/gitee/gen/controller/TemplateConfigController.java

@@ -3,7 +3,9 @@ package com.gitee.gen.controller;
 import com.gitee.gen.common.Action;
 import com.gitee.gen.common.Result;
 import com.gitee.gen.entity.TemplateConfig;
+import com.gitee.gen.entity.TemplateGroup;
 import com.gitee.gen.service.TemplateConfigService;
+import com.gitee.gen.service.TemplateGroupService;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -12,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @author tanghc
@@ -23,6 +27,9 @@ public class TemplateConfigController {
     @Autowired
     private TemplateConfigService templateConfigService;
 
+    @Autowired
+    private TemplateGroupService templateGroupService;
+
     @RequestMapping("/add")
     public Result add(@RequestBody TemplateConfig templateConfig) {
         templateConfigService.insert(templateConfig);
@@ -42,6 +49,16 @@ public class TemplateConfigController {
         }else {
             templateConfigs = templateConfigService.listByGroupId(groupId);
         }
+        Map<Integer, String> idMap = templateGroupService.listAll()
+                .stream()
+                .collect(Collectors.toMap(TemplateGroup::getId, TemplateGroup::getGroupName));
+        for (TemplateConfig templateConfig : templateConfigs) {
+            Integer gid = templateConfig.getGroupId();
+            if (gid != null) {
+                String groupName = idMap.getOrDefault(gid, "");
+                templateConfig.setGroupName(groupName);
+            }
+        }
         return Action.ok(templateConfigs);
     }
 

+ 3 - 3
gen/src/main/java/com/gitee/gen/entity/DatasourceConfig.java

@@ -29,7 +29,7 @@ public class DatasourceConfig {
     /** 删除的前缀*/
     private String delPrefix;
     /** 代码生成器模板组id*/
-    private String groupId;
+    private Integer groupId;
 
     public Integer getId() {
         return id;
@@ -119,11 +119,11 @@ public class DatasourceConfig {
         this.delPrefix = delPrefix;
     }
 
-    public String getGroupId() {
+    public Integer getGroupId() {
         return groupId;
     }
 
-    public void setGroupId(String groupId) {
+    public void setGroupId(Integer groupId) {
         this.groupId = groupId;
     }
 

+ 34 - 23
gen/src/main/java/com/gitee/gen/gen/oracle/OracleColumnSelector.java

@@ -4,6 +4,7 @@ import com.gitee.gen.gen.ColumnDefinition;
 import com.gitee.gen.gen.ColumnSelector;
 import com.gitee.gen.gen.GeneratorConfig;
 import com.gitee.gen.gen.TypeFormatter;
+import org.apache.commons.lang.StringUtils;
 
 import java.util.Map;
 import java.util.Set;
@@ -15,6 +16,27 @@ public class OracleColumnSelector extends ColumnSelector {
 
 	private static final TypeFormatter TYPE_FORMATTER = new OracleTypeFormatter();
 
+	private static final String COLUMN_SQL = "select "
+			+ " utc.column_name as FIELD,utc.data_type TYPE, utc.data_scale SCALE, utc.data_length 最大长度, "
+			+ " 		CASE utc.nullable WHEN 'N' THEN '否' ELSE '是' END 可空, "
+			+ " utc.data_default 默认值,ucc.comments COMMENTS,UTC.table_name 表名, "
+			+ " CASE UTC.COLUMN_NAME "
+			+ " WHEN (select "
+			+ " 		col.column_name "
+			+ " 		from "
+			+ " 		user_constraints con,user_cons_columns col "
+			+ " 		where "
+			+ " 	con.constraint_name=col.constraint_name and con.constraint_type='P' "
+			+ " 		and col.table_name='%s')   THEN 'true' ELSE 'false' END AS KEY "
+			+ " 		from "
+			+ " user_tab_columns utc,user_col_comments ucc "
+			+ " 		where "
+			+ " utc.table_name = ucc.table_name "
+			+ " and utc.column_name = ucc.column_name "
+			+ " and utc.table_name = '%s' "
+			+ " order by "
+			+ " column_id ";
+
 	public OracleColumnSelector(GeneratorConfig generatorConfig) {
 		super(generatorConfig);
 	}
@@ -44,28 +66,7 @@ public class OracleColumnSelector extends ColumnSelector {
 	 */
 	@Override
 	protected String getColumnInfoSQL(String tableName) {
-		StringBuffer sb = new StringBuffer("");
-		sb.append(" select ");
-		sb.append(" utc.column_name as FIELD,utc.data_type TYPE,utc.data_length 最大长度, ");
-		sb.append(" 		CASE utc.nullable WHEN 'N' THEN '否' ELSE '是' END 可空, ");
-		sb.append(" utc.data_default 默认值,ucc.comments COMMENTS,UTC.table_name 表名, ");
-		sb.append(" CASE UTC.COLUMN_NAME ");
-		sb.append(" WHEN (select ");
-		sb.append(" 		col.column_name ");
-		sb.append(" 		from ");
-		sb.append(" 		user_constraints con,user_cons_columns col ");
-		sb.append(" 		where ");
-		sb.append(" 	con.constraint_name=col.constraint_name and con.constraint_type='P' ");
-		sb.append(" 		and col.table_name='"+tableName+"')   THEN 'true' ELSE 'false' END AS KEY ");
-		sb.append(" 		from ");
-		sb.append(" user_tab_columns utc,user_col_comments ucc ");
-		sb.append(" 		where ");
-		sb.append(" utc.table_name = ucc.table_name ");
-		sb.append(" and utc.column_name = ucc.column_name ");
-		sb.append(" and utc.table_name = '"+tableName+"' ");
-		sb.append(" order by ");
-		sb.append(" column_id ");
-		return sb.toString();
+		return String.format(COLUMN_SQL, tableName, tableName);
 	}
 	
 	@Override
@@ -86,10 +87,20 @@ public class OracleColumnSelector extends ColumnSelector {
 		columnDefinition.setIsPk(isPk);
 		
 		String type = (String)rowMap.get("TYPE");
+		// 如果是number
+		if (StringUtils.containsIgnoreCase(type, "number")) {
+			// 有精度则为decimal,否则是int
+			Object scaleCol = rowMap.get("SCALE");
+			if (scaleCol == null) {
+				scaleCol = 0;
+			}
+			String scale = String.valueOf(scaleCol);
+			type = "0".equals(scale) ? "int" : "decimal";
+		}
 		columnDefinition.setType(TYPE_FORMATTER.format(type));
 		
 		columnDefinition.setComment((String)rowMap.get("COMMENTS"));
-		
+
 		return columnDefinition;
 	}
 	

+ 1 - 1
gen/src/main/java/com/gitee/gen/gen/oracle/OracleTypeFormatter.java

@@ -32,7 +32,7 @@ public class OracleTypeFormatter implements TypeFormatter {
 
     @Override
     public boolean isInt(String columnType) {
-        return contains(Arrays.asList("int", "integer", "number"), columnType);
+        return contains(Arrays.asList("int", "integer"), columnType);
     }
 
     @Override

+ 1 - 0
gen/src/main/resources/mybatis/DatasourceConfigMapper.xml

@@ -37,6 +37,7 @@
         SELECT
         <include refid="baseColumns" />
         FROM datasource_config t
+        where is_deleted=0
     </select>
 
     <!-- 根据主键获取单条记录 -->

File diff suppressed because it is too large
+ 1 - 1
gen/src/main/resources/public/index.html


gen/src/main/resources/public/static/css/chunk-7618706a.e2948cc9.css → gen/src/main/resources/public/static/css/chunk-0bb63457.e2948cc9.css


File diff suppressed because it is too large
+ 1 - 1
gen/src/main/resources/public/static/js/app.1704855b.js


File diff suppressed because it is too large
+ 1 - 0
gen/src/main/resources/public/static/js/chunk-0bb63457.44392e8b.js


File diff suppressed because it is too large
+ 1 - 1
gen/src/main/resources/public/static/js/chunk-70c97e3b.8b16cf31.js


File diff suppressed because it is too large
+ 0 - 1
gen/src/main/resources/public/static/js/chunk-7618706a.4be4bc14.js