前言
最近在用SpringBoot + MyBatisPlus + SQL Server
原则上是能不手写的代码那必然不手写,所以就在百度上找了找由表生成Java Bean的方案
但是发现百度上找的代码生成方案对驼峰不太友好,于是就把sql中对字段重命名的地方给改了改
创建函数 StrSplit
CREATE FUNCTION StrSplit( @Long_str varchar ( 8000 ), @split_str varchar ( 100 ))
RETURNS @tmp TABLE (
ID inT IDENTITY PRIMARY KEY ,
short_str varchar ( 8000 )
)
AS
BEGIN DECLARE @long_str_Tmp varchar ( 8000 ), @short_str varchar ( 8000 ), @split_str_length int
SET @split_str_length = LEN(@split_str) IF CHARINDEX ( @split_str , @Long_str ) = 1
SET @long_str_Tmp = SUBSTRING(@Long_str, @split_str_length + 1, LEN(@Long_str) - @split_str_length)
ELSE
SET @long_str_Tmp = @Long_str IF CHARINDEX ( REVERSE ( @split_str ), REVERSE ( @long_str_Tmp )) > 1
SET @long_str_Tmp = @long_str_Tmp + @split_str
ELSE
SET @long_str_Tmp = @long_str_Tmp IF CHARINDEX ( @split_str , @long_str_Tmp ) = 0
Insert INTO @tmp
select @long_str_Tmp
ELSE
BEGIN WHILE CHARINDEX ( @split_str , @long_str_Tmp ) > 0
BEGIN
SET @short_str =
SUBSTRING(@long_str_Tmp, 1, CHARINDEX(@split_str, @long_str_Tmp) - 1) DECLARE @long_str_Tmp_LEN INT , @split_str_Position_END int
SET @long_str_Tmp_LEN = LEN(@long_str_Tmp)
SET @split_str_Position_END = LEN(@short_str) + @split_str_length
SET @long_str_Tmp = REVERSE(SUBSTRING(REVERSE(@long_str_Tmp), 1, @long_str_Tmp_LEN - @split_str_Position_END))
SET @short_str = lower(@short_str)
SET @short_str =
upper(substring(@short_str, 1, 1)) + substring(@short_str, 2, len(@short_str) - 1) IF @short_str <> ''
Insert INTO @tmp
select @short_str
END
END RETURN
END
创建函数 FieldCamel
CREATE FUNCTION FieldCamel(@str varchar(100))
returns varchar(100)
AS
BEGIN
declare @splitIndex int;
set @splitIndex = CHARINDEX('_', @str)
if @splitIndex < 1 return (lower(@str))
declare @result varchar(100);
set @result = (select '' + short_str from StrSplit(@str, '_') for xml path(''));
set @result = lower(SUBSTRING(@result, 1, 1)) + SUBSTRING(@result, 2, len(@result) - 1);
return (@result)
END
执行sql
select '/**'+char(13)+char(10)
+'*'+isnull(CONVERT(VARCHAR(100),g.value),'') + char(13)+char(10)
+'*/'+char(13)+char(10)
+'@TableField("'+ a.name +'")' + char(13)+char(10)
+'private '+
case b.name
WHEN 'bigint' then 'Long'
WHEN 'int' then 'Integer'
WHEN 'smallint' then 'Short'
when 'varchar' then 'String'
when 'nvarchar' then 'String'
when 'text' then 'String'
when 'decimal' then 'BigDecimal'
when 'numeric' then 'BigDecimal'
when 'bit' then 'Boolean'
when 'float' then 'BigDecimal'
when 'datetime' then 'Date'
when 'datetime2' then 'Date'
else 'String' end
+' '+ dbo.FieldCamel(a.name)
+';'+char(13)+char(10)
--,b.name,a.max_length ,g.value,b.name,a.name
from sys.columns a
LEFT join sys.types b on a.user_type_id=b.user_type_id
LEFT join sys.extended_properties g on a.object_id=g.major_id and a.column_id=g.minor_id
where object_id=object_id('dbo.wx_user');
Comments | NOTHING