spring boot——关于一个Mysql主键的问题
问题是这样的:
我现在有一个被@Entity标记的类TimeLine,其中id为主键。
TimeLineController中有一个接收post请求的add()方法,这个方法会接受客户端传来的一个表单,表单中的数据是TimeLine的各个属性。
第一种情况,我的表单中带有id这个属性,这样写入数据库中的一条timeline,id并不是我表单传来的id,感觉像是数据库自己分配的。
第二种情况,我我的表单中没有id这个属性,这会返回一个错误。
TimeLine.java
import javax.persistence.*; @Entity
@Table(name = "timeline")
public class TimeLine { @Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String cotent;
@Column(nullable = false)
private long authorId; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getCotent() {
return cotent;
} public void setCotent(String cotent) {
this.cotent = cotent;
} public long getAuthorId() {
return authorId;
} public void setAuthorId(long authorId) {
this.authorId = authorId;
}
}
TimeLineRepository.java
import com.springboot.first.entity.TimeLine;
import org.springframework.data.jpa.repository.JpaRepository; public interface TimeLineRepository extends JpaRepository<TimeLine, Long> {
TimeLine findById(long id);
}
TimeLineServiceImpl.java
import com.springboot.first.entity.TimeLine;
import com.springboot.first.netUtil.Response;
import com.springboot.first.netUtil.ResponseCode;
import com.springboot.first.netUtil.ResponseTools;
import com.springboot.first.repository.TimeLineRepository;
import com.springboot.first.service.TimeLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.HashMap;
import java.util.List; @Service
public class TimeLineServiceImpl implements TimeLineService { @Autowired
private TimeLineRepository timeLineRepository; @Override
public Response getTimeLineList() {
try {
List<TimeLine> list = timeLineRepository.findAll();
HashMap<String, Object> data = new HashMap<>();
data.put("content", list);
return ResponseTools.response(ResponseCode.SUCCESS,
"", data);
} catch (Exception e) {
return ResponseTools.response(ResponseCode.
Exception_ERROR, e.getMessage(), null);
}
} @Override
public Response selectTimeLineById(long id) {
if(id == 0){
return ResponseTools.response(ResponseCode.PARAM_ERROR,
"", null);
}try{
TimeLine timeLine = timeLineRepository.findById(id);
HashMap<String, Object> data = new HashMap<>();
data.put("content", timeLine);
return ResponseTools.response(ResponseCode.SUCCESS,
"success", data);
}catch (Exception e){
return ResponseTools.response(ResponseCode.
Exception_ERROR, e.getMessage(), null);
}
} @Override
public Response save(TimeLine timeLine) {
if (null == timeLine) {
return ResponseTools.response(ResponseCode.PARAM_ERROR,
"", null);
}
try {
timeLineRepository.save(timeLine);
return ResponseTools.response(ResponseCode.SUCCESS,
"success", null);
} catch (Exception e) {
return ResponseTools.response(ResponseCode.
Exception_ERROR, e.getMessage(), null);
}
} @Override
public Response delete(Long id) {
if(null == id){
return ResponseTools.response(ResponseCode.PARAM_ERROR,
"", null);
}
try{
timeLineRepository.deleteById(id);
return ResponseTools.response(ResponseCode.SUCCESS,
"success", null);
}catch (Exception e){
return ResponseTools.response(ResponseCode.
Exception_ERROR, e.getMessage(), null);
} }
}
TimeLineController.java
import com.springboot.first.entity.TimeLine;
import com.springboot.first.netUtil.Response;
import com.springboot.first.service.TimeLineService;
import org.springframework.web.bind.annotation.*; import javax.annotation.Resource;
import java.util.HashMap; @RestController
@RequestMapping("/timeLine")
public class TimeLineController { @Resource
private TimeLineService timeLineService; @RequestMapping("/list")
public Response getTimeLineList(){
return timeLineService.getTimeLineList();
} @RequestMapping("/get/{id}")
public Response selectTimeLineById(@PathVariable("id") Long id){
return timeLineService.selectTimeLineById(id);
} @RequestMapping(value = "/add", method = RequestMethod.POST)
public Response add(@RequestBody HashMap<String, Object> reqMap){
TimeLine timeLine = new TimeLine();
timeLine.setAuthorId(Long.valueOf((Integer)reqMap.get("authorId")));
timeLine.setCotent((String)reqMap.get("content"));
// timeLine.setId(Long.valueOf((Integer)reqMap.get("id")));
timeLine.setTitle((String)reqMap.get("title"));
return timeLineService.save(timeLine);
} @RequestMapping(value = "/edit", method = RequestMethod.POST)
public Response edit(@RequestBody HashMap<String, Object> reqMap){
TimeLine timeLine = new TimeLine();
timeLine.setAuthorId((Long)reqMap.get("authorId"));
timeLine.setCotent((String)reqMap.get("content"));
timeLine.setId((Long)reqMap.get("id"));
timeLine.setTitle((String)reqMap.get("title"));
return timeLineService.save(timeLine);
} @RequestMapping("/delete/{id}")
public Response delete(@PathVariable Long id){
return timeLineService.delete(id);
}
}
第二种情况页面返回的错误:

