MYBATIS Generatedkey Tag 在 Spring MVC 中似乎不起作用
Posted
技术标签:
【中文标题】MYBATIS Generatedkey Tag 在 Spring MVC 中似乎不起作用【英文标题】:MYBATIS Generatedkey Tag doesn't seem to work in Spring MVC 【发布时间】:2019-08-03 05:03:02 【问题描述】://我使用spring mvc、mybatis和hsql来实现一个CRUD操作
//用于生成 id 我尝试使用 Generated value 标签,但它似乎不是 //已生成
我是 mybatis 的新手,应该将此作为我的实习生项目 pklease help 提交
<insert id='addEmp1' parameterType='Employee'
useGeneratedKeys='true' keyProperty='E_ID'>
INSERT INTO "PUBLIC"."EMPLOYEE"(
"ADDRESS",
"AGE",
"CITY",
"DEPT",
"FNAME",
"LNAME",
"SALARY",
"STATE")
values(
#address,
#age,
#city,
#dept,
#fname,
#lname,
#salary,
#state)
</insert>
//Bean类
public class Employee
public Employee()
// TODO Auto-generated constructor stub
private Long e_id;
@NotBlank(message = "Name is Mandatory")
private String fname;
private String lname;
@NotNull(message = "Age is Mandatory")
@Range(min = 18, max = 99, message = "Age should be between 18 and 99")
private Integer age;
@NotNull(message = "salary may not be empty")
@Range(min = 1)
private Integer salary;
@NotBlank(message = "")
@NotEmpty(message = "Select one Department")
private String dept;
@NotBlank(message = "Select atleast one state")
private String state;
@NotBlank(message = "Select atleast one City")
private String city;
@NotEmpty(message = "At least one Skill is required")
@Valid
@ElementCollection(fetch = FetchType.EAGER)
private List<String> skills = new ArrayList<String>();
private String address;
//stacktrace 显示 id 为空(应该是自动生成的)
### Error updating database. Cause:
java.sql.SQLIntegrityConstraintViolationException: integrity constraint
violation: NOT NULL check constraint; SYS_CT_10226 table: EMPLOYEE column:
E_ID
### The error may exist in com/jamocha/DAO/EmployeeMapper.xml
### The error may involve com.jamocha.DAO.EmployeeDAO.addEmp1-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO "PUBLIC"."EMPLOYEE"( "ADDRESS", "AGE", "CITY",
"DEPT", "FNAME","LNAME", "SALARY", "STATE") values(?,?,?,?,?,?,?,?)
### Cause: java.sql.SQLIntegrityConstraintViolationException: integrity
constraint violation: NOT NULL check constraint; SYS_CT_10226 table:
EMPLOYEE column: E_ID] with root cause
java.sql.SQLIntegrityConstraintViolationException: integrity constraint
violation: NOT NULL check constraint; SYS_CT_10226 table: EMPLOYEE column:
E_ID
【问题讨论】:
【参考方案1】:我认为问题是由插入标签中的属性引起的。请尝试以下映射
<insert id='addEmp1' parameterType='Employee' useGeneratedKeys='true'
keyProperty='e_id' keyColumn='E_ID'>
INSERT INTO "PUBLIC"."EMPLOYEE"(
"ADDRESS",
"AGE",
"CITY",
"DEPT",
"FNAME",
"LNAME",
"SALARY",
"STATE")
values(
#address,
#age,
#city,
#dept,
#fname,
#lname,
#salary,
#state)
</insert>
请注意keyProperty(区分大小写)和keyColumn属性的变化。 我的信息请参考http://www.mybatis.org/mybatis-3/sqlmap-xml.html#insert.2C_update_and_delete了解更多详情。
【讨论】:
以上是关于MYBATIS Generatedkey Tag 在 Spring MVC 中似乎不起作用的主要内容,如果未能解决你的问题,请参考以下文章