# 查找在那个表并生成 字段名 SQL
SELECT TABLE_SCHEMA, TABLE_NAME, concat(TABLE_SCHEMA, '.', TABLE_NAME) AS st,
concat('SELECT ', group_concat(COLUMN_NAME ORDER BY ORDINAL_POSITION SEPARATOR ', ' ),
' FROM ' , concat(TABLE_SCHEMA, '.', TABLE_NAME) , ';') AS SQLstr
FROM information_schema.COLUMNS WHERE 1=1
AND TABLE_NAME like 'job_depend' GROUP BY TABLE_SCHEMA, TABLE_NAME ORDER BY length(TABLE_SCHEMA);
# 查找多个字段在哪个表 并生成 字段名 SQL
SELECT TABLE_SCHEMA, TABLE_NAME, concat(TABLE_SCHEMA, '.', TABLE_NAME) AS st,
concat('SELECT ', group_concat(COLUMN_NAME ORDER BY ORDINAL_POSITION SEPARATOR ', ' ),
' FROM ' , concat(TABLE_SCHEMA, '.', TABLE_NAME) , ';') AS SQLstr
FROM information_schema.COLUMNS WHERE 1=1
AND TABLE_NAME in ('job_depend','job_id') GROUP BY TABLE_SCHEMA, TABLE_NAME
HAVING count(TABLE_NAME)>=2 ORDER BY length(TABLE_SCHEMA);
# #{驼峰字段} (创建存储函数snakeToCamel后使用)
SELECT TABLE_SCHEMA, TABLE_NAME,
snakeToCamel(group_concat(concat('#{',COLUMN_NAME,'}')
ORDER BY ORDINAL_POSITION SEPARATOR ', ')) AS COLUMN_NAMES
FROM information_schema.COLUMNS WHERE 1=1
AND TABLE_NAME like 'job_depend' GROUP BY TABLE_SCHEMA, TABLE_NAME;
# 字段名=#{驼峰字段} SQL (创建存储函数snakeToCamel后使用)
SELECT TABLE_SCHEMA, TABLE_NAME, concat('UPDATE ', TABLE_NAME, ' SET\n',
group_concat(concat(' ', COLUMN_NAME,' = #{',snakeToCamel(COLUMN_NAME),'}')
ORDER BY ORDINAL_POSITION SEPARATOR ',\n')
, '\nWHERE col=val;')AS SQLStr
FROM information_schema.COLUMNS WHERE 1=1
AND TABLE_NAME like 'job_depend' GROUP BY TABLE_SCHEMA, TABLE_NAME;
# 打开 MySQL8 默认关闭的创建存储函数开关
SET GLOBAL log_bin_trust_function_creators = 1;
# 若存储过程存在则删除
DROP FUNCTION IF EXISTS snakeToCamel;
# 重定义分隔符,兼容 DataGrip 以外的程序
DELIMITER $
# 创建一个存储函数
CREATE FUNCTION snakeToCamel(snakeString VARCHAR(16383))
# 返回值类型
RETURNS VARCHAR(16383)
# 开始内容
BEGIN
set snakeString = replace(snakeString, '_a', 'A');
set snakeString = replace(snakeString, '_b', 'B');
set snakeString = replace(snakeString, '_c', 'C');
set snakeString = replace(snakeString, '_d', 'D');
set snakeString = replace(snakeString, '_e', 'E');
set snakeString = replace(snakeString, '_f', 'F');
set snakeString = replace(snakeString, '_g', 'G');
set snakeString = replace(snakeString, '_h', 'H');
set snakeString = replace(snakeString, '_i', 'I');
set snakeString = replace(snakeString, '_j', 'J');
set snakeString = replace(snakeString, '_k', 'K');
set snakeString = replace(snakeString, '_l', 'L');
set snakeString = replace(snakeString, '_m', 'M');
set snakeString = replace(snakeString, '_n', 'N');
set snakeString = replace(snakeString, '_o', 'O');
set snakeString = replace(snakeString, '_p', 'P');
set snakeString = replace(snakeString, '_q', 'Q');
set snakeString = replace(snakeString, '_r', 'R');
set snakeString = replace(snakeString, '_s', 'S');
set snakeString = replace(snakeString, '_t', 'T');
set snakeString = replace(snakeString, '_u', 'U');
set snakeString = replace(snakeString, '_v', 'V');
set snakeString = replace(snakeString, '_w', 'W');
set snakeString = replace(snakeString, '_x', 'X');
set snakeString = replace(snakeString, '_y', 'Y');
set snakeString = replace(snakeString, '_z', 'Z');
set snakeString = replace(snakeString, '_', '');
RETURN snakeString;
END;
DELIMITER ;
# 字段注释
SELECT TABLE_SCHEMA, TABLE_NAME,
group_concat(CASE COLUMN_COMMENT
WHEN '' THEN COLUMN_NAME
ELSE COLUMN_COMMENT END
ORDER BY ORDINAL_POSITION SEPARATOR ', ' ) AS COLUMN_NAMES
FROM information_schema.COLUMNS WHERE 1=1
AND TABLE_NAME like 'job_depend' GROUP BY TABLE_SCHEMA, TABLE_NAME;
# 查找在那个表并生成 字段名 AS 注释 SQL
SELECT TABLE_SCHEMA, TABLE_NAME,
concat('SELECT ',group_concat(
CASE COLUMN_COMMENT
WHEN '' THEN COLUMN_NAME
ELSE concat_ws(' AS ', COLUMN_NAME, COLUMN_COMMENT)
END
ORDER BY ORDINAL_POSITION SEPARATOR ', ')
, ' FROM ' , TABLE_SCHEMA, '.', TABLE_NAME, ';') AS SQLStr
FROM information_schema.COLUMNS WHERE 1=1
AND TABLE_NAME like 'job_depend' GROUP BY TABLE_SCHEMA, TABLE_NAME;
# /** 字段名 */\n private type 驼峰字段; (创建存储函数snakeToCamel后使用)
SELECT TABLE_SCHEMA, TABLE_NAME,
group_concat(concat(' /** ', COLUMN_COMMENT, ' */\n'
,' private ',
CASE COLUMN_TYPE
WHEN 'VARCHAR' THEN 'String'
WHEN 'CHAR' THEN 'String'
WHEN 'TEXT' THEN 'String'
WHEN 'TINYINT' THEN 'Integer'
WHEN 'SMALLINT' THEN 'Integer'
WHEN 'MEDIUMINT' THEN 'Integer'
WHEN 'BOOLEAN' THEN 'Integer'
WHEN 'BIGINT' THEN 'BigInteger'
WHEN 'INTEGER' THEN 'Long'
WHEN 'ID' THEN 'Long'
WHEN 'FLOAT' THEN 'Float'
WHEN 'DOUBLE' THEN 'Double'
WHEN 'DECIMAL' THEN 'BigDecimal'
WHEN 'BIT' THEN 'Boolean'
WHEN 'BLOB' THEN 'byte[]'
WHEN 'DATE' THEN 'Date'
WHEN 'YEAR' THEN 'Date'
WHEN 'TIME' THEN 'Time'
WHEN 'DATETIME' THEN 'Timestamp'
WHEN 'TIMESTAMP' THEN 'Timestamp'
ELSE 'String ' END,
snakeToCamel(COLUMN_NAME),';'
,'\n'
)
ORDER BY ORDINAL_POSITION SEPARATOR '\n' ) AS Java
FROM information_schema.COLUMNS WHERE 1=1
AND TABLE_NAME like 'job_depend' GROUP BY TABLE_SCHEMA, TABLE_NAME;
MySQL #{驼峰字段} for MyBatis
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言: SQL背景:两张表,一张表里有 id, tname, age(id主键) 另一张表里有 id, addre...
- 字段名不能与 mysql关键字冲突 如果column 名与mysql关键字冲突,一定要加反引号。比如 show;如...
- 系列文章 Mybatis数据库字段加解密1-使用mysql自带加密方法 Mybatis数据库字段加解密2-使用ty...
- MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品。MySQ...
- 对于MyISAM引擎,不建立索引的情况下(推荐),效率从高到低:int > UNIX_TIMESTAMP(time...