问题是这样的:

  我现在有一个被@Entity标记的类TimeLine,其中id为主键。
  TimeLineController中有一个接收post请求的add()方法,这个方法会接受客户端传来的一个表单,表单中的数据是TimeLine的各个属性。
  第一种情况,我的表单中带有id这个属性,这样写入数据库中的一条timeline,id并不是我表单传来的id,感觉像是数据库自己分配的。
  第二种情况,我我的表单中没有id这个属性,这会返回一个错误。

TimeLine.java

  1. import javax.persistence.*;
  2.  
  3. @Entity
  4. @Table(name = "timeline")
  5. public class TimeLine {
  6.  
  7. @Id
  8. @GeneratedValue
  9. private Long id;
  10. @Column(nullable = false)
  11. private String title;
  12. @Column(nullable = false)
  13. private String cotent;
  14. @Column(nullable = false)
  15. private long authorId;
  16.  
  17. public long getId() {
  18. return id;
  19. }
  20.  
  21. public void setId(long id) {
  22. this.id = id;
  23. }
  24.  
  25. public String getTitle() {
  26. return title;
  27. }
  28.  
  29. public void setTitle(String title) {
  30. this.title = title;
  31. }
  32.  
  33. public String getCotent() {
  34. return cotent;
  35. }
  36.  
  37. public void setCotent(String cotent) {
  38. this.cotent = cotent;
  39. }
  40.  
  41. public long getAuthorId() {
  42. return authorId;
  43. }
  44.  
  45. public void setAuthorId(long authorId) {
  46. this.authorId = authorId;
  47. }
  48. }

TimeLineRepository.java

  1. import com.springboot.first.entity.TimeLine;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3.  
  4. public interface TimeLineRepository extends JpaRepository<TimeLine, Long> {
  5. TimeLine findById(long id);
  6. }

TimeLineServiceImpl.java

  1. import com.springboot.first.entity.TimeLine;
  2. import com.springboot.first.netUtil.Response;
  3. import com.springboot.first.netUtil.ResponseCode;
  4. import com.springboot.first.netUtil.ResponseTools;
  5. import com.springboot.first.repository.TimeLineRepository;
  6. import com.springboot.first.service.TimeLineService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9.  
  10. import java.util.HashMap;
  11. import java.util.List;
  12.  
  13. @Service
  14. public class TimeLineServiceImpl implements TimeLineService {
  15.  
  16. @Autowired
  17. private TimeLineRepository timeLineRepository;
  18.  
  19. @Override
  20. public Response getTimeLineList() {
  21. try {
  22. List<TimeLine> list = timeLineRepository.findAll();
  23. HashMap<String, Object> data = new HashMap<>();
  24. data.put("content", list);
  25. return ResponseTools.response(ResponseCode.SUCCESS,
  26. "", data);
  27. } catch (Exception e) {
  28. return ResponseTools.response(ResponseCode.
  29. Exception_ERROR, e.getMessage(), null);
  30. }
  31. }
  32.  
  33. @Override
  34. public Response selectTimeLineById(long id) {
  35. if(id == 0){
  36. return ResponseTools.response(ResponseCode.PARAM_ERROR,
  37. "", null);
  38. }try{
  39. TimeLine timeLine = timeLineRepository.findById(id);
  40. HashMap<String, Object> data = new HashMap<>();
  41. data.put("content", timeLine);
  42. return ResponseTools.response(ResponseCode.SUCCESS,
  43. "success", data);
  44. }catch (Exception e){
  45. return ResponseTools.response(ResponseCode.
  46. Exception_ERROR, e.getMessage(), null);
  47. }
  48. }
  49.  
  50. @Override
  51. public Response save(TimeLine timeLine) {
  52. if (null == timeLine) {
  53. return ResponseTools.response(ResponseCode.PARAM_ERROR,
  54. "", null);
  55. }
  56. try {
  57. timeLineRepository.save(timeLine);
  58. return ResponseTools.response(ResponseCode.SUCCESS,
  59. "success", null);
  60. } catch (Exception e) {
  61. return ResponseTools.response(ResponseCode.
  62. Exception_ERROR, e.getMessage(), null);
  63. }
  64. }
  65.  
  66. @Override
  67. public Response delete(Long id) {
  68. if(null == id){
  69. return ResponseTools.response(ResponseCode.PARAM_ERROR,
  70. "", null);
  71. }
  72. try{
  73. timeLineRepository.deleteById(id);
  74. return ResponseTools.response(ResponseCode.SUCCESS,
  75. "success", null);
  76. }catch (Exception e){
  77. return ResponseTools.response(ResponseCode.
  78. Exception_ERROR, e.getMessage(), null);
  79. }
  80.  
  81. }
  82. }

