当前位置: 首页> 游戏> 攻略 > 后端MVC三层架构,Mybatis ,雪花算法生成唯一id

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

时间:2025/7/14 13:26:39来源:https://blog.csdn.net/2301_80311013/article/details/141873903 浏览次数:0次

一.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,这里默认都设置为1instance = new UIDGenerator(1, 1);}return instance;}// 生成唯一IDprivate 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位数字的唯一UIDpublic String generateUID() {long id = nextId();return String.format("%08d", id % 100000000);}
}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: