tk.mybatis sqlserver主键自增数据插入失败

错误信息:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 ‘mu_kit’ 中的标识列插入显式值。

解决方法:

  1. 加@column设置insertable=false也可以使用普通的insert方法。

eg.

    @Id
    @Column(insertable = false,name = "id")
    @GeneratedValue(generator = "JDBC")
    private Integer id;
  1. 用sqlserver专用的insert语句,下面的insertTest方法即把id去掉
 <resultMap id="BaseResultMap" type="com.xiaobu.entity.MuKit">
    <!--@mbg.generated 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">
    <!--@mbg.generated 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。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值