zoukankan      html  css  js  c++  java
  • Mybatis获取自动增长Id

    Mybatis获取自动增长Id

    MyBatis成功插入后获取自动增长的id

    1、向xxMapping.xml配置中加上两个配置。
    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="UserEntity">
    		INSERT INTO USER VALUES(null,#{userName},#{password},#{realName})
    	</insert>
    

    其中keyProperty的值就是数据库中自增长字段名。

    2、在Controller插入方法中,插入成功后,直接通过model的get的方法就能获得自增长的id值。
    @RequestMapping("addUser")
    public String addUser(@ModelAttribute UserEntity userEntity) {
        int i = userService.insertUser(userEntity);//插入记录到数据库,userEntity中没有设置id的值
        String result = "";
        if (i > 0) {
            result = "inster User SUCCESS!!! ID: " + userEntity.getId();//插入成功后,将自增长的id存入到原来的model中,通过get方法就能拿到自增长的id了
        } else {
            result = "inster User FAIL!!!";
        }
        return result;
    }
    
  • 相关阅读:
    React网络请求fetch之get请求
    英语学习
    人物传记
    四月
    启程
    情绪
    办公新址
    孩子大了
    幸福的生日
    hibernate---树状映射
  • 原文地址:https://www.cnblogs.com/desireyang/p/12035329.html
Copyright © 2011-2022 走看看