后端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); } }