后端MVC三层架构,Mybatis ,雪花算法生成唯一id

一.MVC

MVC(Model View Controller),它是一种思想,他把软件系统分为 以下三部分:

Model(模型):用来处理程序中数据逻辑的部分(service,dao层)

View(视图):在应用程序中,专门和浏览器进行交互,展示数据的资源(前端)

Contreller(控制器):可以理解成是一个分发器,来决定对于视图发来的请求,需要用哪一个模型来处理,以及处理完后需要跳回到哪一个视图,也就是用来连接视图和模型的(收请求,返回响应)

这里的项目结构清晰明了,controller层接受到前端发来的请求,然后进行处理调用service层中的方法,进行执行业务逻辑(进行操作数据库的访问),最后返回controller层,进行返回对象状态(数据或则状态)

二.mybatis框架执行数据库操作(首先需要配置号mybatis的xml)

使用一个mapper映射,在接口中进行定义数据库中的执行方法,并通过注解进行操作数据库

1:需要将mapper配置文件进行储存到同一目录,并且名字要相同。

(mybatis中可以使用注解或者利用xml文件进行操作数据库)

2.在mapper映射文件中进行操作数据库(利用注解)

 三.雪花算法进行计算唯一uid

这里包装了一个工具类

package com.csdn.util;

public class UIDGenerator {

    // Snowflake 算法参数
    private final static long twepoch = 1288834974657L;
    private final static long workerIdBits = 5L;
    private final static long datacenterIdBits = 5L;
    private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
    private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
    private final static long sequenceBits = 12L;

    private final static long workerIdShift = sequenceBits;
    private final static long datacenterIdShift = sequenceBits + workerIdBits;
    private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
    private final static long sequenceMask = -1L ^ (-1L << sequenceBits);

    private long workerId;
    private long datacenterId;
    private long sequence = 0L;
    private long lastTimestamp = -1L;

    // 单例模式,确保工具类只有一个实例
    private static UIDGenerator instance;

    private UIDGenerator(long workerId, long datacenterId) {
        if (workerId > maxWorkerId || workerId < 0) {
            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
        }
        if (datacenterId > maxDatacenterId || datacenterId < 0) {
            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
        }
        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }

    // 获取唯一实例
    public static synchronized UIDGenerator getInstance() {
        if (instance == null) {
            // 在初始化时指定workerId和datacenterId,这里默认都设置为1
            instance = new UIDGenerator(1, 1);
        }
        return instance;
    }

    // 生成唯一ID
    private synchronized long nextId() {
        long timestamp = timeGen();

        if (timestamp < lastTimestamp) {
            throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }

        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0L;
        }

        lastTimestamp = timestamp;

        return ((timestamp - twepoch) << timestampLeftShift) |
               (datacenterId << datacenterIdShift) |
               (workerId << workerIdShift) |
               sequence;
    }

    private long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    private long timeGen() {
        return System.currentTimeMillis();
    }

    // 公共方法:生成并返回8位数字的唯一UID
    public String generateUID() {
        long id = nextId();
        return String.format("%08d", id % 100000000);
    }
}

使用Mybatis-Plus生成雪花算法生成id非常简单。Mybatis-Plus已经内置了雪花算法生成分布式唯一id的功能。你可以在IDEA中双击shift搜索Sequence类来查看具体的实现代码。这个类使用的就是雪花算法生成id。关于如何在项目中使用雪花算法生成id,你可以参考CSDN上的一篇博文《mybatis-plus雪花算法增强idworker》。这篇博文详细介绍了如何在Mybatis-Plus中配置和使用雪花算法生成id。你可以按照这篇博文的步骤进行操作,非常简单易懂。总结起来,使用Mybatis-Plus生成雪花算法生成id的步骤包括建表、新建测试工程和单元测试等。在实现分析中,你可以了解到为什么Mybatis-Plus默认就是使用雪花算法生成id。此外,你还可以通过主动设置id生成策略来使用Mybatis-Plus生成雪花算法生成idMybatis-Plus还提供了内置的雪花算法工具类IdWorker,方便你在项目中使用雪花算法生成id。希望这些信息对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [mybatis-plus雪花算法生成Id使用详解](https://blog.csdn.net/w1014074794/article/details/125604191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值