其他 · 2019年9月5日 0

Mysql To C# Entity,Java POJO

Mysql To C# Entity

最近使用Dapper的没找到很好的生成实体类的工具,不过发现一个存储过程,很有想法,也简单,定制性比较高。

CREATE DEFINER=`root`@`localhost` PROCEDURE `GenCSharpModel`(IN `pTableName` VARCHAR(255))
BEGIN
DECLARE vClassName varchar(255);
declare vClassCode mediumtext;
declare v_codeChunk varchar(1024);
DECLARE v_finished INTEGER DEFAULT 0;
DEClARE code_cursor CURSOR FOR  SELECT code FROM temp1; 

DECLARE CONTINUE HANDLER  FOR NOT FOUND SET v_finished = 1;

set vClassCode ='';

SELECT pTableName into vClassName;

/*将所有属性存储到临时表中*/
CREATE TEMPORARY TABLE IF NOT EXISTS  temp1 ENGINE=MyISAM  
as (select concat( '/// <summary>\n','///', COLUMN_COMMENT ,'\n/// </summary>\n','public ', ColumnType , ' ' , FieldName,' { get; set; }') code
    FROM(
      SELECT COLUMN_NAME AS FieldName, 
        case DATA_TYPE 
        when 'bigint' then 'long'
        when 'binary' then 'byte[]'
        when 'bit' then 'bool'
        when 'char' then 'string'
        when 'date' then 'DateTime'
        when 'datetime' then 'DateTime'
        when 'datetime2' then 'DateTime'
        when 'datetimeoffset' then 'DateTimeOffset'
        when 'decimal' then 'decimal'
        when 'float' then 'float'
        when 'image' then 'byte[]'
        when 'int' then 'int'
        when 'money' then 'decimal'
        when 'nchar' then 'char'
        when 'ntext' then 'string'
        when 'numeric' then 'decimal'
        when 'nvarchar' then 'string'
        when 'real' then 'double'
        when 'smalldatetime' then 'DateTime'
        when 'smallint' then 'short'
        when 'mediumint' then 'INT'
        when 'smallmoney' then 'decimal'
        when 'text' then 'string'
        when 'time' then 'TimeSpan'
        when 'timestamp' then 'DateTime'
        when 'tinyint' then 'byte'
        when 'uniqueidentifier' then 'Guid'
        when 'varbinary' then 'byte[]'
        when 'varchar' then 'string'
        when 'year' THEN 'UINT'
        else 'UNKNOWN_' + DATA_TYPE
    end ColumnType,COLUMN_COMMENT
  FROM(SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS  WHERE table_name = pTableName ) B)C);

set vClassCode = '';
/*拼接*/
OPEN code_cursor;

        get_code: LOOP

            FETCH code_cursor INTO v_codeChunk;

            IF v_finished = 1 THEN 
                LEAVE get_code;
            END IF;

            -- build code
            select  CONCAT(vClassCode,'\n', v_codeChunk) into  vClassCode ;

        END LOOP get_code;

    CLOSE code_cursor;

drop table temp1;

/*生成类model*/
select concat('public class ',vClassName,'\n{', vClassCode,'\n}');
END

.
.
.
.
.
.
.
.
.

【本文章出自NM1024.com,转载请注明作者出处。】






>>转载请注明原文链接地址:Mysql To C# Entity,Java POJO