后端学习报错记录

时间:2024-10-27 18:46:12

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进行注解,标明实体类中的字段到底与数据库中哪个字段一样。

  1. //后端实体类代码
  2. @TableName("work")
  3. @Data
  4. public class Work {
  5. @TableId(type = )
  6. private Integer workId;
  7. private String author;
  8. @TableField("comment_num")
  9. private Integer commentNum;
  10. @TableField("collect_num")
  11. private Integer collectNum;
  12. }
  13. //前端代码
  14. <el-table-column
  15. prop="workId"
  16. label="ID"
  17. width="200"
  18. sortable/>
  19. //一开始写的prop=id,网页上ID就显示不出来,改成和实体类一样后就没问题了

3、前端列表显示图片(24 上传图片文件_哔哩哔哩_bilibili)

首先要在后端写一个上传图片文件的代码。

  1. //接口,service包下
  2. public interface IStorageService {
  3. boolean save(MultipartFile file,String fileName,String filePath);
  4. //文件本身,文件名称,文件位置
  5. }
  1. //具体实现,重新建一个类
  2. @Service
  3. public class StorageServiceImpl implements IStorageService {//实现接口
  4. @Override
  5. public boolean save(MultipartFile file, String fileName, String filePath) {
  6. //将文件存到具体的位置上
  7. String path=filePath+fileName;
  8. File targetFile=new File(path);
  9. if(!().exists()){//判断文件目录是否存在
  10. ().mkdirs();//创建父目录
  11. }
  12. //把文件的内容写入磁盘,是以二进制的形式读取文件然后写入磁盘,springboot提供了非常好的工具类FileCopyUtils
  13. try{
  14. ((),targetFile);
  15. //获取文件的二进制(),写到targetFile中
  16. return true;
  17. //成功写入
  18. }catch (IOException e){
  19. }
  20. return false;
  21. }
  22. }

前端显示图片的代码(vue3)

  1. <el-table-column prop="picture1" label="步骤图1" width="250" >
  2. <template #default="scope">
  3. <img :src=".picture1" class="img"/>
  4. </template>
  5. </el-table-column>
  6. //img是自己定义的样式

4、自动获取创建时间

  1. //获取当前时间
  2. Date date = new Date();
  3. //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
  4. //希望转成的格式: 1999-11-20 10:03:56
  5. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  6. //2. 调用format方法,将Date对象转成字符串
  7. String str = (date);
  8. //3.将字符串转为数据库中的Date类型
  9. sd=(str);
  10. (sd);

5、postman测试报错500

数据库设置的变量名不当,可能与系统内置的变量名发生冲突。比如我设置了一个字段为function,一直报500错误,后来是写一个数据表的字段,测试一个,发现function字段有问题,把它重命名为functions,问题解决。