1、前端表单可以传到后端,即controller可以查看数据,但报500错,数据库无法插入到数据库
2023-03-18 18:16:11.997 ERROR 33136 --- [nio-9090-exec-1] .[.[.[/].[dispatcherServlet] : () for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is :
### Error updating database. Cause: : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/user ( username,
password,
mobile,
create_time ) VALUES ( '124',
'12',
'123'' at line 1
### The error may exist in com/example/demo/mapper/ (best guess)
### The error may involve -Inline
### The error occurred while setting parameters
### SQL: INSERT INTO /user ( username, password, mobile, create_time ) VALUES ( ?, ?, ?, ? )
### Cause: : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/user ( username,
password,
mobile,
create_time ) VALUES ( '124',
'12',
'123'' at line 1
; bad SQL grammar []; nested exception is : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/user ( username,
password,
mobile,
create_time ) VALUES ( '124',
'12',
'123'' at line 1] with root cause
: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/user ( username,
password,
mobile,
create_time ) VALUES ( '124',
'12',
'123'' at line 1
解决方法:将User类中@TableName("/user")中的/删去
2、前端读取数据库数据时,有些字段能读出来,有些读不出来

像这里,其他字段的数据都获取到了只有ID的值没有显示,原因是不仅后端实体类中的字段名要与数据库中的字段名一一对应,前端定义的字段名也必须与后端实体类中的字段名一致。
如果后端实体类中的字段名与数据库中的字段名不一致,可以使用@TableField进行注解,标明实体类中的字段到底与数据库中哪个字段一样。
-
//后端实体类代码
-
@TableName("work")
-
@Data
-
public class Work {
-
@TableId(type = )
-
private Integer workId;
-
private String author;
-
@TableField("comment_num")
-
private Integer commentNum;
-
@TableField("collect_num")
-
private Integer collectNum;
-
}
-
//前端代码
-
<el-table-column
-
prop="workId"
-
label="ID"
-
width="200"
-
sortable/>
-
//一开始写的prop=id,网页上ID就显示不出来,改成和实体类一样后就没问题了
3、前端列表显示图片(24 上传图片文件_哔哩哔哩_bilibili)

首先要在后端写一个上传图片文件的代码。
-
//接口,service包下
-
public interface IStorageService {
-
boolean save(MultipartFile file,String fileName,String filePath);
-
//文件本身,文件名称,文件位置
-
}
-
//具体实现,重新建一个类
-
@Service
-
public class StorageServiceImpl implements IStorageService {//实现接口
-
@Override
-
public boolean save(MultipartFile file, String fileName, String filePath) {
-
//将文件存到具体的位置上
-
String path=filePath+fileName;
-
File targetFile=new File(path);
-
if(!().exists()){//判断文件目录是否存在
-
().mkdirs();//创建父目录
-
}
-
//把文件的内容写入磁盘,是以二进制的形式读取文件然后写入磁盘,springboot提供了非常好的工具类FileCopyUtils
-
try{
-
((),targetFile);
-
//获取文件的二进制(),写到targetFile中
-
return true;
-
//成功写入
-
}catch (IOException e){
-
-
}
-
return false;
-
}
-
}
前端显示图片的代码(vue3)
-
<el-table-column prop="picture1" label="步骤图1" width="250" >
-
<template #default="scope">
-
<img :src=".picture1" class="img"/>
-
</template>
-
</el-table-column>
-
//img是自己定义的样式
4、自动获取创建时间
-
//获取当前时间
-
Date date = new Date();
-
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
-
//希望转成的格式: 1999-11-20 10:03:56
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-
//2. 调用format方法,将Date对象转成字符串
-
String str = (date);
-
//3.将字符串转为数据库中的Date类型
-
sd=(str);
-
(sd);
5、postman测试报错500
数据库设置的变量名不当,可能与系统内置的变量名发生冲突。比如我设置了一个字段为function,一直报500错误,后来是写一个数据表的字段,测试一个,发现function字段有问题,把它重命名为functions,问题解决。