mysql存储比特位

2024-05-23 22:52
文章标签 mysql 存储 database 比特

本文主要是介绍mysql存储比特位,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、介绍

二、SQL

CREATE TABLE bits_table (id INT PRIMARY KEY AUTO_INCREMENT,bit_value BIGINT UNSIGNED
);-- 插入一个 8 位的 BIT 值
INSERT INTO bits_table (bit_value) VALUES (B'10101010');-- 查询并格式化输出
SELECT id,bit_value,CONCAT('b', LPAD(BIN(bit_value), 64, '0')) AS formatted_bit_value -- 将 BIGINT 转换为 64 位的二进制字符串
FROM bits_table;

在这个例子中,CONCAT('b', LPAD(BIN(bit_value), 64, '0')) 用于将 bit_value 转换为一个以 'b' 开头的 64 位二进制字符串,LPAD 用于在左边填充 '0' 以达到 64 位的长度。

请注意,如果你需要存储非整数数量的位或者位数不固定,你可能需要以文本形式存储或者使用其他数据库特性来实现。

-- 假设我们有一个表 `bits_table`,其中有一个 `BIGINT` 类型的列 `bigint_col`
-- 我们要修改 `bigint_col` 列的第二位-- 将 `bigint` 转换为 `bit` 字符串,并取得第二位的值
SELECT bigint_col,-- 将 `bigint` 转换为 `bit` 字符串,并取得第二位的值SUBSTRING(BIN(bigint_col), 2, 1) AS second_bit
FROMbits_table;-- 更新第二位为1
UPDATE bits_table
SET bigint_col = -- 将 `bigint` 转换为 `bit` 字符串,将第二位设置为1,然后转换回 `bigint`(CONV(CONCAT(SUBSTRING(BIN(bigint_col), 1, 1), '1', SUBSTRING(BIN(bigint_col), 3)), 2, 10)
WHERE-- 你的条件语句,比如 id = 1id = 1;
<!-- MyBatis的mapper文件 -->
<update id="updateBit">UPDATE your_table_nameSET your_bigint_column = bitor(bitand(your_bigint_column, bnot(1 << 20)), (#{value} << 20))WHERE your_condition
</update>这里使用了两个位运算符:bitand(a, b): 对两个bigint数进行按位与操作。bitor(a, b): 对两个bigint数进行按位或操作。bnot(x): 对bigint数进行按位取反操作,结果是把x的第y位取反。<<: 左移运算符,用于将一个整数左移指定的位数。确保你的mapper接口中有相应的方法:

UPDATE your_table_name
SET your_bigint_column = BIN(CONV(CONV(your_bigint_column, 2, 10) + POW(2, 19 - 1), 2, 10))
WHERE your_condition;your_table_name是你的表名,your_bigint_column是你想要更新的列名,your_condition是你的更新条件。请注意,这个例子中假设了以下几点:你想要将第20位设置为1。你的列是无符号的,因此最高位是第20位。如果是有符号的,请适当调整位数。如果你想将第20位设置为0或某个特定值,只需要将POW(2, 19 - 1)中的1改为你想要设置的值(以二进制表示)。如果是将第20位设置为0,就是POW(2, 19 - 0)

三、demo

1、使用bigint类型存储bit

  总览

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>bit-demo</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.4</version><relativePath/></parent><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--尽量不要同时导入mybatis 和 mybatis_plus,避免版本差异--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.13</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies></project>
server.port=6666
server.servlet.context-path=/bitDemo
#mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3308/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy
#mybatis
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
#
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
1.1、表
create table demo.user_sys_flag
(id      int auto_incrementprimary key,flag    bigint      null,user_id varchar(50) null
);
1.2、dto与枚举
package com.bit.demo.dto;import com.baomidou.mybatisplus.annotation.TableName;
import com.bit.demo.enums.UserSysFlagEnums;
import com.bit.demo.util.BitUtil;
import lombok.Builder;
import lombok.Data;@Data
@Builder
@TableName("user_sys_flag")
public class UserSysFlagDTO {private Integer id;private Long flag;private String userId;public boolean isEnableFlag1(){return BitUtil.isSet(flag,UserSysFlagEnums.FLAG_1.bitPosition);}public boolean isEnableFlag2(){return BitUtil.isSet(flag,UserSysFlagEnums.FLAG_2.bitPosition);}public boolean isEnableFlag60() {return BitUtil.isSet(flag,UserSysFlagEnums.FLAG_60.bitPosition);}
}
package com.bit.demo.enums;public enum UserSysFlagEnums {FLAG_1("flag_1",1L),FLAG_2("flag_2",1L<<1),FLAG_3("flag_3",1L<<2),FLAG_60("flag_60",1L<<59);public final String key;public final Long bitPosition;UserSysFlagEnums(String key, Long bitPosition){this.key = key;this.bitPosition = bitPosition;}
}
1.3、dao
package com.bit.demo.repository;import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bit.demo.dto.UserSysFlagDTO;
import com.bit.demo.mapper.UserSysFlagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;@Repository
public class UserSysFlagRepository {@Autowiredpublic UserSysFlagMapper userSysFlagMapper;public void insert(UserSysFlagDTO userSysFlagDTO) {userSysFlagMapper.insert(userSysFlagDTO);}public UserSysFlagDTO getByUserId(String userId) {LambdaQueryWrapper<UserSysFlagDTO> userQuery = new LambdaQueryWrapper<>();userQuery.eq(UserSysFlagDTO::getUserId,userId);return userSysFlagMapper.selectOne(userQuery);}public void updateFlagByUserIdAndIndex(String userId, int index, int indexValue) {userSysFlagMapper.updateFlagByUserIdAndIndex(userId,index,indexValue);}public String queryBitByUserId(Integer bitLength,String userId) {return userSysFlagMapper.queryBitByUserId(bitLength,userId);}
}
package com.bit.demo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bit.demo.dto.UserSysFlagDTO;
import org.apache.ibatis.annotations.Param;public interface UserSysFlagMapper extends BaseMapper<UserSysFlagDTO> {void updateFlagByUserIdAndIndex(@Param("userId") String userId,@Param("index") int index,@Param("bitValue") int bitValue);String queryBitByUserId(@Param("bitLength")Integer bitLength,@Param("userId") String userId);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bit.demo.mapper.UserSysFlagMapper"><update id="updateFlagByUserIdAndIndex">UPDATE user_sys_flagSET flag =CASEWHEN #{bitValue} = 1 THEN flag | (1 &lt;&lt; ${index})   <!-- Setting the 20th bit to 1 -->ELSE flag &amp; ~(1 &lt;&lt; ${index})                     <!-- Setting the 20th bit to 0 -->ENDwhere user_id=#{userId}</update><select id="queryBitByUserId" resultType="string">SELECTCONCAT('b', LPAD(BIN(flag), ${bitLength}, '0')) AS formatted_bit_valueFROMuser_sys_flag WHEREuser_id = #{userId}</select></mapper>

1.4、service
package com.bit.demo.service.impl;import com.bit.demo.dto.UserSysFlagDTO;
import com.bit.demo.mapper.UserSysFlagMapper;
import com.bit.demo.repository.UserSysFlagRepository;
import com.bit.demo.service.UserSysFlagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service("userSysFlagService")
public class UserSysFlagServiceImpl implements UserSysFlagService {@Autowiredprivate UserSysFlagRepository userSysFlagRepository;@Autowiredprivate UserSysFlagMapper userSysFlagMapper;@Overridepublic void insert(UserSysFlagDTO userSysFlagDTO) {userSysFlagRepository.insert(userSysFlagDTO);}@Overridepublic UserSysFlagDTO getByUserId(String userId) {return userSysFlagRepository.getByUserId(userId);}@Overridepublic void updateFlagByUserIdAndIndex(String userId, int index, int indexValue) {userSysFlagRepository.updateFlagByUserIdAndIndex(userId,index,indexValue);}@Overridepublic String queryBitByUserId(Integer bitLength,String userId) {return userSysFlagRepository.queryBitByUserId(bitLength,userId);}
}
1.5、util
package com.bit.demo.util;import com.bit.demo.enums.UserSysFlagEnums;public class BitUtil {public static boolean isSet(long options, long bit) {return (options & bit) == bit;}
}
1.6、启动类
package com.bit.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.bit.demo.mapper")
@SpringBootApplication
public class BitApplication {public static void main(String[] args) {SpringApplication.run(BitApplication.class, args);}
}
1.7、test
package com.bit.demo;import com.bit.demo.dto.UserSysFlagDTO;
import com.bit.demo.service.UserSysFlagService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest(classes = {BitApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@Slf4j
public class UserSysFlagTest {@Autowiredprivate UserSysFlagService userSysFlagService;//1、初始化。最大64位,假如需要60个开关,0代表关,1代表开,默认为关。@Testpublic void init() {UserSysFlagDTO userSysFlagDTO = UserSysFlagDTO.builder().userId("zs").flag(0L).build();userSysFlagService.insert(userSysFlagDTO);}//2、更新,更新第n位的flag@Testpublic void update() {//将 `bigint` 转换为 `bit` 字符串,将第index位设置为indexValue,然后转换回 `bigint`//index从0开始String userId = "zs";int index = 0;int indexValue = 0;userSysFlagService.updateFlagByUserIdAndIndex(userId,index,indexValue);}//3、查询@Testpublic void query() {UserSysFlagDTO userSysFlagDTO = userSysFlagService.getByUserId("zs");log.info(userSysFlagDTO.toString());log.info("flag1值为:{}", userSysFlagDTO.isEnableFlag1());log.info("flag2值为:{}", userSysFlagDTO.isEnableFlag2());log.info("flag60值为:{}", userSysFlagDTO.isEnableFlag60());}//查询二进制@Testpublic void queryBit(){Integer bitLength = 64;String userId = "zs";String bitStr = userSysFlagService.queryBitByUserId(bitLength,userId);System.out.println(bitStr);}
}
1.8、测试:
(1)初始化

如我初始化了ls,默认是0

(2)更新第1位

   更新ls第1位为1

String userId = "ls";int index = 0;int indexValue = 1;

 queryBit:可以看到第一位是1了

query:可以看到isEnableFlag1是true了

 (3)更新其他位

如更新第60位为1:

  String userId = "ls";int index = 59;int indexValue = 1;

 queryBit:可以看到第60位是1了

query:可以看到isEnableFlag1、isEnableFlag60都是true了

(4)再次更新第1位

更新为0,也即关闭功能

String userId = "ls";int index = 0;int indexValue = 0;
queryBit查看:

query查看:

 

这篇关于mysql存储比特位的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996645

相关文章

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.

SpringBoot3.X 整合 MinIO 存储原生方案

《SpringBoot3.X整合MinIO存储原生方案》本文详细介绍了SpringBoot3.X整合MinIO的原生方案,从环境搭建到核心功能实现,涵盖了文件上传、下载、删除等常用操作,并补充了... 目录SpringBoot3.X整合MinIO存储原生方案:从环境搭建到实战开发一、前言:为什么选择MinI

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下