mybatis 设置ID

2022-03-08  本文已影响0人  _花

ID为整型时

1.使用useGenerateKey

 <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="personId">
        insert into person(name,pswd) values(#{name},#{pswd})
</insert> 

2. 使用select LAST_INSERT_ID()

<insert id="insert" parameterType="Person">
        <selectKey keyProperty="personId" resultType="java.lang.Long">
            select LAST_INSERT_ID()
        </selectKey>
        insert into person(name,pswd) values(#{name},#{pswd})
</insert>

使用select last_insert_id()时要注意,当一次插入多条记录时,只是获得第一次插入的id值,务必注意。
https://www.cnblogs.com/hongdada/p/9956992.html

ID为字符串时

1.使用uuid

 <insert id="insertUser" parameterType="com.baidu.aduhdmap.permission.model.User">
  <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
            select uuid()
    </selectKey>
    insert into acl_user (username, email, memo, is_deleted,
      gmt_create, gmt_modified)
    values (#{username,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},#{memo,jdbcType=VARCHAR}, #{isDeleted,jdbcType=TINYINT},
      #{gmtCreate,jdbcType=TIMESTAMP}, #{gmtModified,jdbcType=TIMESTAMP})
  </insert>

2.使用自定义的方法,在service里生成

/**
     * 新增用户
     *
     * @param user
     * @return
     */
    public  CommonResult addUser(User user){
        Date nowTime = new Date();
        user.setId(BaseUtil.getUid(18));
        user.setGmtCreate(nowTime);
        user.setGmtModified(nowTime);
        user.setIsDeleted(false);
        System.out.println(user);
        Boolean result = userMapper.insertUser(user);
        if(result){
            return CommonResult.success(null,"用户新增成功!");
        }else{
            return CommonResult.failed("用户新增失败!");
        }
    }

BaseUtil文件如下

package com.baidu.aduhdmap.permission.util;

import org.apache.commons.lang3.RandomStringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BaseUtil {
    public final static String BLANK = "";
    public final static String YYMMDDHHMMSS = "yyMMddHHmmss";
    public final static String YYYY_MM_DD = "yyyy-MM-dd";
    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
    public final static String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

    public BaseUtil() {
    }

    /**
     * description : 返回唯一id
     *
     * @param length length
     */
    public static String getUid(int length) {
        if (length >= 12) {
            return getCurrentDate(0, YYMMDDHHMMSS) + RandomStringUtils.random(length - 12, Boolean.FALSE, Boolean.TRUE);
        } else {
            return RandomStringUtils.random(length, Boolean.FALSE, Boolean.TRUE);
        }
    }

    /**
     * description :
     *
     * @param increment increment
     * @param pattern   pattern
     * @author wanzhicheng 2022/3/8
     */
    public static String getCurrentDate(int increment, String pattern) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, increment);
        Date date = c.getTime();
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }
}

上一篇 下一篇

猜你喜欢

热点阅读