文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

表转换Java类

2018-02-02 02:54

关注

表转换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("表名");

 

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