Skip to content

Commit

Permalink
add support Java-SpringBoot-MyBatis template
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Oct 21, 2019
1 parent 206494b commit eb3ffbc
Show file tree
Hide file tree
Showing 21 changed files with 349 additions and 62 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>2</VersionMajor>
<VersionMinor>2</VersionMinor>
<VersionPatch>70</VersionPatch>
<VersionPatch>80</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
74 changes: 67 additions & 7 deletions doc/SmartCode-Spring-Boot.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Module: net.smartsql.code
Module: net.smartsql.demo
Author: Ahoo Wang
DataSource:
Name: DbTable
Parameters:
DbName: smartsql_db
DbProvider: MySql
ConnectionString: Data Source=**;database=smartsql_db;uid=root;pwd=**;Charset=utf8;SslMode=none
ConnectionString: Data Source=serverAddress;database=smartsql_db;uid=root;pwd=smartsql;Charset=utf8;SslMode=none
Language: Java
TemplateEngine:
Name: Razor
Expand All @@ -15,7 +15,7 @@ Output:
Path: 'D://SmartSql-Java'
Parameters:
BuildDir: 'D:\SmartSql-Java\build'

ApplicationName: Demo
NamingConverter:
Table:
Tokenizer:
Expand Down Expand Up @@ -74,7 +74,31 @@ Build:
{Key: 'Pom-Parent.cshtml',Output: {Name: 'pom',Extension: '.xml'}},
{Key: 'Pom-Api.cshtml',Output: {Path: "api",Name: 'pom',Extension: '.xml'}},
{Key: 'Pom-Server.cshtml',Output: {Path: "server",Name: 'pom',Extension: '.xml'}},
{Key: 'Resources/Application-YAML.cshtml',Output: {Path: "server/src/main/resources",Name: 'application',Extension: '.yml'}}
{Key: 'Resources/Application-YAML.cshtml',Output: {Path: "server/src/main/resources",Name: 'application',Extension: '.yml'}},
{Key: 'Application.cshtml',Output:
{
Path: "server/src/main/java/{{Project.Module}}/",
DotSplit: true,
Name: '{{Project.Parameters.ApplicationName}}Application',
Extension: '.java'
}
},
{Key: 'Test/Application-Test.cshtml',Output:
{
Path: "server/src/test/java/{{Project.Module}}/",
DotSplit: true,
Name: '{{Project.Parameters.ApplicationName}}ApplicationTests',
Extension: '.java'
}
},
{Key: 'AppConfig.cshtml',Output:
{
Path: "server/src/main/java/{{Project.Module}}/config",
DotSplit: true,
Name: 'AppConfig',
Extension: '.java'
}
}
]

Entity:
Expand All @@ -90,13 +114,13 @@ Build:
Parameters:
AbstractEntity:
Name: AbstractEntity
Package: net.smartsql.code
Package: net.smartsql.entity
Properties:
- id
- createTime
- modifiedTime
- deleted

SqlMap:
Type: Table
Module: repository
Expand All @@ -108,4 +132,40 @@ Build:
Name: '{{Items.CurrentTable.ConvertedName}}Mapper'
Extension: '.xml'
Parameters:
LogicalDelete: deleted
LogicalDelete: deleted

Repository:
Type: Table
Module: repository
TemplateEngine:
Path: Repository.cshtml
Output:
Path: 'server/src/main/java/{{Project.Module}}.{{Build.Module}}'
DotSplit: true
Name: '{{Items.CurrentTable.ConvertedName}}Repository'
Extension: '.java'
Parameters:
GenericRepository:
Package: net.smartsql.repository

Service:
Type: Table
Module: service
TemplateEngine:
Path: Service.cshtml
Output:
Path: 'server/src/main/java/{{Project.Module}}.{{Build.Module}}'
DotSplit: true
Name: '{{Items.CurrentTable.ConvertedName}}Service'
Extension: '.java'

