之前纠结了好久,新增一直报错:SQL state [99999]; error code [17068]; 调用中的无效参数; nested exception is java.sql.SQLException:
百度mybatis配置useGeneratedKeys=“false” 就行,那mybatis-plus呢怎么配置,没找到,估计是主键的新增策略有关系,并且成功自增,直接上干货.
1.第一种,还是用mybatis-plus
实体类 主键 设置为 自增 ,主键类型为 Long
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.IdType;
@TableId(type = IdType.ASSIGN_ID)
//菜单ID
private Long menu_id;
2.第二种,用sprigdatajpa
import javax.persistence.*;
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer user_id;
maven
<!-- JPA 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
不管用jpa和mybatis-plus,都要在数据库的主键自己弄个新增策略
比如表名是sys_menu 表的主键id是menu_id
那就直接在plsql执行
步骤一::新增一个seq
create sequence sys_menu_seq start with 1 increment by 1;
步骤二,
create or replace trigger sys_menu_trigger
before insert on sys_menu
for each row
begin
select sys_menu_seq.nextval into :new.menu_id from dual;
end ;
然后就等同于mysql中的id主键自增了,
跑一下,执行结果,贴一下传递的参数例子{“menu_name”:“taaaaaatt”,“parent_id”:0,“order_num”:“”,“path”:“/test”,“component”:“Layout”,“query”:“/test”,“is_frame”:“”,“is_cache”:“”,“menu_type”:“”,“visible”:“”,“status”:0,“perms”:“”,“icon”:“list”,“create_by”:“”,“create_time”:“”,“update_by”:“”,“update_time”:“”,“remark”:“”}
![apipost新增的结果](https://img-blog.csdnimg.cn/4d3bc778f75f4d94bef37abcec978e17.png
新增成功