Last active
April 21, 2022 04:15
-
-
Save crgarridos/e36d22f9c9c74020a2e1 to your computer and use it in GitHub Desktop.
Generate Java Model Class from MySQL table (extracted from: http://www.code4copy.com/post/generate-java-model-class-from-mysql-table)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- -------------------------------------------------------------------------------- | |
-- Routine DDL | |
-- Note: comments before and after the routine body will not be stored by the server | |
-- -------------------------------------------------------------------------------- | |
DELIMITER $$ | |
CREATE DEFINER=`root`@`localhost` PROCEDURE `GenJavaModel`(in pTableName VARCHAR(255) ) | |
BEGIN | |
DECLARE vClassName varchar(255); | |
declare vClassGetSet mediumtext; | |
declare vClassPrivate mediumtext; | |
declare v_codeChunk_pri_var varchar(1024); | |
declare v_codeChunk_pub_get varchar(1024); | |
declare v_codeChunk_pub_set varchar(1024); | |
DECLARE v_finished INTEGER DEFAULT 0; | |
DEClARE code_cursor CURSOR FOR | |
SELECT pri_var,pub_get, pub_set FROM temp1; | |
DECLARE CONTINUE HANDLER | |
FOR NOT FOUND SET v_finished = 1; | |
set vClassGetSet =''; | |
/* Make class name*/ | |
SELECT (CASE WHEN col1 = col2 THEN col1 ELSE concat(col1,col2) END) into vClassName | |
FROM( | |
SELECT CONCAT(UCASE(MID(ColumnName1,1,1)),LCASE(MID(ColumnName1,2))) as col1, | |
CONCAT(UCASE(MID(ColumnName2,1,1)),LCASE(MID(ColumnName2,2))) as col2 | |
FROM | |
(SELECT SUBSTRING_INDEX(pTableName, '_', -1) as ColumnName2, | |
SUBSTRING_INDEX(pTableName, '_', 1) as ColumnName1) A) B; | |
/*store all properties into temp table*/ | |
CREATE TEMPORARY TABLE IF NOT EXISTS temp1 ENGINE=MyISAM | |
as ( | |
select | |
concat('\tprivate ', ColumnType,' _', FieldName,';') pri_var, | |
concat( 'public ', ColumnType , ' get' , FieldName,'(){\r\n\t\t return _', FieldName,';\r\n\t}') pub_get, | |
concat( 'public void ', ' set' , FieldName,'( ',ColumnType,' value){\r\n\t\t _', FieldName,' = value;\r\n\t}') pub_set | |
FROM( | |
SELECT (CASE WHEN col1 = col2 THEN col1 ELSE concat(col1,col2) END) AS FieldName, | |
case DATA_TYPE | |
when 'bigint' then 'long' | |
when 'binary' then 'byte[]' | |
when 'bit' then 'bool' | |
when 'char' then 'String' | |
when 'date' then 'Date' | |
when 'datetime' then 'DateTime' | |
when 'datetime2' then 'DateTime' | |
when 'decimal' then 'decimal' | |
when 'float' then 'float' | |
when 'image' then 'byte[]' | |
when 'int' then 'int' | |
when 'money' then 'decimal' | |
when 'nchar' then 'String' | |
when 'ntext' then 'String' | |
when 'numeric' then 'decimal' | |
when 'nvarchar' then 'String' | |
when 'real' then 'double' | |
when 'smalldatetime' then 'Date' | |
when 'smallint' then 'short' | |
when 'mediumint' then 'int' | |
when 'smallmoney' then 'decimal' | |
when 'text' then 'String' | |
when 'time' then 'Date' | |
when 'timestamp' then 'Date' | |
when 'tinyint' then 'byte' | |
when 'uniqueidentifier' then 'String' | |
when 'varbinary' then 'byte[]' | |
when 'varchar' then 'String' | |
when 'year' THEN 'int' | |
else 'UNKNOWN_' + DATA_TYPE | |
end ColumnType | |
FROM( | |
select CONCAT(UCASE(MID(ColumnName1,1,1)),LCASE(MID(ColumnName1,2))) as col1, | |
CONCAT(UCASE(MID(ColumnName2,1,1)),LCASE(MID(ColumnName2,2))) as col2, DATA_TYPE | |
from | |
(SELECT SUBSTRING_INDEX(COLUMN_NAME, '_', -1) as ColumnName2, | |
SUBSTRING_INDEX(COLUMN_NAME, '_', 1) as ColumnName1, | |
DATA_TYPE, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = pTableName) A) B)C); | |
set vClassGetSet = ''; | |
set vClassPrivate = ''; | |
/* concat all properties*/ | |
OPEN code_cursor; | |
get_code: LOOP | |
FETCH code_cursor INTO v_codeChunk_pri_var, v_codeChunk_pub_get, v_codeChunk_pub_set; | |
IF v_finished = 1 THEN | |
LEAVE get_code; | |
END IF; | |
-- build code | |
select CONCAT('\t',vClassPrivate,'\r\n', v_codeChunk_pri_var) into vClassPrivate; | |
select CONCAT('\t',vClassGetSet,'\r\n\t', v_codeChunk_pub_get,'\r\n\t', v_codeChunk_pub_set) into vClassGetSet ; | |
END LOOP get_code; | |
CLOSE code_cursor; | |
drop table temp1; | |
/*make class*/ | |
select concat('public class ',vClassName,'\r\n{',vClassPrivate,'\r\n', vClassGetSet,'\r\n}'); | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment