从早上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);
}
终于搞定,其中也有好多坑,比如字符过长 需要宁外设置数据库
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/133863.html