Mysql快速生成Java实体类

需求缘由

Mysql快速生成Java实体类_第1张图片
在使用MybatisPlus过程中,就肯定会创建实体类,虽然 MybatisPlus 也为我们提供了代码生成器,但是我感觉功能有点多了,我只需要创建实体的方法即可,假如我想快速将上述表转为如下的Bean对象,该怎么搞呢?

/**
 *主键
 */
@TableId(value = "id", type = IdType.UUID)
private Integer id;
/**
 *名称
 */
@TableField(value = "name")
private String name;
/**
 *金额
 */
@TableField(value = "money")
private BigDecimal money;
/**
 *年龄
 */
@TableField(value = "age")
private Integer age;
/**
 *创建时间
 */
@TableField(value = "create_time")
private Date createTime;
/**
 *更新时间
 */
@TableField(value = "update_time")
private Date updateTime;

SQL语句

针对实体表的处理,我们可以执行以下SQL语句,并将结果复制到Java代码中即可(需要提前执行 下划线转驼峰函数 哦)

SELECT
	CONCAT(
		"/**\n *",
		REPLACE ( REPLACE ( COLUMN_COMMENT, CHAR ( 10 ), '' ), CHAR ( 13 ), '' ),
		"\n */\n",
	IF
		( COLUMN_KEY = 'PRI', concat( "@TableId(value = \"", COLUMN_NAME, "\", type = IdType.ASSIGN_ID)\n" ), concat( "@TableField(value = \"", COLUMN_NAME, "\")\n" ) ),
		"private ",
	CASE DATA_TYPE 
			WHEN "int" THEN "Integer" 
			WHEN "char" THEN "String" 
			WHEN "varchar" THEN	"String" 
			WHEN "timestamp" THEN "LocalDateTime" 
			WHEN "datetime" THEN "LocalDateTime" 
			WHEN "decimal" THEN	"BigDecimal" 
			WHEN "tinyint" THEN	"Integer" 
			ELSE "Object" 
		END,
		" ",
		underlineToCamel ( COLUMN_NAME ),
		";" 
) 
FROM
	information_schema.COLUMNS 
WHERE
	table_schema = '【替换为自己的库名】' 
	AND table_name = '【替换为表名】' 
ORDER BY
	COLUMN_KEY DESC,
	ORDINAL_POSITION;

附:下划线转驼峰函数

CREATE DEFINER=`root`@`%` FUNCTION `underlineToCamel`(paramString VARCHAR(200)) RETURNS varchar(200) CHARSET utf8
    DETERMINISTIC
begin
		set paramString = replace(paramString, '_a', 'A');
    set paramString = replace(paramString, '_b', 'B');
    set paramString = replace(paramString, '_c', 'C');
    set paramString = replace(paramString, '_d', 'D');
    set paramString = replace(paramString, '_e', 'E');
    set paramString = replace(paramString, '_f', 'F');
    set paramString = replace(paramString, '_g', 'G');
    set paramString = replace(paramString, '_h', 'H');
    set paramString = replace(paramString, '_i', 'I');
    set paramString = replace(paramString, '_j', 'J');
    set paramString = replace(paramString, '_k', 'K');
    set paramString = replace(paramString, '_l', 'L');
    set paramString = replace(paramString, '_m', 'M');
    set paramString = replace(paramString, '_n', 'N');
    set paramString = replace(paramString, '_o', 'O');
    set paramString = replace(paramString, '_p', 'P');
    set paramString = replace(paramString, '_q', 'Q');
    set paramString = replace(paramString, '_r', 'R');
    set paramString = replace(paramString, '_s', 'S');
    set paramString = replace(paramString, '_t', 'T');
    set paramString = replace(paramString, '_u', 'U');
    set paramString = replace(paramString, '_v', 'V');
    set paramString = replace(paramString, '_w', 'W');
    set paramString = replace(paramString, '_x', 'X');
    set paramString = replace(paramString, '_y', 'Y');
    set paramString = replace(paramString, '_z', 'Z');
    set paramString = replace(paramString, '_', '');
    RETURN paramString;
	end

快去试试吧~

你可能感兴趣的:(java,开发语言)