spring mybatis 配置批处理后insert 不能获取主键的问题?

sql

    <insert id="save" parameterType="com.sifude.entity.SysRole" useGeneratedKeys="true" keyProperty="roleId" flushCache="true" >
        insert into tbl_sys_role (role_id, name, description,
        create_time, record_status)
        values (null, #{name,jdbcType=VARCHAR},
        #{description,jdbcType=VARCHAR},
        CURRENT_TIMESTAMP, #{recordStatus,jdbcType=SMALLINT}
        )
    </insert>

spring-mybatis.xml

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="mapperLocations" value="classpath:com/operation/mapper/*Mapper.xml" />
        <property name="typeAliasesPackage" value="com.operation.entity" />
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage"  value="com.operation.dao"/>
    </bean>
    
    <bean id="batchSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <!--更新采用批量模式 -->  
        <constructor-arg index="1" value="BATCH"/>  
    </bean> 

应该怎么配置insert才能返回key?

阅读 4.7k
2 个回答
 <!--更新采用批量模式 -->  
        <constructor-arg index="1" value="BATCH"/>  

采用了批量更新模式是不能返回主键的,改为普通更新模式就好了。如果用批量更新模式,就是用spring-batch来更新数据。

你插入的role_id的值就是Null
去掉试试,如果插入成功返回的值应该是1,表示插入成功一条记录

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题