MybatisPlus+Oracle自动生成id

MybatisPlus+Oracle自动生成id,第1张

MybatisPlus+Oracle自动生成id

目的:使用Oracle的sequence+MybatisPlus的“TableId”枚举,自动填充id字段

以下代码中包含:

1)父类,包含基础字段、TableId、KeySequence的配置以及一些公共方法

2)父类上配置的KeySequence的sql语句

3)业务子类

4)MybatisPlusConfig 的bean配置(该配置的作用是使TableId生效)

  • 父类
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.Random;

import static ms.infrastructure.constant.baseConstant.SYSTEM_NAME;


@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@KeySequence(value ="GLOBAL_SEQ" )
public class AbstractModel implements Serializable {

    private static final long serialVersionUID = 3623141357792435967L;

    @TableId(value = "ID", type = IdType.INPUT)
    private Long id;

    
    @TableField("REMARK")
    private String remark;

    
    @TableField("CREATOR")
    private String creator;

    
    @TableField("CREATED_TIME")
    private Date createdTime;

    
    @TableField("MODIFIER")
    private String modifier;

    
    @TableField("MODIFIED_TIME")
    private Date modifiedTime;

    
    @TableField("VERSION")
    private Integer version;

    
    @TableField("YN")
    private Boolean yn;

    public static void touchForCreate(Object target) {
        if (target instanceof AbstractModel) {
            AbstractModel model = (AbstractModel) target;
            Date now = new Date();
            if (model.getCreatedTime() == null) {
                model.setCreatedTime(now);
            }
            model.setModifiedTime(now);
            model.setVersion(0);
            if (StringUtils.isEmpty(model.getCreator())) {
                model.setCreator(SYSTEM_NAME);
                model.setModifier(SYSTEM_NAME);
            }
            model.setYn(true);
        }
    }

    public static void touchForUpdate(Object target, String modifier) {
        if (target instanceof AbstractModel) {
            AbstractModel model = (AbstractModel) target;
            model.setModifiedTime(new Date());
            model.setModifier(modifier);
            //请注意,更新数据时,若希望使用乐观锁,需要自己写sql来处理version,也就不要调到该方法
            model.setVersion(model.getVersion() + 1);
        }
    }

    protected String generateId() {
        String millis = System.currentTimeMillis() + "";
        return millis.substring(4) + new Random().nextInt(1000);
    }

    public void doModify(String m, int newVersion) {
        modifier = m;
        modifiedTime = new Date();
        version = newVersion;
    }
}
  • sequence语句
--DROP SEQUENCE GLOBAL_SEQ;
CREATE SEQUENCE GLOBAL_SEQ
	INCREMENT BY 1
	START WITH 1
	MAXVALUE 999999999
	CYCLE
  CACHE 100;
  • 业务子类
import ms.manager.base.model.AbstractModel;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@TableName(value = "user_info", autoResultMap = true)
public class UserInfo extends AbstractModel {

    
    @TableField("userid")
    private String userid;

    
    @TableField("name")
    private String name;

    
    @TableField("mobile")
    private String mobile;

    
    @TableField("passWd")
    private String passWd;

    
    @TableField("role")
    private String role;

    
    @TableField("headimgurl")
    private String headimgurl;

    public void generateUid() {
        userid = "U_" + super.generateId();
    }
  • bean配置
import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

    @Bean
    public OracleKeyGenerator oracleKeyGenerator() {
        return new OracleKeyGenerator();
    }
}

测试插入后的效果

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5693546.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存