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...

原文地址:https://www.cnblogs.com/gaoquanquan/p/10257083.html