大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说表转换Java类_java表格,希望您对编程的造诣更进一步.
将匈牙利命名法(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(" 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("表名");
代码100分
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/7953.html