自学内容网 自学内容网

useGeneratedKeys=“true“ keyProperty=“id“失效问题

问题描述:

1、id是自增主键

CREATE TABLE IF NOT EXISTS system_organization(
    id bigint(20) NOT NULL AUTO_INCREMENT  COMMENT '主键ID' ,
    pid bigint(20) NOT NULL   COMMENT '父级ID' ,
    label VARCHAR(100) NOT NULL   COMMENT '名称' ,
    level tinyint(1)    COMMENT '层级' ,
    all_pid VARCHAR(4000)    COMMENT '所有父节点id(用英文逗号分隔)' ,
    create_time DATETIME NOT NULL  DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' ,
    update_time DATETIME NOT NULL  DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间' ,
    create_user VARCHAR(50) DEFAULT NULL COMMENT '创建人' ,
    update_user VARCHAR(50) DEFAULT NULL COMMENT '更新人' ,
    PRIMARY KEY (id),
    UNIQUE KEY `index_organization_label`(`label`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='组织架构表';

2、Mapper.xml配置了useGeneratedKeys="true" keyProperty="id"

<insert id="insert" parameterType="com.demo.po.AmcBaseInfoPO"
            useGeneratedKeys="true" keyProperty="id">
        insert into scd_incremental_base_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=DECIMAL},
            </if>
        </trim>
</insert>

3、Dao没有使用@Param

    /**
     * 插入基础数据
     *
     * @param amcBaseInfoPo 数据参数
     * @return
     */
    int insert(AmcBaseInfoPO amcBaseInfoPo);

4、当然,取的是id字段,不是影响行数

    @Transactional(rollbackFor = Exception.class)
    public Long wholeAddNode(OrganizationAddReq addReq) {
        // 插入组织数据
        SystemOrganizationDO insertDO = new SystemOrganizationDO();
        insertDO.setLabel(label);
        insertDO.setLevel(level);
        insertDO.setPid(pid);
        insertDO.setAllPid(allPidStr);
        systemOrganizationDao.insert(insertDO);
        // 这里取不到id
        Long organizationId = insertDO.getId();
        // 事务执行成功以后更新缓存
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            @Override
            public void afterCommit() {
                // TODO 刷新缓存:当前组织的所有下级子节点id集合(包含自身)
            }
        });
        return organizationId;
    }

为啥就是不行呢???

问题解决

突然发现application.yml里面配置了mybatis-config.xml

# mybatis配置参数
mybatis:
  config-location: classpath:mybatis-config.xml
  mapper-locations: classpath:mybatis/**/*Mapper.xml

试着把mybatis-config.xml里面的配置注释掉

芜湖!!!居然可以获取到id了!!!!!

一个一个试过去,发现是因为defaultExecutorType配置为BATCH的情况下,id不会赋值!!

果断改回SIMPLE

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
    <settings>
        <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE(之前设置为BATCH时会导致insert方法不赋值自增id, 改成SIMPLE后能够成功赋值) -->
         <setting name="defaultExecutorType" value="SIMPLE" />
    </settings>
</configuration>

问题原因

略(俺也不知道为啥)


原文地址:https://blog.csdn.net/why_su/article/details/135620506

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!