Controller:
Type: Table
Module: controller
TemplateEngine:
Path: Controller.cshtml
Output:
Path: 'server/src/main/java/{{Project.Module}}.{{Build.Module}}'
DotSplit: true
Name: '{{Items.CurrentTable.ConvertedName}}Controller'
Extension: '.java'
4 changes: 2 additions & 2 deletions doc/SmartCode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Output:
Type: File
Path: 'D:\SmartSql-Starter'
Parameters:
SmartSqlVersion: '4.1.23'
SmartSqlSchemaVersion: '4.1.16'
SmartSqlVersion: '4.1.32'
SmartSqlSchemaVersion: '4.1.29'
BuildDir: 'D:\SmartSql-Starter\build'
DockerImage: 'smartsql.starter'

Expand Down
28 changes: 22 additions & 6 deletions src/SmartCode.App/BuildTasks/MultiTemplateBuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,27 @@ public MultiTemplateBuildTask(IPluginManager pluginManager
_pluginManager = pluginManager;
_logger = logger;
}

public async Task Build(BuildContext context)
{
if (context.Build.Parameters.Value(TEMPLATES_KEY, out IEnumerable templates))
{
foreach (var templateKVs in templates)
{
var _templateKVs = (Dictionary<object, object>)templateKVs;
var _templateKVs = (Dictionary<object, object>) templateKVs;
if (!_templateKVs.Value(TEMPLATE_KEY, out string templateKey))
{
throw new SmartCodeException($"Build:{context.BuildKey},Can not find TemplateKey!");
}

context.Build.TemplateEngine.Path = templateKey;
context.Result = await _pluginManager.Resolve<ITemplateEngine>(context.Build.TemplateEngine.Name).Render(context);
context.Result = await _pluginManager.Resolve<ITemplateEngine>(context.Build.TemplateEngine.Name)
.Render(context);
if (!_templateKVs.Value(TEMPLATE_OUTPUT_KEY, out Dictionary<object, object> outputKVs))
{
throw new SmartCodeException($"Build:{context.BuildKey},Can not find Output!");
}

if (context.Output == null)
{
throw new SmartCodeException($"Build:{context.BuildKey},Output can not be null!");
Expand All @@ -57,39 +61,51 @@ public async Task Build(BuildContext context)
{
Path = context.Output.Path,
Mode = context.Output.Mode,
DotSplit = context.Output.DotSplit,
Name = context.Output.Name,
Extension = context.Output.Extension
};
if (outputKVs.Value(nameof(Output.Path), out string outputPath))
{
output.Path = outputPath;
}

if (outputKVs.Value(nameof(Output.Mode), out CreateMode outputMode))
{
output.Mode = outputMode;
}

if (outputKVs.Value(nameof(Output.DotSplit), out string dotSplit))
{
output.DotSplit = Convert.ToBoolean(dotSplit);
}

if (String.IsNullOrEmpty(output.Path))
{
throw new SmartCodeException($"Build:{context.BuildKey},Template:{templateKey},can not find Output.Path!");
throw new SmartCodeException(
$"Build:{context.BuildKey},Template:{templateKey},can not find Output.Path!");
}

if (!outputKVs.Value(nameof(Output.Name), out string outputName))
{
throw new SmartCodeException($"Build:{context.BuildKey},Template:{templateKey},can not find Output.Name!");
throw new SmartCodeException(
$"Build:{context.BuildKey},Template:{templateKey},can not find Output.Name!");
}

output.Name = outputName;

if (outputKVs.Value(nameof(Output.Extension), out string extension))
{
output.Extension = extension;
}

await _pluginManager.Resolve<IOutput>(context.Output.Type).Output(context, output);
}
}
}

public void Initialize(IDictionary<string, object> parameters)
{

}
}
}
}
2 changes: 1 addition & 1 deletion src/SmartCode.App/Outputs/FileOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public async Task Output(BuildContext context, Output output = null)

using (StreamWriter streamWriter = new StreamWriter(filePath))
{
await streamWriter.WriteAsync(context.Result);
await streamWriter.WriteAsync(context.Result.Trim());
}

