栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java获取 mybatis 新增数据的【自动增长】Id

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java获取 mybatis 新增数据的【自动增长】Id

方法一:
        TeamInfo teamInfo = new TeamInfo();
        teamInfo.setTeamName(context.getTeamName());
        teamInfo.setSource(context.getSource());
        teamInfo.setCreateDatetime(new Date());
        teamInfo.setUpdateDatetime(new Date());
        teamInfo.setDelFlag(CommonConstant.DEL_FLAG_NODEL);
        teamInfoMapper.insert(teamInfo);
        Long id = teamInfo.getId();
        System.out.println("id===" + id);

这样 直接 getId 可以拿到当前的Id

方法二
    
        INSERT INTO team_info (team_name, `source`, create_datetime, update_datetime, del_flag)
        VALUES ('大仙集团', '大仙集团', '2022-06-01 11:32:24', '2022-06-01 11:32:24', '0')

        
            select LAST_INSERT_ID()
        

    

在 新增语句中 加入 其中 keyProperty 是 表 中id 在实体类对应的属性;
order 是在 insert 之前执行 还是之后,一般是之后

对应的Java中

    TeamInfo teamInfo = new TeamInfo();
       Long newId = teamInfoMapper.insertDemo(teamInfo);
        System.out.println("newId===" + teamInfo.getId());

必须传入 实体对象,使用 teamInfo.getId() 获取id;

方法三:
    
        INSERT INTO team_info (team_name, `source`, create_datetime, update_datetime, del_flag)
        VALUES ('大仙集团', '大仙集团', '2022-06-01 11:32:24', '2022-06-01 11:32:24', '0')

    

其中 keyProperty 是 表 中id 在实体类对应的属性;

对应的Java中

    TeamInfo teamInfo = new TeamInfo();
       Long newId = teamInfoMapper.insertDemo(teamInfo);
        System.out.println("newId===" + teamInfo.getId());

必须传入 实体对象,使用 teamInfo.getId() 获取id;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943623.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号