浏览代码

数据的增删改已完成

wuweihao 3 年之前
父节点
当前提交
3b8bcfdf63

+ 21 - 1
gis_db/src/main/java/com/gis/db/controller/RecordController.java

@@ -2,6 +2,7 @@ package com.gis.db.controller;
 
 import com.gis.common.base.entity.dto.PageDto;
 import com.gis.common.util.Result;
+import com.gis.db.entity.dto.RecordDto;
 import com.gis.db.service.FieldService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -30,9 +31,28 @@ public class RecordController {
 
 
     @ApiOperation(value = "查询-详情", notes = "tableId:表id; id:记录id")
-    @PostMapping("detail/{tableId}/{id}")
+    @GetMapping("detail/{tableId}/{id}")
     public Result detail(@PathVariable Long tableId, @PathVariable Long id){
         return fieldService.detail(tableId, id);
     }
 
+    @ApiOperation(value = "新增")
+    @PostMapping("/insert")
+    public Result insertRecord(@Valid @RequestBody RecordDto param){
+        return fieldService.insertRecord(param);
+    }
+
+    @ApiOperation(value = "编辑")
+    @PostMapping("/update")
+    public Result updateRecord(@Valid @RequestBody RecordDto param){
+        return fieldService.updateRecord(param);
+    }
+
+    @ApiOperation(value = "删除(真删除)")
+    @GetMapping("/del/{tableId}/{ids}")
+    public Result delRecord(@PathVariable String tableId, @PathVariable String ids){
+        return fieldService.delRecord(tableId, ids);
+    }
+
+
 }

+ 26 - 0
gis_db/src/main/java/com/gis/db/entity/dto/RecordDto.java

@@ -0,0 +1,26 @@
+package com.gis.db.entity.dto;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+import java.util.Map;
+
+/**
+ * Created by owen on 2022/5/10 0010 16:52
+ */
+@Data
+public class RecordDto {
+
+    @NotBlank(message = "表id不能为空")
+    @ApiModelProperty(value = "表id",required = true)
+    private String tableId;
+
+    @NotNull(message = "数据记录不能为空")
+    @ApiModelProperty(value = "数据记录(key:字段名称, value:值)",required = true)
+    private Map<String, Object> record;
+
+    @ApiModelProperty(value = "数据id")
+    private String id;
+}

+ 28 - 0
gis_db/src/main/java/com/gis/db/entity/vo/FieldVo.java

@@ -0,0 +1,28 @@
+package com.gis.db.entity.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * Created by owen on 2022/5/10 0010 16:12
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class FieldVo {
+
+    @ApiModelProperty(value = "字段名称")
+    private String name;
+
+    @ApiModelProperty(value = "字段类型")
+    private String type;
+
+    @ApiModelProperty(value = "字段注释")
+    private String remark;
+
+    @ApiModelProperty(value = "字段长度")
+    private Integer length;
+
+}

+ 8 - 0
gis_db/src/main/java/com/gis/db/mapper/DdlMapper.java

@@ -1,12 +1,14 @@
 package com.gis.db.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Delete;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
