定义主键自增
更改标识规范改为是,就完成了主键自增的设置
插入操作
对于此表
insert into Person(id,tname,age) values (#{id},#{tname},#{age})
//报错
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'Person' 中的标识列插入显式值。
### The error may exist in cn/mapper/PersonMapper.xml
### The error may involve cn.mapper.PersonMapper.InsertPerson-Inline
### The error occurred while setting parameters
### SQL: /*set IDENTITY_INSERT Person ON*/ insert into Person(id,tname,age) values (?,?,?) /*set IDENTITY_INSERT Person OFF*/
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'Person' 中的标识列插入显式值。
//或者
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 不能将值 NULL 插入列 'id',表 'tan.dbo.Person';列不允许有 Null 值。INSERT 失败。
### The error may exist in cn/mapper/PersonMapper.xml
### The error may involve cn.mapper.PersonMapper.InsertPerson-Inline
### The error occurred while setting parameters
### SQL: set IDENTITY_INSERT Person ON insert into Person(id,tname,age) values (?,?,?) set IDENTITY_INSERT Person OFF
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 不能将值 NULL 插入列 'id',表 'tan.dbo.Person';列不允许有 Null 值。INSERT 失败。
正确的自增插入语句
insert into Person(tname,age) values (#{tname},#{age})
不需要自增字段
关于插入返回id值的配置
<!--
xml方式
useGeneratedKeys="true" 开启自动返回主键id
keyProperty="id" 实体属性名称
keyColumn="id" 数据库表字段名称
-->
<insert id="InsertPerson" parameterType="Person" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
<--注解方式-->
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
id值就返回到实例中