数据库模板引擎,适用于Golang。
核心思想来源于https://gitee.com/xiandafu/beetl
go get -u github.com/guangzhou-meta/beetl-go
/resources/sql/DemoTest.md
DemoTest
===
```sql
select #{page('*')} from sys_user
where 1=1
-- @if(!isEmpty(name)){
and name=#{name}
-- @}
-- @pageIgnoreTag(){
order by createTime
-- @}
```[实际可参考 test/resources/sql/DemoTest.md]
DemoTest2
===
```sql
select #{page('*')} from sys_user
where 1=1
-- @if(!isEmpty(name)){
and name=#{name}
-- @}
-- @pageIgnoreTag(){
order by createTime
-- @}
```[实际可参考 test/resources/sql/DemoTest.md]
/resources/beetl-default.properties
参考 resources/beetl-default.properties
package dao
import (
"github.com/guangzhou-meta/beetl-go/common"
"github.com/guangzhou-meta/beetl-go/engine"
"github.com/guangzhou-meta/beetl-go/template"
)
type DemoTest struct {
DemoTest func(params engine.RenderParamsI) ([]common.SqlResultI, error)
}
func (*DemoTest) TemplateName() string {
return "DemoTest"
}
package main
import (
"fmt"
"os"
)
import (
"github.com/guangzhou-meta/beetl-go/engine"
"github.com/guangzhou-meta/beetl-go/template"
)
func init() {
root, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
templateEngine := engine.NewEngine().
SetTemplateDir(root + "/resources/sql"). // 模板扫描地址
SetPropertiesFilePath(nil). // 自定义配置文件路径
Build()
}
package main
import (
"dao"
)
var demoTestInstance *dao.DemoTest
func register() {
demoTestInstance = &dao.DemoTest{}
templateEngine.RegisterTemplate(demoTestInstance)
}
package main
import (
"dao"
"fmt"
)
import (
"github.com/guangzhou-meta/beetl-go/engine"
"github.com/guangzhou-meta/beetl-go/template"
)
var demoTestInstance *dao.DemoTest
type DemoTestDTO struct {
Name *string `json:"name"`
}
func main() {
register() // 注册模板
name := "Unizone"
params := &DemoTestDTO{
Name: &name,
}
sqlList, err := demoTestInstance.DemoTest(engine.NewRenderPageParams().SetObject(params))
if err != nil {
fmt.Printf("Render Fail: %v\n", err)
return
}
for _, sql := range sqlList {
fmt.Println(sql.GetSqlType(), ": ", sql)
}
}