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

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?


使用spring-batch mybatis可实现


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

【热门文章】
【热门文章】