错误信息:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 ‘mu_kit’ 中的标识列插入显式值。
解决方法:
- 加@column设置insertable=false也可以使用普通的insert方法。
eg.
@Id
@Column(insertable = false,name = "id")
@GeneratedValue(generator = "JDBC")
private Integer id;
- 用sqlserver专用的insert语句,下面的insertTest方法即把id去掉
<resultMap id="BaseResultMap" type="com.xiaobu.entity.MuKit">
<[email protected] generated on Fri Oct 25 14:18:00 CST 2019.-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="delivery_number" jdbcType="VARCHAR" property="deliveryNumber" />
<result column="mu_number" jdbcType="VARCHAR" property="muNumber" />
<result column="mu_sn" jdbcType="VARCHAR" property="muSn" />
<result column="production_order" jdbcType="VARCHAR" property="productionOrder" />
<result column="running_hours" jdbcType="VARCHAR" property="runningHours" />
</resultMap>
<!--suppress SqlResolve -->
<sql id="Base_Column_List">
<[email protected] generated on Fri Oct 25 14:18:00 CST 2019.-->
create_time, delivery_number, mu_number, mu_sn, production_order, running_hours
</sql>
<!--suppress n -->
<sql id="base_properties_list">
#{createTime,jdbcType=TIMESTAMP},#{deliveryNumber,jdbcType=VARCHAR}, #{muNumber,jdbcType=VARCHAR},#{muSn,jdbcType=VARCHAR},#{productionOrder,jdbcType=VARCHAR},#{runningHours,jdbcType=VARCHAR}
</sql>
<insert id="insertTest" parameterType="com.xiaobu.entity.MuKit" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO mu_kit (<include refid="Base_Column_List"/>)
values (<include refid="base_properties_list"/>)
</insert>
insertSelective对应的sql语句加入了NULL校验,即只会插入数据不为null的字段值。
insert则会插入所有字段,会插入null。