4、常数项添加

2020-05-07  本文已影响0人  wqjcarnation

前台

<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" >

      <el-select v-model="ruleForm.constantTypeID" placeholder="请选择常数类别名" prop="constantTypeID">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.constantTypeName"
            :value="item.id">
          </el-option>
        </el-select>

        <el-form-item label="常数相编码" prop="contantCode">
          <el-input v-model="ruleForm.contantCode"></el-input>
        </el-form-item>

        <el-form-item label="常数相名称" prop="constantName">
          <el-input v-model="ruleForm.constantName"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>

      </el-form>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        options: [],
        ruleForm:{
          contantCode:'',
          constantName:'',
          constantTypeID:'',
        },
        rules:{
        contantCode:[{required: true, message: '请输入常数项编码', trigger: 'blur'},
                          {min:2,max:64,message:'编码长度在2-64之间',trigger: 'blur'}],
        constantName:[{required: true, message: '请输入常数类别名称', trigger: 'blur'},
                          {min:1,max:64,message:'常数名称在1-64之间',trigger: 'blur'}],
        constantTypeID:[{required: true, message: '请选择常数类别名', trigger: 'blur'}]
        }
      }
    },
    methods:{
      /**
       * [{
"id":"402880ed71ecf0ef0171ecf3182c0000",
"constantTypeCode":"DeptCategory",
"constantTypeName":"科室分类",
"delMark":1
},......]
       */
      selectAllConstantTypeID(){
        
        this.$axios.get("http://localhost:8080/constatntType/selectAllConstantTypeID")
        .then(response=>{
            this.options = response.data;
        })
        .catch()
      },
      submitForm:function(formName){
      //alert("提交表单");
      //result返回的是boolean
      this.$refs.ruleForm.validate((result)=>{
        if(result){
          //alert(this.ruleForm.contantCode);//接着请求后台进行添加
          //.post(url,data,options)options:传数据的类型json
          this.$axios.post("http://localhost:8080/constantItem/save",
          this.ruleForm,
          {headers:{'Content-Type': 'application/json;charset=UTF-8'}})
          .then(response=>{
           //alert(response.data.id);
            if(response.data.id != null){
              this.$message('添加成功');
            }
          })
          .catch(err=>{
            //alert(err);
            this.$message('添加失败');
          })
        }
      })
      },
      resetForm:function(formName){
      //lert("重置");
      this.$refs.ruleForm.resetFields();
      },

    },
    mounted(){
      this.selectAllConstantTypeID()
    }
  }
</script>

后台

    @Entity
    @Table(name = "t_constantItem")
    @GenericGenerator(name = "jpa-uuid", strategy = "uuid")
    public class ConstantItem {
        @Id
        @GeneratedValue(generator = "jpa-uuid")
        @Column(name = "ID", nullable = false, length = 32)
        // ID
        private String ID;
        // 所属常数类别ID
        @Column(name = "constantTypeID", nullable = false, length = 32)
        private String constantTypeID;
        // 常数项编码
        @Column(name = "ContantCode", nullable = false, length = 64)
        private String ContantCode;
        // 常数项名称
        @Column(name = "ConstantName", nullable = false, length = 64)
        private String ConstantName;
        // 删除标记
        @Column(name = "DelMark", nullable = false, length = 1)
        private String DelMark="1";
        public String getID() {
            return ID;
        }
        public void setID(String iD) {
            ID = iD;
        }
        
        public String getConstantTypeID() {
            return constantTypeID;
        }
        public void setConstantTypeID(String constantTypeID) {
            this.constantTypeID = constantTypeID;
        }
        public String getContantCode() {
            return ContantCode;
        }
        public void setContantCode(String contantCode) {
            ContantCode = contantCode;
        }
        public String getConstantName() {
            return ConstantName;
        }
        public void setConstantName(String constantName) {
            ConstantName = constantName;
        }
        public String getDelMark() {
            return DelMark;
        }
        public void setDelMark(String delMark) {
            DelMark = delMark;
        }
        
        
    
    }

ConstantTypeController中新增加

@RequestMapping("selectAllConstantTypeID")
public List<ConstantType> selectAllConstantTypeID() {
    List<ConstantType> resultlist = constantTypeService.selectAllConstantTypeID();
    return resultlist;
}

新建ConstantItemController

    @RestController
    @RequestMapping("constantItem")
    public class ConstantItemController {
        @Autowired
        IConstantItemService service;
        
        @RequestMapping("save")
        public ConstantItem save(@RequestBody ConstantItem type) {
            ConstantItem result = service.save(type);
            return result;
        }
        @RequestMapping("findByTypeId")
        public List<ConstantItem> findByTypeId(String typeid) {
            List<ConstantItem>  result = service.findByConstantTypeID(typeid);
            return result;
        }
        
    }

ConstantItemServiceImpl

    public interface ConstantItemRepository extends JpaRepository<ConstantItem, String> {
        List<ConstantItem> findByConstantTypeID(String typeid);
    }
    @Service
    public class ConstantItemServiceImpl implements IConstantItemService {
        @Autowired
        ConstantItemRepository responsitory;
        
        @Override
        public ConstantItem save(ConstantItem type) {
            return responsitory.save(type);
        }
    
        @Override
        public List<ConstantItem> findByConstantTypeID(String typeid) {
            return responsitory.findByConstantTypeID(typeid);
        }
    }

注意:ConstantItem.java中的属性名首字母改小写,否则报错
以后写实体类也需要注意这一点

上一篇 下一篇

猜你喜欢

热点阅读