如果您可以访问数据库,那么我建议您创建一个新的列哈希。
例如请跟随这个脚本
drop database if exists test_md5;
create database test_md5;
use test_md5;
CREATE TABLE if not exists `test_users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) not null,
PRIMARY KEY (`id`)
);
CREATE TABLE if not exists `test_posts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`user_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_post_user` (`user_id`),
CONSTRAINT `fk_post_user` FOREIGN KEY (`user_id`) REFERENCES `test_users` (`id`)
);
-- Users
insert into test_users(username) values('john');
insert into test_users(username) values('jane');
-- John's test_posts Content
insert into test_posts(title, user_id) values('hey i am john, this is my very first post', 1);
insert into test_posts(title, user_id) values('hey i am john, this is my very first post', 1);
insert into test_posts(title, user_id) values('hey i am john again, i\'ll be going off from website for couple of days', 1);
-- Jane's test_posts Content
insert into test_posts(title, user_id) values('hey i am jane, this website looks a biut dry', 2);
请注意,我的第二行与第一行的内容相同。
现在,当查询它们时,我为构成单行项或多个表的数据完整性的字段添加提取列。
当我们将posts.id column 添加到concat 函数时,它使行可区分。
SELECT posts.id, posts.title, users.username,
md5(concat(posts.title, users.username))
as hash from test_posts as posts
inner join test_users as users where posts.user_id = users.id
在第一次迭代中,您将返回 hash 以及数据。
在第二次迭代中,您将获得请求中的哈希值和来自 sql 响应或数据库层的哈希值,您可以比较这两个值并进一步决定还需要做什么。
另一种方法是,您为数据库中表示的每个模型实现有意义的equals and hashcode。
另一种解决方案是将字符串转换为 JSON 并计算校验和并进行比较,但这效率较低,因为它涉及添加额外的 json 库,并且转换为 JSON 是一种昂贵的魔法。
参考这里:
https://ckhconsulting.com/row-or-rows-md5-hash-with-mysql/