_logger.LogInformation($"------ Mode:{output.Mode},Build:{context.BuildKey} -> {filePath} End! ------");
Expand Down
2 changes: 1 addition & 1 deletion src/SmartCode.CLI/SmartCode.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.4" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.4.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
Expand Down
14 changes: 7 additions & 7 deletions src/SmartCode.Db/SmartCode.Db.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SmartSql.Options" Version="4.1.23" />
<PackageReference Include="SmartSql.TypeHandler" Version="4.1.23" />
<PackageReference Include="SmartSql.TypeHandler.PostgreSql" Version="4.1.23" />
<PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
<PackageReference Include="SmartSql.Options" Version="4.1.32" />
<PackageReference Include="SmartSql.TypeHandler" Version="4.1.32" />
<PackageReference Include="SmartSql.TypeHandler.PostgreSql" Version="4.1.32" />
<PackageReference Include="System.Data.SqlClient" Version="4.7.0" />
<PackageReference Include="System.Data.SQLite" Version="1.0.110" />
<PackageReference Include="MySql.Data" Version="8.0.17" />
<PackageReference Include="Npgsql" Version="4.0.9" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.19.31" />
<PackageReference Include="MySql.Data" Version="8.0.18" />
<PackageReference Include="Npgsql" Version="4.1.1" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.19.50" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/SmartCode.ETL/SmartCode.ETL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SmartSql.Bulk.MySql" Version="4.1.23" />
<PackageReference Include="SmartSql.Bulk.PostgreSql" Version="4.1.23" />
<PackageReference Include="SmartSql.Bulk.SqlServer" Version="4.1.23" />
<PackageReference Include="SmartSql.Bulk.MySql" Version="4.1.32" />
<PackageReference Include="SmartSql.Bulk.PostgreSql" Version="4.1.32" />
<PackageReference Include="SmartSql.Bulk.SqlServer" Version="4.1.32" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/SmartCode.Generator/RazorTemplates/Java/AppConfig.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@using SmartCode
@model BuildContext
@{
Layout = "_JavaLayout.cshtml";
var project = Model.Project;
var buildTask = Model.Build;
}


package @(project.Module).config;

public class AppConfig {

}
23 changes: 20 additions & 3 deletions src/SmartCode.Generator/RazorTemplates/Java/Application.cshtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package me.ahoo.demo;
@using SmartCode
@model BuildContext
@{
Layout = "_JavaLayout.cshtml";
var project = Model.Project;

var appName = "Demo";

if (project.Parameters.TryGetValue("ApplicationName", out var _appName))
{
appName = _appName.ToString();
}
var appClassName = $"{appName}Application";
}

package @(project.Module);

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@@SpringBootApplication
public class DemoApplication {
@@EnableSwagger2
public class @appClassName {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
SpringApplication.run(@(appClassName).class, args);
}

}
41 changes: 41 additions & 0 deletions src/SmartCode.Generator/RazorTemplates/Java/Controller.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@using SmartCode
@model BuildContext
@{
Layout = "_JavaLayout.cshtml";
var project = Model.Project;
var buildTask = Model.Build;
var table = Model.GetCurrentTable();
var pkCol = table.PKColumn;
if (pkCol == null)
{
throw new SmartCodeException($"Service Template can not find PKColumn,Table:{table.Name}.");
}
var pkType = pkCol.LanguageType;
var insertRetType = table.AutoIncrement ? pkType : "int";
var entityName = table.ConvertedName;
var entityCamelName = NamingUtil.CamelCase(table.ConvertedName);
var serviceName = $"{table.ConvertedName}Service";
var serviceCamelName = NamingUtil.CamelCase(serviceName);
var controllerName = $"{table.ConvertedName}Controller";
}



package @(project.Module).@buildTask.Module;

import lombok.var;

import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import @(project.Module).service.@serviceName;

@Html.NewLine()
@@RestController
@@RequestMapping("@entityCamelName")
public class @controllerName {
@@Autowired
@serviceName @serviceCamelName;

}
2 changes: 1 addition & 1 deletion src/SmartCode.Generator/RazorTemplates/Java/Entity.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import java.util.UUID;
import java.util.Date;
@if (useAbstractEntity)
{
<text>import @abstractEntityPackage;</text>
<text>import @($"{abstractEntityPackage}.{abstractEntityName}");</text>
}

import lombok.Data;
Expand Down
Loading

0 comments on commit eb3ffbc

Please sign in to comment.