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 ...
随机推荐
- ruby 数组与散列
def say_goodnight(name) result ="Good night ." +name return result end def say_goodmorning ...
- php学习笔记-默认参数
在定义函数的时候,我们可以把其中的一个参数变的特殊起来,使它有一个默认值,这个参数就叫默认参数.在调用这个函数的时候,你既可以给这个默认参数传递一个值,这样的话默认参数的值会被覆盖掉,也可以不给它传递 ...
- Overloaded的方法是否可以改变返回值的类型
摘要: 重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同) Overload是重载的意思,Override是覆盖的意思,也就是重写. 重载 ...
- CodeForces 384E Propagating tree (线段树+dfs)
题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...
- 美团热更新Robust Demo演示
1.Android Studio clone 远程Robust项目源码 gradle 同步依赖资源,可能需要半个小时左右. 2.生成样例apk包 配置app module下build.gradle 插 ...
- enum枚举型
在实际编程中,有些数据的取值往往是有限的,只能是非常少量的整数,并且最好为每个值都取一个名字,以方便在后续代码中使用,比如一个星期只有七天,一年只有十二个月,一个班每周有六门课程等. 以每周七天为例, ...
- Sql Server将一列字段拼接成字符串方法
最近在项目中遇到个问题,需要将表中某列字段合并成字符串输出,如果直接通过代码全部读取出来,再遍历进行拼接显然不是最好的方法,所以想着能否在数据读取的时候直接拼接好返回,网上搜了可通过for xml来实 ...
- android studio中使用x5 webview来读写cookies的问题
本人新手,刚接触AS也没有多久,记录下两个问题. 1. 怎么在android studio中写入cookies 把写入cookies的动作放在了主界面的onCreate事件中了,看了腾讯的说明,说是要 ...
- c#打开关闭进程
private const string FileName = "test.exe"; //进程名称不带扩展名 private const string ProcessName = ...
- javascript javascript面向对象的理解及简单的示例
javascript面向对象的理解及简单的示例 零.本节重点: 1.封装: 2.继承: 壹.下面理解: 一. javascript面向对象概念: 为了说明 JavaScript 是一门彻底的面向对象的 ...