Vue实现表单提交给SpringBoot后台并存入Mysql数据库
从早上7点过一直搞到现在,终于实现Vue实现表单提交给SpringBoot后台并存入Mysql数据库,因为之前没有接触Vue,就昨天看了他的官方文档创建mysql的对应实体类@Entity(name="bookInfo")public class Books {@Id@GeneratedValueprivate Long id;private String bookName;private Str
·
从早上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);
}
终于搞定,其中也有好多坑,比如字符过长 需要宁外设置数据库
更多推荐
已为社区贡献3条内容
所有评论(0)