表转换Java类
短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
将匈牙利命名法(some_columns)转驼峰(SomeClass or someFields)的函数:
delimiter $$
create function `to_camel`(class="lazy" data-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(class="lazy" data-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 "class="lazy" data-src"
union all
select concat(" private ",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("表名");
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341