Skip to content

Commit

Permalink
支持自定义实体代码后缀
Browse files Browse the repository at this point in the history
  • Loading branch information
ixre committed Dec 30, 2020
1 parent cc52437 commit 37be0a1
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 66 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Usage of tto:
- \#!lang: 指定当前生成代码的语言
如:
```
#!target:java/{{.global.pkg}}/pojo/{{.table.Title}}Entity.java
#!target:java/{{.global.pkg}}/pojo/{{.table.Title}}{{.global.entity_suffix}}.java
```
多个预定义表达式可以放在一行
```
Expand Down Expand Up @@ -295,7 +295,7 @@ base_path="/api"
以下代码用于生成Java的Pojo对象, 更多示例点击[这里](templates)
```
#!target:{{.global.pkg}}/pojo/{{.table.Title}}Entity.java
#!target:{{.global.pkg}}/pojo/{{.table.Title}}{{.global.entity_suffix}}.java
package {{pkg "java" .global.pkg}}.pojo;
import javax.persistence.Basic;
Expand All @@ -309,7 +309,7 @@ import javax.persistence.GeneratedValue;
/** {{.table.Comment}} */
@Entity
@Table(name = "{{.table.Name}}", schema = "{{.table.Schema}}")
public class {{.table.Title}}Entity {
public class {{.table.Title}}{{.global.entity_suffix}} {
{{range $i,$c := .columns}}{{$type := type "java" $c.Type}}
private {{$type}} {{$c.Name}}
public void set{{$c.Prop}}({{$type}} {{$c.Name}}){
Expand Down
4 changes: 4 additions & 0 deletions bin/tto.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func generate() {
if re.GetBoolean("code.id_upper") {
dg.UseUpperId()
}
// 实体后缀
if suffix := re.GetString("code.entity_suffix");suffix != ""{
dg.Var(tto.ENTITY_SUFFIX,suffix)
}
// 获取表格并转换
userMeta := re.GetBoolean("code.meta_settings")
tables, err := dg.Parses(list, userMeta)
Expand Down
2 changes: 1 addition & 1 deletion core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// 版本号
const BuildVersion = "0.4.0"
const BuildVersion = "0.4.1"

// 代码页
const ReleaseCodeHome = "https://github.com/ixre/tto"
Expand Down
3 changes: 3 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
BASE_PATH = "base_path"
// 包名
PKG = "pkg"
// 实体后缀,默认:Entity
ENTITY_SUFFIX = "entity_suffix"
// 当前时间
TIME = "time"
// 版本
Expand Down Expand Up @@ -125,6 +127,7 @@ func (s *sessionImpl) init() Session {
// predefine default vars
s.Var(BASE_URL, "")
s.Var(BASE_PATH, "")
s.Var(ENTITY_SUFFIX,"Entity")
// load global registry
rd := GetRegistry()
for _, k := range rd.Keys {
Expand Down
8 changes: 4 additions & 4 deletions templates/java/java/_IRepo.java_
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import com.gcy.sz.repo.model.*;
/** {{.table.Comment}}仓储 */
public interface I{{.table.Title}}Repo {
/** 获取{{.table.Comment}} */
{{.table.Title}}Entity get(Serializable id);
{{.table.Title}}{{.global.entity_suffix}} get(Serializable id);
/** 根据条件获取单条{{.table.Comment}} */
{{.table.Title}}Entity get{{.table.Title}}By(String where,Map<String,Object> params);
{{.table.Title}}{{.global.entity_suffix}} get{{.table.Title}}By(String where,Map<String,Object> params);
/** 根据条件获取多条{{.table.Comment}} */
List<{{.table.Title}}Entity> select{{.table.Title}}(String where,Map<String,Object> params);
List<{{.table.Title}}{{.global.entity_suffix}}> select{{.table.Title}}(String where,Map<String,Object> params);
/** 保存{{.table.Comment}} */
int save{{.table.Title}}({{.table.Title}}Entity v);
int save{{.table.Title}}({{.table.Title}}{{.global.entity_suffix}} v);
/** 删除{{.table.Comment}} */
Error delete{{.table.Title}}(Serializable id);
/** 批量删除{{.table.Comment}} */
Expand Down
14 changes: 7 additions & 7 deletions templates/java/java/_Repo.java_
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ public class {{.table.Title}}RepoImpl implements I{{.table.Title}}Repo {
/** 注入上下文 */
@Inject XContext ctx;
/** 获取{{.table.Comment}} */
public {{.table.Title}}Entity get(Serializable id){
public {{.table.Title}}{{.global.entity_suffix}} get(Serializable id){
TinySession s = this.ctx.hibernate();
{{.table.Title}}Entity e = s.get({{.table.Title}}Entity.class,id);
{{.table.Title}}{{.global.entity_suffix}} e = s.get({{.table.Title}}{{.global.entity_suffix}}.class,id);
return e;
}
/** 根据条件获取单条{{.table.Comment}} */
public {{.table.Title}}Entity get{{.table.Title}}By(String where,Map<String,Object> params){
public {{.table.Title}}{{.global.entity_suffix}} get{{.table.Title}}By(String where,Map<String,Object> params){
TinySession s = this.ctx.hibernate();
{{.table.Title}}Entity e = s.get({{.table.Title}}Entity.class,where, params);
{{.table.Title}}{{.global.entity_suffix}} e = s.get({{.table.Title}}{{.global.entity_suffix}}.class,where, params);
s.close();
return e;
}

/** 根据条件获取多条{{.table.Comment}} */
public List<{{.table.Title}}Entity> select{{.table.Title}}(String where,Map<String,Object> params){
public List<{{.table.Title}}{{.global.entity_suffix}}> select{{.table.Title}}(String where,Map<String,Object> params){
TinySession s = this.ctx.hibernate();
List<{{.table.Title}}Entity> list = s.select({{.table.Title}}Entity.class,
List<{{.table.Title}}{{.global.entity_suffix}}> list = s.select({{.table.Title}}{{.global.entity_suffix}}.class,
"SELECT * FROM {{.table.Name}} WHERE "+where, params);
s.close();
return list;
}

/** 保存{{.table.Comment}} */
public int save{{.table.Title}}({{.table.Title}}Entity v){
public int save{{.table.Title}}({{.table.Title}}{{.global.entity_suffix}} v){
TinySession s = this.ctx.hibernate();
s.save(v);
s.close();
Expand Down
4 changes: 2 additions & 2 deletions templates/java/java/spring_jpa_repo.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!target:java/{{.global.pkg}}/repo/{{.table.Title}}Repository.java
package {{pkg "java" .global.pkg}}.repo;

import {{pkg "java" .global.pkg}}.pojo.{{.table.Title}}Entity
import {{pkg "java" .global.pkg}}.pojo.{{.table.Title}}{{.global.entity_suffix}}
import org.springframework.data.jpa.repository.JpaRepository
{{$pkType := type "java" .table.PkType}}
/** {{.table.Comment}}仓储接口 */
public interface {{.table.Title}}Repository : JpaRepository<{{.table.Title}}Entity, {{$pkType}}>{
public interface {{.table.Title}}Repository : JpaRepository<{{.table.Title}}{{.global.entity_suffix}}, {{$pkType}}>{

}
8 changes: 4 additions & 4 deletions templates/java/java/spring_service.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!target:java/{{.global.pkg}}/service/{{.table.Title}}Service.java
package {{pkg "java" .global.pkg}}.service

import {{pkg "java" .global.pkg}}.pojo.{{.table.Title}}Entity
import {{pkg "java" .global.pkg}}.pojo.{{.table.Title}}{{.global.entity_suffix}}
import {{pkg "java" .global.pkg}}.repo.{{.table.Title}}Repository
import org.springframework.stereotype.Service
import java.util.*
Expand All @@ -15,13 +15,13 @@ public class {{.table.Title}}Service {
private {{$tableTitle}}Repository repo;

/** 保存{{.table.Comment}} */
public {{$tableTitle}}Entity save{{$tableTitle}}({{$tableTitle}}Entity {{.table.Name}}){
public {{$tableTitle}}{{.global.entity_suffix}} save{{$tableTitle}}({{$tableTitle}}{{.global.entity_suffix}} {{.table.Name}}){
return this.repo.save({{.table.Name}})
}


/** 批量保存{{.table.Comment}} */
public Iterable<{{$tableTitle}}Entity> saveAll{{$tableTitle}}(Iterable<{{$tableTitle}}Entity> entities){
public Iterable<{{$tableTitle}}{{.global.entity_suffix}}> saveAll{{$tableTitle}}(Iterable<{{$tableTitle}}{{.global.entity_suffix}}> entities){
return this.repo.saveAll(entities);
}

Expand All @@ -31,7 +31,7 @@ public void deleteById({{$pkType}} id) {
}

/** 查找{{.table.Comment}} */
public Optional<{{$tableTitle}}Entity> findById({{$pkType}} id){
public Optional<{{$tableTitle}}{{.global.entity_suffix}}> findById({{$pkType}} id){
return this.repo.findById(id);
}
}
4 changes: 2 additions & 2 deletions templates/java/kotlin/class.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!target:kotlin/{{.global.pkg}}/model/{{.table.Title}}Entity.kt
#!target:kotlin/{{.global.pkg}}/model/{{.table.Title}}{{.global.entity_suffix}}.kt
package {{pkg "kotlin" .global.pkg}}.model;

/** {{.table.Comment}} */
Expand All @@ -8,7 +8,7 @@ class {{.table.Title}}{
var {{lower_title $c.Prop}}:{{type "kotlin" $c.Type}} = {{default "kotlin" $c.Type}} {{end}}

/** 拷贝数据 */
fun copy(src :{{.table.Title}}Entity):{{.table.Title}}Entity{
fun copy(src :{{.table.Title}}{{.global.entity_suffix}}):{{.table.Title}}{{.global.entity_suffix}}{
val dst = this;
{{range $i,$c := .columns}}
dst.{{lower_title $c.Prop}} = src.{{lower_title $c.Prop}}{{end}}
Expand Down
8 changes: 4 additions & 4 deletions templates/java/quarkus/pojo.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!target:src/main/java/{{.global.pkg}}/pojo/{{.table.Title}}Entity.java
#!target:src/main/java/{{.global.pkg}}/pojo/{{.table.Title}}{{.global.entity_suffix}}.java
package {{pkg "java" .global.pkg}}.pojo;

import javax.persistence.Basic;
Expand All @@ -12,7 +12,7 @@
/** {{.table.Comment}} */
@Entity
@Table(name = "{{.table.Name}}", schema = "{{.table.Schema}}")
public class {{.table.Title}}Entity {
public class {{.table.Title}}{{.global.entity_suffix}} {
{{range $i,$c := .columns}}{{$type := type "java" $c.Type}}

{{if $c.IsPk}}\
Expand All @@ -34,8 +34,8 @@ public class {{.table.Title}}Entity {
{{end}}

/** 拷贝数据 */
public {{.table.Title}}Entity copy({{.table.Title}}Entity src){
{{.table.Title}}Entity dst = new {{.table.Title}}Entity();
public {{.table.Title}}{{.global.entity_suffix}} copy({{.table.Title}}{{.global.entity_suffix}} src){
{{.table.Title}}{{.global.entity_suffix}} dst = new {{.table.Title}}{{.global.entity_suffix}}();
{{range $i,$c := .columns}}
dst.set{{$c.Prop}}(src.get{{$c.Prop}}());{{end}}
return dst;
Expand Down
4 changes: 2 additions & 2 deletions templates/java/quarkus/quarkus_repo.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!target:src/main/kotlin/{{.global.pkg}}/repo/{{.table.Title}}JpaRepository.kt
package {{pkg "kotlin" .global.pkg}}.repo;

import {{pkg "kotlin" .global.pkg}}.pojo.{{.table.Title}}Entity
import {{pkg "kotlin" .global.pkg}}.pojo.{{.table.Title}}{{.global.entity_suffix}}
import io.quarkus.hibernate.orm.panache.PanacheRepository
import javax.enterprise.context.ApplicationScoped

{{$pkType := type "kotlin" .table.PkType}}
/** {{.table.Comment}}仓储 */
@ApplicationScoped
class {{.table.Title}}JpaRepository : PanacheRepository<{{.table.Title}}Entity> {
class {{.table.Title}}JpaRepository : PanacheRepository<{{.table.Title}}{{.global.entity_suffix}}> {

}
18 changes: 9 additions & 9 deletions templates/java/quarkus/quarkus_service.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!target:src/main/kotlin/{{.global.pkg}}/service/{{.table.Title}}Service.kt
package {{pkg "kotlin" .global.pkg}}.service

import {{pkg "kotlin" .global.pkg}}.pojo.{{.table.Title}}Entity
import {{pkg "kotlin" .global.pkg}}.pojo.{{.table.Title}}{{.global.entity_suffix}}
import {{pkg "kotlin" .global.pkg}}.repo.{{.table.Title}}JpaRepository
import javax.inject.Inject
import javax.enterprise.inject.Default
Expand All @@ -23,29 +23,29 @@ class {{.table.Title}}Service {
fun parseId(id:Any):Long{return TypesConv.toLong(id)}

/** 根据ID查找{{.table.Comment}} */
fun findByIdOrNull(id:{{$pkType}}):{{$tableTitle}}Entity?{
fun findByIdOrNull(id:{{$pkType}}):{{$tableTitle}}{{.global.entity_suffix}}?{
return this.repo.findByIdOptional(this.parseId(id))?.get()
}

/** 根据条件查找单个对象 */
fun findBy(query:String,vararg params:Any):{{$tableTitle}}Entity?{
return this.repo.find(query,params).singleResultOptional<{{$tableTitle}}Entity>()?.get()
fun findBy(query:String,vararg params:Any):{{$tableTitle}}{{.global.entity_suffix}}?{
return this.repo.find(query,params).singleResultOptional<{{$tableTitle}}{{.global.entity_suffix}}>()?.get()
}

/** 根据条件查找并返回列表 */
fun listBy(query:String,params:List<Any>):List<{{$tableTitle}}Entity>{
fun listBy(query:String,params:List<Any>):List<{{$tableTitle}}{{.global.entity_suffix}}>{
return this.repo.list(query,params)
}

/** 保存{{.table.Comment}} */
@Transactional
fun save{{$tableTitle}}(e: {{$tableTitle}}Entity):Error? {
fun save{{$tableTitle}}(e: {{$tableTitle}}{{.global.entity_suffix}}):Error? {
return catch {
var dst: {{$tableTitle}}Entity
var dst: {{$tableTitle}}{{.global.entity_suffix}}
if (e.{{$pkProp}} > 0) {
dst = this.repo.findById(this.parseId(e.{{$pkProp}}))!!
} else {
dst = {{$tableTitle}}Entity()
dst = {{$tableTitle}}{{.global.entity_suffix}}()
{{$c := try_get .columns "create_time"}}\
{{if $c }}dst.createTime = Types.time.unix().toLong(){{end}}
}
Expand All @@ -60,7 +60,7 @@ class {{.table.Title}}Service {

/** 批量保存{{.table.Comment}} */
@Transactional
fun saveAll{{$tableTitle}}(entities:Iterable<{{$tableTitle}}Entity>){
fun saveAll{{$tableTitle}}(entities:Iterable<{{$tableTitle}}{{.global.entity_suffix}}>){
this.repo.persist(entities)
this.repo.flush()
}
Expand Down
10 changes: 5 additions & 5 deletions templates/java/quarkus/restful_resource.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!target:src/main/kotlin/{{.global.pkg}}/resources/{{.table.Title}}Resource.kt
package {{pkg "kotlin" .global.pkg}}.resources

import {{pkg "kotlin" .global.pkg}}.pojo.{{.table.Title}}Entity
import {{pkg "kotlin" .global.pkg}}.pojo.{{.table.Title}}{{.global.entity_suffix}}
import {{pkg "kotlin" .global.pkg}}.service.{{.table.Title}}Service
import {{pkg "kotlin" .global.pkg}}.component.TinyQueryComponent
import net.fze.common.Result
Expand All @@ -27,14 +27,14 @@ class {{.table.Title}}Resource {
/** 获取{{.table.Comment}} */
@GET@Path("/{id}")
@PermitAll
fun get(@PathParam("id") id:{{$pkType}}): {{.table.Title}}Entity? {
fun get(@PathParam("id") id:{{$pkType}}): {{.table.Title}}{{.global.entity_suffix}}? {
return service.findByIdOrNull(id)
}

/** 创建{{.table.Comment}} */
@POST
@PermitAll
fun create(entity: {{.table.Title}}Entity):Result {
fun create(entity: {{.table.Title}}{{.global.entity_suffix}}):Result {
val err = this.service.save{{.table.Title}}(entity)
if(err != null)return Result.create(1,err.message)
return Result.OK
Expand All @@ -43,7 +43,7 @@ class {{.table.Title}}Resource {
/** 更新{{.table.Comment}} */
@PUT@Path("/{id}")
@PermitAll
fun save(@PathParam("id") id:{{$pkType}},entity: {{.table.Title}}Entity):Result {
fun save(@PathParam("id") id:{{$pkType}},entity: {{.table.Title}}{{.global.entity_suffix}}):Result {
entity.{{lower_title .table.PkProp}} = id
val err = this.service.save{{.table.Title}}(entity)
if(err != null)return Result.create(1,err.message)
Expand All @@ -63,7 +63,7 @@ class {{.table.Title}}Resource {
/** {{.table.Comment}}列表 */
@GET
@PermitAll
fun list(): List<{{.table.Title}}Entity> {
fun list(): List<{{.table.Title}}{{.global.entity_suffix}}> {
return mutableListOf()
}

Expand Down
12 changes: 6 additions & 6 deletions templates/java/spring/entity.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!target:spring/src/main/java/{{.global.pkg}}/entity/{{.table.Title}}Entity.java
#!target:spring/src/main/java/{{.global.pkg}}/entity/{{.table.Title}}{{.global.entity_suffix}}.java
package {{pkg "java" .global.pkg}}.entity;

import net.fze.util.TypeConv;
Expand All @@ -17,7 +17,7 @@
/** {{.table.Comment}} */
@Entity
@Table(name = "{{.table.Name}}", schema = "{{.table.Schema}}")
public class {{.table.Title}}Entity {
public class {{.table.Title}}{{.global.entity_suffix}} {
{{range $i,$c := .columns}}{{$type := type "java" $c.Type}}
private {{$type}} {{$c.Name}};
public void set{{$c.Prop}}({{$type}} {{$c.Name}}){
Expand All @@ -35,8 +35,8 @@ public class {{.table.Title}}Entity {
{{end}}

/** 拷贝数据 */
public {{.table.Title}}Entity copy({{.table.Title}}Entity src){
{{.table.Title}}Entity dst = new {{.table.Title}}Entity();
public {{.table.Title}}{{.global.entity_suffix}} copy({{.table.Title}}{{.global.entity_suffix}} src){
{{.table.Title}}{{.global.entity_suffix}} dst = new {{.table.Title}}{{.global.entity_suffix}}();
{{range $i,$c := .columns}}
dst.set{{$c.Prop}}(src.get{{$c.Prop}}());{{end}}
return dst;
Expand All @@ -49,8 +49,8 @@ public Map<String,Object> toMap(){
return mp;
}

public static {{.table.Title}}Entity fromMap(Map<String,Object> data){
{{.table.Title}}Entity dst = new {{.table.Title}}Entity();\
public static {{.table.Title}}{{.global.entity_suffix}} fromMap(Map<String,Object> data){
{{.table.Title}}{{.global.entity_suffix}} dst = new {{.table.Title}}{{.global.entity_suffix}}();\
{{range $i,$c := .columns}}
{{ $goType := type "go" $c.Type}}\
{{if eq $goType "int"}}dst.set{{$c.Prop}}(TypeConv.toInt(data.get("{{$c.Prop}}")));\
Expand Down
8 changes: 4 additions & 4 deletions templates/java/spring/java/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import {{pkg "java" .global.pkg}}.service.{{.table.Title}}Service;
import {{pkg "java" .global.pkg}}.entity.{{.table.Title}}Entity;
import {{pkg "java" .global.pkg}}.entity.{{.table.Title}}{{.global.entity_suffix}};
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -42,7 +42,7 @@ public String list(Model m){
/** 修改{{.table.Comment}} */
@GetMapping("/edit{{.table.Title}}")
public String edit{{.table.Title}}({{$pkType}} {{.table.Pk}}, Model m){
{{.table.Title}}Entity entity = this.service.findByIdOrNull(id);
{{.table.Title}}{{.global.entity_suffix}} entity = this.service.findByIdOrNull(id);
if(entity == null)return "admin/nodata";
m.addAttribute("ID",id);
m.addAttribute("Entity",entity);
Expand All @@ -54,7 +54,7 @@ public String list(Model m){
@PostMapping("/save{{.table.Title}}")
@ResponseBody
public Result save{{.table.Title}}(HttpServletRequest req) {
{{.table.Title}}Entity entity = HttpUtils.mapEntity(req, {{.table.Title}}Entity.class);
{{.table.Title}}{{.global.entity_suffix}} entity = HttpUtils.mapEntity(req, {{.table.Title}}{{.global.entity_suffix}}.class);
Error err = Typed.std.tryCatch(() -> {
{{range $i,$c := .columns}}entity.set{{$c.Prop}}(TypeConv.to{{title (type "java" $c.Type)}}(req.getParameter("{{$c.Name}}")));
{{end}}
Expand All @@ -71,7 +71,7 @@ public String list(Model m){
@GetMapping("/{{lower_title .table.Title}}Details")
public String orderDetails(HttpServletRequest req, HttpServletResponse rsp,Model m){
{{$pkType}} {{$pkField}} = TypeConv.to{{title (type "java" .table.PkType)}}(req.getParameter("{{$pkField}}"));
{{.table.Title}}Entity entity = this.service.findByIdOrNull({{$pkField}});
{{.table.Title}}{{.global.entity_suffix}} entity = this.service.findByIdOrNull({{$pkField}});
if(entity == null) {
rsp.setStatus(404);
return "";
Expand Down
Loading

0 comments on commit 37be0a1

Please sign in to comment.