将匈牙利命名法(some_columns)转驼峰(SomeClass or someFields)的函数:
delimiter $$
create function `to_camel`(src varchar(255), lowercase boolean) returns varchar(255) charset utf8
begin
declare temp varchar(255);
declare buffer varchar(255);
declare i int;
declare lasti int;
declare len int;
set temp = concat(src, '_');
set buffer = '';
set i = 1;
set lasti = 1;
set len = length(temp);
while i <= len do
if substring(temp, i, 1) = '_' then
set temp =concat(substring(temp, 1, lasti - 1), upper(substring(temp, lasti, 1)),substring(temp, lasti + 1));
set buffer =concat(buffer, substring(temp, lasti, i - lasti));
set lasti = i + 1;
end if;
set i = i + 1;
end while;
if lowercase then
set buffer =concat(lower(substring(buffer, 1, 1)), substring(buffer, 2));
end if;
return buffer;
end $$
delimiter ;
将db_type转JavaType的函数:
delimiter $$
create function `to_java_type`(sqltype varchar(255))returns varchar(255) charset utf8
begin
declare javatype varchar(255);
set javatype = '';
case sqltype
when 'bigint' then
set javatype = 'Long';
when 'binary' then
set javatype = 'Integer';
when 'bit' then
set javatype = 'Boolean';
when 'blob' then
set javatype = 'Byte[]';
when 'bool' then
set javatype = 'Boolean';
when 'boolean' then
set javatype = 'Boolean';
when 'char' then
set javatype = 'String';
when 'date' then
set javatype = 'Date';
when 'datetime' then
set javatype = 'Date';
when 'decimal' then
set javatype = 'Double';
when 'double' then
set javatype = 'Double';
when 'enum' then
set javatype = 'Object';
when 'float' then
set javatype = 'Float';
when 'int' then
set javatype = 'Integer';
when 'longblog' then
set javatype = 'Byte[]';
when 'longtext' then
set javatype = 'String';
when 'mediumblob' then
set javatype = 'Byte[]';
when 'mediumint' then
set javatype = 'Integer';
when 'mediumtext' then
set javatype = 'String';
when 'numeric' then
set javatype = 'Double';
when 'real' then
set javatype = 'Boolean';
when 'set' then
set javatype = 'Object';
when 'smallint' then
set javatype = 'Integer';
when 'text' then
set javatype = 'String';
when 'time' then
set javatype = 'Date';
when 'timestamp' then
set javatype = 'Date';
when 'tinyblob' then
set javatype = 'Byte[]';
when 'tinyint' then
set javatype = 'Integer';
when 'tinytext' then
set javatype = 'String';
when 'varbinary' then
set javatype = 'Integer';
when 'varchar' then
set javatype = 'String';
when 'year' then
set javatype = 'Date';
end case;
if javatype = '' then
set javatype = 'Object';
end if;
return javatype;
end $$
delimiter ;
将表转换成Java类(仅含熟悉)的存储过程,getters和setters需要自己生成:
delimiter $$
create procedure `table_to_class`(tablename varchar(255))
begin
select concat('public class ',substring(to_camel(tablename, false),2), ' implements Serializable {') as 'src'
union all
select concat('\tprivate ',to_java_type(data_type), ' ', to_camel(column_name, true), '; //') from information_schema.columns where table_name = tablename
union all
select concat('public ',substring(to_camel(tablename, false),2), '() {}')
union all
select '}';
end $$
delimiter ;
使用方法:
call table_to_class('表名');
来源:oschina
链接:https://my.oschina.net/u/2333003/blog/4308530