tk.mybatis sqlserver increment primary key data insertion fails

Error information:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: when IDENTITY_INSERT is set to OFF, not insert explicit value table 'mu_kit' identity column.

Solution:

  1. Plus @column provided insertable = false insert may be used conventional methods.

eg.

    @Id
    @Column(insertable = false,name = "id")
    @GeneratedValue(generator = "JDBC")
    private Integer id;
  1. Sqlserver with special insert statement, i.e., the following method id removed insertTest
 <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 statement corresponding to the NULL check added, i.e. only insert data field is not null value.
insert will be inserted in all fields, will insert null.

Published 152 original articles · won praise 18 · views 70000 +

Guess you like

Origin blog.csdn.net/tanhongwei1994/article/details/102744597