从早上7点过一直搞到现在,终于实现Vue实现表单提交给SpringBoot后台并存入Mysql数据库,因为之前没有接触Vue,就昨天看了他的官方文档

创建mysql的对应实体类

@Entity(name="bookInfo")
public class Books {

    @Id
    @GeneratedValue
    private Long id;

    private String bookName;
    private String bookImgUrl;

    private String bookTag;
    private String bookPrice;
    private String bookWriter;
    private String bookNum;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

对应数据库

在这里插入图片描述

前端部分

<div id="textInput">
				<br />
			<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
			  <el-form-item label="用户名" prop="name">
				  <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
			  </el-form-item>
			  
			  <el-form-item label="密码" prop="password">
			    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
			  </el-form-item>
			  
			  
			  <el-form-item>
				  <el-button style="background-color: #AAAAAA;" @click="resetForm('ruleForm')">重置</el-button>
			    <el-button style="background-color: #AAAAAA;"  @click="submitForm('ruleForm')">提交</el-button>
			    
			  </el-form-item>
			</el-form>
			</div>
		

js部分


var formMain = {
    data() {

        return {
            ruleForm: {
                bookImgUrl: '',
                bookName: '',
                bookTag:'',
                bookPrice:'',
                bookWriter:'',
                bookNum:''
            },
            rules: {
                bookImgUrl: [
                    { required: true, message: '请输入图书图片地址',trigger:'blur' }
                ],
                bookName: [
                    { required: true, message: '请输入图书名称',trigger:'blur'  }
                ],
                bookTag: [
                    { required: true, message: '请输入图书标签',trigger:'blur'  }
                ],
                bookPrice: [
                    { required: true, message: '请输入图书价格',trigger:'blur'  }
                ],
                bookWriter: [
                    { required: true, message: '请输入图书作者',trigger:'blur'  }
                ],
                bookNum: [
                    { required: true, message: '请输入图书库存',trigger:'blur'  }
                ]
            }
        };
    },
    methods: {
        submitForm(ruleForm) {
            this.$refs[ruleForm].validate(async (valid) => {
                if (valid) {

                    axios.post('/admin/input',this.ruleForm).then((res) =>{
                        console.log(res)
                        console.log(res.config.data)
                        res = JSON.stringify(res.config.data)
                        console.log(res)
                    })

                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(ruleForm) {
            this.$refs[ruleForm].resetFields();
        }
    }

}
var Ctor = Vue.extend(formMain)
new Ctor().$mount('#textInput')

注意 mysql的对应实体类要和ruleForm一一对应
比如 bookImgUrl对应mysql的对应实体类的bookImgUrl

后端数据api

@PostMapping("input")
    public String post(@RequestBody Books books) {

        Books addBooks = bookService.addBook(books);
        return "redirect:/admin/index";
    }

bookService接口

public interface BookService {
    Books addBook(Books books);
}

bookService实现类

@Service
public class BookServiceImpl implements BookService {


    @Autowired
    private BookRepository bookRepository;

    @Transactional
    @Override
    public Books addBook(Books books) {

        books.setCreateTime(new Date());
        books.setUpdateTime(new Date());

        return bookRepository.save(books);
    }
    }

BookRepository

public interface BookRepository extends JpaRepository<Books,Long> {

    Books findByBookName(String bookName);

}

终于搞定,其中也有好多坑,比如字符过长 需要宁外设置数据库

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