解决方法:dao换成了使用原始sql...
spring boot——关于一个Mysql主键的问题的更多相关文章
- Spring boot JPA 用自定义主键策略 生成自定义主键ID
最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...
- MySQL主键设计
[TOC] 在项目过程中遇到一个看似极为基础的问题,但是在深入思考后还是引出了不少问题,觉得有必要把这一学习过程进行记录. MySQL主键设计原则 MySQL主键应当是对用户没有意义的. MySQL主 ...
- spring cloud教程之使用spring boot创建一个应用
<7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...
- (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...
- MYSQL主键自动增加的配置及auto_increment注意事项
文章一 原文地址: http://ej38.com/showinfo/mysql-202971.html 文章二: 点击转入第二篇文章 在数据库应用,我们经常要用到唯一编号.在MySQL中可通过字 ...
- 获得自动增长的MySQL主键
下面的脚本教您如何获得自动增长的MySQL主键,如果您对MySQL主键方面感兴趣的话,不妨一看,相信对您学习MySQL主键方面会有所启迪. import java.sql.Connection; im ...
- Mysql主键索引、唯一索引、普通索引、全文索引、组合索引的区别
原文:Mysql主键索引.唯一索引.普通索引.全文索引.组合索引的区别 Mysql索引概念: 说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不 ...
- mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY'
mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY' 解决方法: 在my.cnf的[mysql ...
- 如何基于Spring Boot搭建一个完整的项目
前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...
随机推荐
- Ubuntu14.04文件目录说明
一.Dev设备目录 二.etc配置文件目录 三.bin默认程序安装目录 四.boot系统启动用到的配置文件以及内核镜像 五.home用户目录 六.lib库文件目录 七.media系统自动挂载设备会选择 ...
- Luogu 2470 [SCOI2007]压缩
和Luogu 4302 [SCOI2003]字符串折叠 差不多的想法,区间dp 为了计算方便,我们可以假设区间[l, r]的前面放了一个M,设$f_{i, j, 0/1}$表示区间$[i, j]$中是 ...
- ubuntu16.04.2安装tftp服务器
1.安装 $ apt-get install tftp-hpa tftpd-hpa tftp-hpa是客户端 tftpd-hpa是服务器 2.创建目录 $ mkdir /tftpboot 这是创建tf ...
- Android ExpandableListView的使用
一.MainActivity要继承ExpandableListActivity.效果是当单击ListView的子项是显示另一个ListView. package com.example.explear ...
- 国内物联网平台(1):百度物接入IoT Hub
国内物联网平台(1) ——百度物接入IoT Hub 马智 物接入IoT Hub - 架构 全托管的云服务,帮助建立设备与云端之间安全可靠的双向连接 支撑海量设备的数据收集.监控.故障预测等各种物联网场 ...
- 通过委托更新UI(异步加载)
来自:http://blog.csdn.net/gongzhe2011/article/details/27351853 using System.Windows.Forms; using Syste ...
- winfrom浏览器控件
(1)webbrowser 在ie的基础上开发出来的,一般情况下很好用,特殊情况下没法用,一堆坑,h5支持效果不好 使用:直接拖控件就好了 (2)WebKit .NET http://webkitdo ...
- Android下创建一个输入法
输入法是一种可以让用户输入文字的控件.Android提供了一套可扩展的输入法框架,使得应用程序可以让用户选择各种类型的输入法,比如基于触屏的键盘输入或者基于语音.当安装了特定输入法之后,用户即可在系统 ...
- Pandas——读取csv,txt文件
""" 读取csv文件 该文本中的分割符既有空格又有制表符(‘/t’),sep参数用‘/s+’,可以匹配任何空格. """ import p ...
- Win10每次开机总是自动弹出MSN网址导航如何取消
Win10每次开机总是自动弹出MSN网址导航如何取消 近来有用户在升级Win10系统后,每次开机总是会自动弹出MSN中文网的网址导航.如果不想要开机打开MSN网址导航,那么应该怎么设置来取消呢?对此, ...