+import java.util.Map;
 
 
 /**
@@ -26,10 +28,16 @@ public interface DdlMapper extends BaseMapper<Object> {
     // 批量删除字段, 但字段不存在会出异常
     void delFields(@Param("tableName")String tableName, @Param("fieldNames")List<String> fieldNames);
 
+    // 字段不存在会抛异常
     void delField(@Param("tableName")String tableName, @Param("fieldName")String fieldName);
 
     // 检查字段是否存在
     @Select("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =#{tableName} AND COLUMN_NAME =#{fieldName}")
     Integer existColumn(String tableName, String fieldName);
 
+    void insertRecord(@Param("tableName")String tableName, @Param("fieldNames")List<String> fieldNames, @Param("fieldData")List<Object> fieldData);
+
+    void updateRecord(@Param("tableName")String tableName, @Param("id")String id, @Param("fieldData") Map<String, Object> fieldData);
+
+    void delRecord(@Param("tableName")String tableName, @Param("ids")String ids);
 }

+ 7 - 4
gis_db/src/main/java/com/gis/db/service/FieldService.java

@@ -3,10 +3,7 @@ package com.gis.db.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.gis.common.base.entity.dto.PageDto;
 import com.gis.common.util.Result;
-import com.gis.db.entity.dto.FieldDto;
-import com.gis.db.entity.dto.FieldPageDto;
-import com.gis.db.entity.dto.IdsDto;
-import com.gis.db.entity.dto.TableDto;
+import com.gis.db.entity.dto.*;
 import com.gis.db.entity.po.FieldEntity;
 import com.gis.db.entity.po.TableEntity;
 
@@ -30,4 +27,10 @@ public interface FieldService extends IService<FieldEntity> {
     Result delField(IdsDto param);
 
     Result getList(FieldPageDto param);
+
+    Result insertRecord(RecordDto param);
+
+    Result delRecord(String tableId, String ids);
+
+    Result updateRecord(RecordDto param);
 }

+ 2 - 0
gis_db/src/main/java/com/gis/db/service/TableService.java

@@ -22,4 +22,6 @@ public interface TableService extends IService<TableEntity> {
     Result removes(IdsDto ids);
 
     List<String> getTableNameByTableId(List<Integer> tableIds);
+
+    String getTableName(String tableId);
 }

+ 76 - 6
gis_db/src/main/java/com/gis/db/service/impl/FieldServiceImpl.java

@@ -14,8 +14,10 @@ import com.gis.common.util.Result;
 import com.gis.db.entity.dto.FieldDto;
 import com.gis.db.entity.dto.FieldPageDto;
 import com.gis.db.entity.dto.IdsDto;
+import com.gis.db.entity.dto.RecordDto;
 import com.gis.db.entity.po.FieldEntity;
 import com.gis.db.entity.po.TableEntity;
+import com.gis.db.entity.vo.FieldVo;
 import com.gis.db.mapper.DdlMapper;
 import com.gis.db.mapper.FieldMapper;
 import com.gis.db.service.DdlService;
@@ -150,6 +152,52 @@ public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> impl
         return Result.success(iPage);
     }
 
+    /**
+     * 添加数据
+     * @param param
+     * @return
+     */
+    @Override
+    public Result insertRecord(RecordDto param) {
+
+        String tableName = tableService.getTableName(param.getTableId());
+
+        Map<String, Object> record = param.getRecord();
+        // 字段
+        List<String> fieldList = new ArrayList<>();
+        // 字段对应的值
+        List<Object> fieldData = new ArrayList<>();
+        for (Map.Entry<String, Object> entry : record.entrySet()) {
+            fieldList.add(entry.getKey());
+            fieldData.add(entry.getValue());
+        }
+
+        ddlMapper.insertRecord(tableName, fieldList, fieldData);
+
+        return Result.success();
+    }
+
+    @Override
+    public Result delRecord(String tableId, String ids) {
+        String tableName = tableService.getTableName(tableId);
+        ddlMapper.delRecord(tableName, ids);
+        return Result.success();
+    }
+
+    @Override
+    public Result updateRecord(RecordDto param) {
+        String tableName = tableService.getTableName(param.getTableId());
+
+        String id = param.getId();
+        BaseRuntimeException.isBlank(id, null, "数据id不能为空");
+
+        // 字段对应的值
+        Map<String, Object> record = param.getRecord();
+
+        ddlMapper.updateRecord(tableName, id, record);
+        return Result.success();
+    }
+
     private List<String> getFieldName(List<Integer> ids){
         List<FieldEntity> entityList = this.listByIds(ids);
         List<String> filedNames = new ArrayList<>();
@@ -198,23 +246,45 @@ public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> impl
 
 
 
-    private Map getResultByTableId(Long tableId){
+    private Map getMapByTableId(Long tableId){
         List<FieldEntity> list = findByTableId(tableId);
 
         Map filedMap = new HashMap();
+        FieldVo vo ;
         for (FieldEntity entity : list) {
             filedMap.put(entity.getName(), entity.getType());
+            vo = new FieldVo();
+            BeanUtils.copyProperties(entity, vo);
+            filedMap.put(entity.getName(), vo);
         }
 
         // 添加默认字段: 使用数据库下划线命名
-        filedMap.put("id", "int");
-        filedMap.put("create_time", "date");
-        filedMap.put("update_time", "date");
-        filedMap.put("creator_id", "int");
+        filedMap.put("id", new FieldVo("id", "int", "业务id", 8));
+        filedMap.put("create_time", new FieldVo("create_time", "date", "创建时间", 0));
+        filedMap.put("update_time", new FieldVo("update_time", "date", "修改时间", 0));
+        filedMap.put("creator_id", new FieldVo("creator_id", "int", "创建者id", 8));
+
 
         return filedMap;
     }
 
+    private List<Object>  gertFieldListByTableId(Long tableId){
+        List<FieldEntity> list = findByTableId(tableId);
+
+        List<Object> fields = new ArrayList<>();
+        FieldVo vo ;
+        for (FieldEntity entity : list) {
+            vo = new FieldVo();
+            BeanUtils.copyProperties(entity, vo);
+            fields.add(vo);
+        }
+
+        // 添加默认字段: 使用数据库下划线命名
+        fields.add(new FieldVo("id", "int", "业务id", 8));
+        fields.add(new FieldVo("update_time", "date", "修改时间", 0));
+        return fields;
+    }
+
 
     @Override
     public Result getFieldData(Long tableId, PageDto param) {
@@ -225,7 +295,7 @@ public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> impl
         IPage<Map> resPage = getBaseMapper().page(tableEntity.getName(), getFileNames(tableId), page);
 
         Map resultMap = new HashMap();
-        resultMap.put("fieldNames", getResultByTableId(tableId));
+        resultMap.put("fieldNames", gertFieldListByTableId(tableId));
         resultMap.put("fieldData", resPage);
 
         return Result.success(resultMap);

+ 12 - 3
gis_db/src/main/java/com/gis/db/service/impl/TableServiceImpl.java

@@ -1,14 +1,11 @@
 package com.gis.db.service.impl;
 
-import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.lang.Validator;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.gis.common.base.entity.dto.PageDto;
-import com.gis.common.base.entity.po.BaseEntity;
 import com.gis.common.base.exception.BaseRuntimeException;
 import com.gis.common.constant.ErrorEnum;
 import com.gis.common.util.BaseUtil;
@@ -24,6 +21,7 @@ import com.gis.db.service.TableService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
@@ -43,6 +41,7 @@ public class TableServiceImpl extends ServiceImpl<TableMapper, TableEntity> impl
     @Autowired
     FieldService fieldService;
 
+    @Cacheable
     @Override
     public Result createTable(TableDto param) {
 
@@ -115,6 +114,16 @@ public class TableServiceImpl extends ServiceImpl<TableMapper, TableEntity> impl
         return (ArrayList) list;
     }
 
+    @Override
+    public String getTableName(String tableId) {
+        TableEntity entity = this.getById(tableId);
+        BaseRuntimeException.isNull(entity, null, "该表不存在");
+        String name = entity.getName();
+        BaseRuntimeException.isBlank(name, null, "表名为空, 请检查");
+
+        return name;
+    }
+
     /**
      * 判断表面是否存在
      * @param name

+ 23 - 1
gis_db/src/main/resources/mybatis-mapper/DdlMapper.xml

@@ -13,12 +13,30 @@
           PRIMARY KEY (`id`)
         ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = #{remark}
     </insert>
+    <insert id="insertRecord">
+        insert into ${tableName} (
+        <foreach collection="fieldNames" index="index" item="name" separator=",">
+            ${name}
+        </foreach>
+        ) VALUES (
+        <foreach collection="fieldData" index="indexData" item="data" separator=",">
+            "${data}"
+        </foreach>
+        )
+    </insert>
 
 
     <update id="updateSql">
         ${param}
     </update>
 
+    <update id="updateRecord" parameterType="java.util.Map">
+        update ${tableName} set
+        <foreach collection="fieldData.keys" index="index" item="name" separator=",">
+            ${name} = #{fieldData[${name}]}
+        </foreach>
+        where id = ${id}
+    </update>
 
 
     <delete id="delTable">
@@ -34,7 +52,11 @@
     </delete>
 
     <delete id="delField">
-        ALTER TABLE ${tableName}  DROP IF EXISTS ${fieldName}
+        ALTER TABLE ${tableName}  DROP ${fieldName}
+    </delete>
+
+    <delete id="delRecord">
+        delete FROM ${tableName} where id in (${ids})
     </delete>
 
 </mapper>