|
|
@@ -474,6 +474,34 @@ id := engine.SqlMapClient(key, ¶mMap).Query().Results[0]["id"] //返回查
|
|
|
id := engine.SqlTemplateClient(key, ¶mMap).Query().Results[0]["id"] //返回查询结果的第一条数据的id列的值
|
|
|
```
|
|
|
|
|
|
+* 在sqltemplate中使用自定义函数(目前暂支持默认的HTMLTemplate,其他两种TODO,但也有变通方法使用)
|
|
|
+```
|
|
|
+//定义的自定义函数
|
|
|
+func ShowTimestamp(now time.Time) string {
|
|
|
+ return fmt.Sprintf("%d", now.Unix())
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ engine, err := xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", "root", "", "127.0.0.1", "3306", "mysql"))
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ engine.ShowExecTime(true)
|
|
|
+ engine.ShowSQL(true)
|
|
|
+ tpl := xorm.Default("./templates", ".tpl")
|
|
|
+ tpl.SetFuncs("test.tpl", xorm.FuncMap{"ShowTimestamp": ShowTimestamp})
|
|
|
+ err = engine.RegisterSqlTemplate(tpl)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ result := engine.SqlTemplateClient("test.tpl",&map[string]interface{}{"now":time.Now()}).Query()
|
|
|
+}
|
|
|
+
|
|
|
+//模板中编写的sql内容如下
|
|
|
+select * from user{{ShowTimestamp .now}}
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
# 事务模型
|
|
|
|
|
|
* 本xorm版本同时支持简单事务模型和嵌套事务模型进行事务处理,当使用简单事务模型进行事务处理时,需要创建Session对象,另外当使用Sql()、SqlMapClient()、SqlTemplateClient()方法进行操作时也推荐手工创建Session对象方式管理Session。在进行事物处理时,可以混用ORM方法和RAW方法。注意如果您使用的是mysql,数据库引擎为innodb事务才有效,myisam引擎是不支持事务的。示例代码如下:
|