zoukankan      html  css  js  c++  java
  • mybaits返回插入成功后的自增值

    mybaits返回插入成功后的自增值

    在项目中,我们经常遇到这样的情况:insert语句成功后,需要自增的id值,这个时候,我们可以通过mybatis的 useGeneratedKeys 来实现,具体如下:

       <!--新增巡检预案-->
        <insert id="insertVideoFormPolling" useGeneratedKeys="true" keyProperty="pollingPlan.pollingPlanId" parameterType="com.unisits.zngkpt.data.pollingmandata.pojo.PollingPlan" >
          INSERT INTO polling_plan(polling_plan_type,unit_id,polling_plan_name,polling_plan_desc)
          VALUES (1,${pollingPlan.unitId},#{pollingPlan.pollingPlanName},#{pollingPlan.pollingPlanDesc})
        </insert>

    在这里,useGeneratedKeys 代表这个表的id是自增的,keyProperty属性指定,哪列是自增的,那么怎么获取自增的id呢?

    public void addVideoFormPolling(){
    PollingPlan pp = new PollingPlan();
    pp.setPollingPlanName("test23");
    pp.setUnitId(1000);
    pp.setPollingPlanDesc("test");
    int result = pollingVideoDao.insertVideoFormPolling(pp);
    System.out.println(pp.getPollingPlanId()+"--"+result);
    }

    而这里的 pp.getPollingPlanId() 就是获取的返回值的id

  • 相关阅读:
    解决phpmailer可以在windows下面发送成功, 在linux下面失败的问题
    centos安装svn
    linux下面配置安装nodejs+npm
    排序与搜索
    链表
    栈和队列
    顺序表
    初识算法、数据结构
    Linux_02
    Linux_01
  • 原文地址:https://www.cnblogs.com/ningheshutong/p/8126641.html
Copyright © 2011-2022 走看看