TimeLineController.java

  1. import com.springboot.first.entity.TimeLine;
  2. import com.springboot.first.netUtil.Response;
  3. import com.springboot.first.service.TimeLineService;
  4. import org.springframework.web.bind.annotation.*;
  5.  
  6. import javax.annotation.Resource;
  7. import java.util.HashMap;
  8.  
  9. @RestController
  10. @RequestMapping("/timeLine")
  11. public class TimeLineController {
  12.  
  13. @Resource
  14. private TimeLineService timeLineService;
  15.  
  16. @RequestMapping("/list")
  17. public Response getTimeLineList(){
  18. return timeLineService.getTimeLineList();
  19. }
  20.  
  21. @RequestMapping("/get/{id}")
  22. public Response selectTimeLineById(@PathVariable("id") Long id){
  23. return timeLineService.selectTimeLineById(id);
  24. }
  25.  
  26. @RequestMapping(value = "/add", method = RequestMethod.POST)
  27. public Response add(@RequestBody HashMap<String, Object> reqMap){
  28. TimeLine timeLine = new TimeLine();
  29. timeLine.setAuthorId(Long.valueOf((Integer)reqMap.get("authorId")));
  30. timeLine.setCotent((String)reqMap.get("content"));
  31. // timeLine.setId(Long.valueOf((Integer)reqMap.get("id")));
  32. timeLine.setTitle((String)reqMap.get("title"));
  33. return timeLineService.save(timeLine);
  34. }
  35.  
  36. @RequestMapping(value = "/edit", method = RequestMethod.POST)
  37. public Response edit(@RequestBody HashMap<String, Object> reqMap){
  38. TimeLine timeLine = new TimeLine();
  39. timeLine.setAuthorId((Long)reqMap.get("authorId"));
  40. timeLine.setCotent((String)reqMap.get("content"));
  41. timeLine.setId((Long)reqMap.get("id"));
  42. timeLine.setTitle((String)reqMap.get("title"));
  43. return timeLineService.save(timeLine);
  44. }
  45.  
  46. @RequestMapping("/delete/{id}")
  47. public Response delete(@PathVariable Long id){
  48. return timeLineService.delete(id);
  49. }
  50. }

第二种情况页面返回的错误:

解决方法:dao换成了使用原始sql...

spring boot——关于一个Mysql主键的问题的更多相关文章

  1. Spring boot JPA 用自定义主键策略 生成自定义主键ID

    最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...

  2. MySQL主键设计

    [TOC] 在项目过程中遇到一个看似极为基础的问题,但是在深入思考后还是引出了不少问题,觉得有必要把这一学习过程进行记录. MySQL主键设计原则 MySQL主键应当是对用户没有意义的. MySQL主 ...

  3. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

  4. (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

    大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...

  5. MYSQL主键自动增加的配置及auto_increment注意事项

    文章一 原文地址: http://ej38.com/showinfo/mysql-202971.html 文章二:   点击转入第二篇文章 在数据库应用,我们经常要用到唯一编号.在MySQL中可通过字 ...

  6. 获得自动增长的MySQL主键

    下面的脚本教您如何获得自动增长的MySQL主键,如果您对MySQL主键方面感兴趣的话,不妨一看,相信对您学习MySQL主键方面会有所启迪. import java.sql.Connection; im ...

  7. Mysql主键索引、唯一索引、普通索引、全文索引、组合索引的区别

    原文:Mysql主键索引.唯一索引.普通索引.全文索引.组合索引的区别 Mysql索引概念: 说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不 ...

  8. mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry &#39;xxx&#39; for key &#39;PRIMARY&#39;

    mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY' 解决方法: 在my.cnf的[mysql ...

  9. 如何基于Spring Boot搭建一个完整的项目

    前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...

随机推荐

  1. 数字图像处理实验(6):PROJECT 04-02,Fourier Spectrum and Average Value 标签: 图像处理MATLABfft 2017-05-07 23:1

    实验要求: Objective: To observe the Fourier spectrum by FFT and the average value of an image. Main requ ...

  2. Luogu 2149 [SDOI2009]Elaxia的路线

    感觉这题可以模板化. 听说spfa死了,所以要练堆优化dijkstra. 首先对$x_{1},y_{1},x_{2},y_{2}$各跑一遍最短路,然后扫一遍所有边看看是不是同时在两个点的最短路里面,如 ...

  3. WOJ 43 电话邀请

    并查集缩点这个trick感觉明明用得很广泛,为什么以前都不知道…… 先把$m$条线路从小到大排个序,这样可以保证之前合并出来的一定是最小的,大的代价不会把小的覆盖掉. 维护两个并查集,一个用来缩点,另 ...

  4. getline()函数的两种用法

    getline()函数的输入流对象可以是标准输入流对象cin,也可以是一个文件输入流对象fin; (1)输入流对象的成员函数(有三个参数,一般除非需要自己选定停止符,并不推荐使用): basic_is ...

  5. Ubuntu16安装GTK+2.0教程

    Step 1 修改清华源(修改完可提高下载速度) 先运行 sudo gedit /etc/apt/sources.list 替换文本内容,保存,退出. # 默认注释了源码镜像以提高 apt updat ...

  6. 数据结构 elegant_sequence(优雅的序列)

    数据结构 elegant_sequence(优雅的序列) 问题描述 如果一个序列的元素的异或和等于 1,我们称这个序列为优雅的序列.现在给你一个 01 序列,和 m 次询问.对于每次询问,给出 l,r ...

  7. 【Python OpenGL】【2】第一个三角形(Pyopengl)

    根据顶点缓存来生成图元(Python OpenGL) 原文(英文链接)http://ogldev.atspace.co.uk/www/tutorial03/tutorial03.html __auth ...

  8. P1082 同余方程

    题意:给定a,b,求$ax \equiv 1 \pmod b$的最小正整数解x,保证有解 exgcd:求$ax+by=gcd(a,b)$的 一组解x,y 首先根据正常的gcd可得出   $gcd(a, ...

  9. Luogu5324 BJOI2019删数(线段树)

    考虑无修改怎么做.对于1~n的每个数,若其存在,将最后一个放在其值的位置,剩余在其前面依次排列,答案即为值域1~n上没有数的位置个数.带修改显然记一下偏移量线段树改一改就好了. #include< ...

  10. STS(spring tool suite)修改默认编码

    安装STS后首先要做的修改默认编码: 1.windows--perferences--general--workspace,Text file encoding设置成utf-8 2.windows-- ...