mysql java 根据表名转为javaBean,下划线转驼峰,带注释,部分常用类型匹配。

时间:2022-07-14 19:08:26

-- mysql java 根据表名转为javaBean,下划线转驼峰,带注释,部分常用类型匹配
select concat(
    '/** ',
    COLUMN_COMMENT,
    ' */',
    'private ',
    (case DATA_TYPE
        when 'varchar' then 'String '
        when 'int' then 'Integer '
        when 'double' then 'double '
        when 'float' then 'double '
        when 'datetime' then 'String '
        end),
    leftstr,
    if(leftstr = '',SUBSTRING(rightstr,1,1),UPPER(SUBSTRING(rightstr,1,1))),
    SUBSTRING(rightstr,2,length(rightstr)),
    ';'
) as java_variable
from (
    select 
        ORDINAL_POSITION,
    substr(COLUMN_NAME,1,instr(COLUMN_NAME,'_')-1) leftstr,
    substr(COLUMN_NAME,instr(COLUMN_NAME,'_'    )+1,
    length(COLUMN_NAME)-instr(COLUMN_NAME,'_')) rightstr,
    DATA_TYPE,
    COLUMN_COMMENT from information_schema.COLUMNS 
    where table_name = 'teacher' -- 填写所需要的表名
) a order by ORDINAL_POSITION
-- 支持mysql8.0+

mysql java 根据表名转为javaBean,下划线转驼峰,带注释,部分常用类型匹配。