建设网站联系方式,常德论坛网,免费外贸网站制作,淮安汽车网站制作小技巧notebook
1、MybatisPlus 批量保存
从BaseMapper接口方法可知#xff0c;mybatis plus mapper只有根据id批量删除和查询#xff0c;没有批量保存#xff08;insert 、update#xff09;#xff0c;要实现也很简单#xff0c;需要定义一个Service
Service
Slf4j
…小技巧notebook
1、MybatisPlus 批量保存
从BaseMapper接口方法可知mybatis plus mapper只有根据id批量删除和查询没有批量保存insert 、update要实现也很简单需要定义一个Service
Service
Slf4j
public class QueueUsedInfoService extends ServiceImplQueueUsedInfoMapper, QueueUsedInfo implements BatchInsertService {
}
interface BatchInsertService extends IServiceQueueUsedInfo {
}其中QueueUsedInfoMapper实现了BaseMapper接口
Mapper
public interface QueueUsedInfoMapper extends BaseMapperQueueUsedInfo {
}这个时候可以发现QueueUsedInfoService中有诸如saveBatchsaveOrUpdateBatch方法使用
2、MybatisPlus 注解实现脚本标签使用
比如、、这些标签在mapper映射文件中可以直接使用但是如果通过注解的话是不可以直接用的需要用到{“”}标签包裹如
Select({scriptSELECTe.id,d.* FROM(SELECTc.* FROM(SELECTa.cluster_id,a.cluster_user,substring_index( substring_index( a.used, ,, b.help_topic_id 1 ), ,, - 1 ) AS queue FROMpartition_info aINNER JOIN mysql.help_topic b ON b.help_topic_id lt; ( length( a.used ) - length( REPLACE ( a.used, ,, )) 1 ) ) c WHERECONCAT( c.cluster_id, -, c.queue ) IN foreach itemitem indexindex collectionitems open( separator, close)#{item}/foreach) dLEFT JOIN queue_used_info e ON d.cluster_id e.cluster_id AND d.cluster_user e.cluster_user AND d.queue e.queue/script})
ListQueueUsedInfo getQueueUsedInfos(Param(items) ListString lstCondition);另外注意
3、Mybatis实现insert时同时根据主键唯一性有条件update
!--批量操作无则insert 有则updateupdate时根据最后更新时间做三元表达式判断更新时间小于数据库时间则不更新--
insert idbatchInsertOrUpdate parameterTypelistinsert into sc_job(id,cluster_id,job_user,job_status,last_update_time) valuesforeach collection lstJob itemitem separator ,(#{item.id},#{item.clusterId},#{item.jobUser},#{item.jobStatus},#{item.lastUpdateTime})/foreach on DUPLICATE KEY UPDATElast_update_time IF(last_update_time values(last_update_time),last_update_time,values(last_update_time)),cluster_id IF(last_update_time values(last_update_time),cluster_id,values (cluster_id)),job_user IF(last_update_time values(last_update_time),job_user,values (job_user)),job_status IF(last_update_time values(last_update_time),job_status,values (job_status))
/insert解释如果没出现主键冲突则insert出现主键冲突的情况下满足条件的字段进行update
4、MySQL 利用help_topic系统表实现字符串无限制分割
前提账号拥有root权限
SELECTa.cluster_id,substring_index( substring_index( a.used, ,, b.help_topic_id 1 ), ,, - 1 ) AS queue FROMpartition_info aINNER JOIN mysql.help_topic b ON b.help_topic_id ( length( a.used ) - length( REPLACE ( a.used, ,, )) 1 ) 对表used字段进行无限制分割原理
利用help_topic.help_topic_id自增属性得到切割后数据个数然后使用substring_index循环切割每次得到最后一个这种方法的缺陷是需要MySQL用户拥有root权限
另外一种办法
SELECTa.cluster_id,substring_index( substring_index( a.used, ,, b.id 1 ), ,, - 1 ) AS queue FROMpartition_info aINNER JOIN (SELECT (ROW :Row 1) as id FROM user_info,(SELECT Row:-1) zz) b ON b.id ( length( a.used ) - length( REPLACE ( a.used, ,, )) 1 ) user_info表是库种已存在的表行数必须大于分割字段的最大逗号数利用其row number的特性创建自增序列建议选用初始化表保证有足够多的原始数据作为Row的支撑
5、跨服务器数据库数据交叉处理脚本
#!/bin/bash
echo Start Check Data.#mysqldump source schema and data
mysqldump -h192.168.137.129 -P3307 -uroot -proot parauser --tables 源库表 ./parauser.sql;
mysqldump -h192.168.137.129 -P3307 -uroot -proot ngbilling --tables 源库表 ./ngbilling.sql;#source schema and validate data to console
mysql -h192.168.137.129 -P3306 -uroot -proot -e
use 目标库;
set names utf8;
source ./parauser.sql;
source ./ngbilling.sql;---------------------------业务处理------------------------------------DROP TABLES 源库表;
commit;#remove dump file
rm ./ngbilling.sql ./parauser.sql -f;echo Check Data Successful.
exit; 6、年份分表数据批量导出备份
#!/bin/bash
echo Start Export Matomo Data...
#$1为要导出数据年份$2为需要创建备份数据库名称不需要手动创建matomo_backup.sql放export.sh同级目录
echo 导出$1年的数据到备份数据库$2...;
#mysqldump导出源数据库结构及数据具体数据库连接信息请自行修改
mysqldump -h192.168.137.128 -P3306 -uroot -proot matomo --column-statistics0 ./matomo.sql;#切换到目标数据库服务器具体数据库连接信息请自行修改
mysql -h192.168.137.128 -P3307 -uroot -proot -e
#创建备份库
CREATE DATABASE IF NOT EXISTS $2 DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_bin;
use $2;
source ./matomo_backup.sql;#创建数据源临时库
CREATE DATABASE IF NOT EXISTS matomo DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_bin;
use matomo;
set names utf8;
source ./matomo.sql;#导出需要备份年份的数据普通表
---------------------------
INSERT IGNORE INTO $2.matomo_archive_invalidations SELECT * FROM matomo_archive_invalidations where DATE_FORMAT(ts_invalidated,%Y) $1;
---------------------------#切换到备份库通过存储过程、游标创建备份库中不存在的数据表及数据插入
use $2;#处理matomo_archive_blob_*;matomo_archive_numeric_*数据表集合年份表
DROP PROCEDURE IF EXISTS export_list;
delimiter $
CREATE PROCEDURE export_list()
BEGIN#遍历结束标志DECLARE var_flag INT DEFAULT 0;#遍历表名变量DECLARE var_table_name VARCHAR(100);#表集合DECLARE table_name_list CURSOR FORSELECT table_name FROM information_schema.tablesWHERE table_schema matomo AND table_type base tableAND(table_name like matomo_archive_blob_% AND SUBSTR(table_name,21,4) $1ORtable_name like matomo_archive_numeric_% AND SUBSTR(table_name,24,4) $1) ;#将结束标志绑定到游标DECLARE CONTINUE HANDLER FOR NOT FOUND SET var_flag 1;#打开游标OPEN table_name_list;FETCH table_name_list INTO var_table_name;WHILE var_flag !1 DO#判断表类型IF FIND_IN_SET(matomo_archive_blob_,var_table_name)THEN#需要先判断该表在备份库是否存在不存在需要先创建然后再插入数据SET createTableStr CONCAT(CREATE TABLE IF NOT EXISTS ,var_table_name,(idarchive int(10) UNSIGNED NOT NULL,name varchar(190) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,idsite int(10) UNSIGNED NULL DEFAULT NULL,date1 date NULL DEFAULT NULL,date2 date NULL DEFAULT NULL,period tinyint(3) UNSIGNED NULL DEFAULT NULL,ts_archived datetime NULL DEFAULT NULL,value mediumblob NULL,PRIMARY KEY (idarchive, name) USING BTREE,INDEX index_period_archived(period, ts_archived) USING BTREE) ENGINE InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ROW_FORMAT COMPACT);ELSE#先判断该表在备份库是否存在不存在需要先创建然后再插入数据SET createTableStr CONCAT(CREATE TABLE IF NOT EXISTS ,var_table_name,(idarchive int(10) UNSIGNED NOT NULL,name varchar(190) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,idsite int(10) UNSIGNED NULL DEFAULT NULL,date1 date NULL DEFAULT NULL,date2 date NULL DEFAULT NULL,period tinyint(3) UNSIGNED NULL DEFAULT NULL,ts_archived datetime NULL DEFAULT NULL,value mediumblob NULL,PRIMARY KEY (idarchive, name) USING BTREE,INDEX index_period_archived(period, ts_archived) USING BTREE) ENGINE InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ROW_FORMAT COMPACT);END IF;PREPARE stmt FROM createTableStr;EXECUTE stmt;#对matomo_archive_blob_*;matomo_archive_numeric_*数据表数据进行备份SET insertStr CONCAT(INSERT IGNORE INTO ,var_table_name, SELECT * FROM matomo.,var_table_name);PREPARE stmt FROM insertStr;EXECUTE stmt;FETCH table_name_list INTO var_table_name;END WHILE;CLOSE table_name_list;
END;#执行存储过程
CALL export_list();#删除存储过程
DROP PROCEDURE export_list;#切换回matomo临时库
use matomo;#删除临时源库
DROP DATABASE matomo;
commit;#remove dump file
rm ./matomo.sql;echo Export Data Successful.
exit;
导出备份完成后对源库表清理脚本
#!/bin/bash
echo Start Clean Matomo Data...
echo 开始清除已备份的$1年数据请确保已执行export.sh进行备份...;
mysql -h192.168.137.128 -P3306 -uroot -proot -e
use matomo;#删除已备份年份的历史数据普通表
DELETE FROM matomo_archive_invalidations where DATE_FORMAT(ts_invalidated,%Y) $1;#删除matomo_archive_blob_*;matomo_archive_numeric_*已备份年份的数据表年份表
DROP PROCEDURE IF EXISTS drop_table_list;
delimiter $
CREATE PROCEDURE drop_table_list()
BEGINDECLARE i INT DEFAULT 1;WHILE i 12 DOIF i 10THENSET dropTableStr CONCAT(DROP table IF EXISTS matomo_archive_blob_$1_0 , i , , , matomo_archive_numeric_$1_0 , i);ELSESET dropTableStr CONCAT(DROP table IF EXISTS matomo_archive_blob_$1_ , i , , , matomo_archive_numeric_$1_ , i);END IF;PREPARE stmt FROM dropTableStr;EXECUTE stmt;SET i i1;END WHILE;
END;#执行存储过程
CALL drop_table_list();
#删除存储过程
DROP PROCEDURE drop_table_list;
commit;echo Clean Data Successful.
exit; 7、MySQL数据同步到Redis步骤
#将利用redis管道模式将MySQL数据刷入Redis
#1、MySQL查询结果拼接成Redis执行脚本见db/mysql_to_redis.sql
#2、将上一步脚本执行查询结果写入文件具体的地址,数据库s账号密码根据需要设置,mysql_to_redis.sqlmtr.txt路径为MySQL容器路径
docker exec mysql /bin/bash -c mysql -h192.168.137.128 -P3306 -uroot -proot -Dngbilling --skip-column-names --raw /mydir/mysql_to_redis.sql /mydir/mtr.txt
#3、cat数据文件通过管道符将结果让redis-cli批量刷入redis具体的地址,数据库密码根据需要设置mtr.txt为redis容器根路径
cat mtr.txt |redis-cli -h 192.168.137.128 -n 0 --pipe -a root8、MySQL根据select结果集批量更新
UPDATE console.cluster a
INNER JOIN ( SELECT name, is_proxy, resource_type FROM console.b_cluster ) b ON b.name a.id
SET a.owned b.is_proxy ,a.res_type b.resource_type;UPDATE console.account_group a,ngbilling.b_group b
SET a.fee_name b.group_name
WHEREa.id b.group_id;