mysql - spring mybatis 配置批处理后insert 不能获取主键的问题?
阿神
阿神 2017-04-17 13:46:24
[MySQL讨论组]

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?

阿神
阿神

闭关修行中......

全部回复(2)
PHP中文网
 <!--更新采用批量模式 -->  
        <constructor-arg index="1" value="BATCH"/>  

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

PHP中文网

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号