|
|
10 years ago | |
|---|---|---|
| docs | 10 years ago | |
| test | 10 years ago | |
| .gitignore | 10 years ago | |
| LICENSE | 10 years ago | |
| README.md | 10 years ago | |
| cover.out | 10 years ago | |
| engine.go | 10 years ago | |
| engineplus.go | 10 years ago | |
| error.go | 10 years ago | |
| goracle_driver.go | 10 years ago | |
| helpers.go | 10 years ago | |
| helpersplus.go | 10 years ago | |
| logger.go | 10 years ago | |
| lru_cacher.go | 10 years ago | |
| memroy_store.go | 10 years ago | |
| mssql_dialect.go | 10 years ago | |
| mymysql_driver.go | 10 years ago | |
| mysql_dialect.go | 10 years ago | |
| mysql_driver.go | 10 years ago | |
| oci8_driver.go | 10 years ago | |
| odbc_driver.go | 10 years ago | |
| oracle_dialect.go | 10 years ago | |
| pg_reserved.txt | 10 years ago | |
| postgres_dialect.go | 10 years ago | |
| pq_driver.go | 10 years ago | |
| processors.go | 10 years ago | |
| rows.go | 10 years ago | |
| session.go | 10 years ago | |
| sessionplus.go | 10 years ago | |
| sqlite3_dialect.go | 10 years ago | |
| sqlite3_driver.go | 10 years ago | |
| sqlmap.go | 10 years ago | |
| sqltemplate.go | 10 years ago | |
| statement.go | 10 years ago | |
| syslogger.go | 10 years ago | |
| xorm.go | 10 years ago | |
| xormplus.go | 10 years ago |
###优化xorm的查询API,并提供类似ibatis的配置文件及动态SQL功能
go get -u github.com/xormplus/xorm
var err error
db, err = xorm.NewPostgreSQL("postgres://postgres:root@localhost:5432/testdb?sslmode=disable")
if err != nil {
t.Fatal(err)
}
err = db.InitSqlMap() //初始化SqlMap配置,可选功能
if err != nil {
t.Fatal(err)
}
err = db.InitSqlTemplate() //初始化动态SQL模板配置,可选功能
if err != nil {
t.Fatal(err)
}
####db.InitSqlMap()过程
####db.InitSqlTemplate()过程
###支持类似这样的链式读取数据操作
sql := "select id,title,createdatetime,content from article where id = ?"
rows, err := db.Sql(sql, 2).Query().Json() //返回查询结果的json字符串
rows, err := db.Sql("sql", 2).QueryWithDateFormat("20060102").Json() //返回查询结果的json字符串,并支持格式化日期
rows, err := db.Sql("sql", 2).QueryWithDateFormat("20060102").Xml() //返回查询结果的xml字符串,并支持格式化日期
id := db.Sql(sql, 2).Query().Result[0]["id"] //返回查询结果的第一条数据的id列的值
title := db.Sql(sql, 2).Query().Result[0]["title"]
createdatetime := db.Sql(sql, 2).Query().Result[0]["createdatetime"]
content := db.Sql(sql, 2).Query().Result[0]["content"]
articles := make([]Article, 0)
xml,err := db.Where("id=?", 6).Find(&articles).Xml() //返回查询结果的xml字符串
json,err := db.Where("id=?", 6).Find(&articles).Json() //返回查询结果的json字符串
sql := "select id,title,createdatetime,content from article where id = ?id and userid=?userid"
paramMap := map[string]interface{}{"id": 6, "userid": 1} //支持参数使用map存放
rows, err := db.Sql(sql, ¶mMap).QueryByParamMap().XmlIndent("", " ", "article")
###支持SqlMap配置,配置文件样例
<sqlMap>
<sql id="selectAllArticle">
select id,title,createdatetime,content
from article where id in (?1,?2)
</sql>
<sql id="selectStudentById1">
select * from article where id=?id
</sql>
</sqlMap>
paramMap := map[string]interface{}{"1": 2, "2": 5} //支持参数使用map存放
rows, err := db.SqlMapClient("selectAllArticle", ¶mMap).QueryByParamMap().Xml() //返回查询结果的xml字符串
rows, err := db.SqlMapClient("selectAllArticle", ¶mMap).QueryByParamMap().Json() //返回查询结果的json字符串
rows, err := db.SqlMapClient("selectAllArticle", ¶mMap).QueryByParamMapWithDateFormat("2006/01/02").XmlIndent("", " ", "article") //返回查询结果格式化的xml字符串,并支持格式化日期
###提供动态SQL支持,使用pongo2模板引擎 例如配置文件名:select.example.stpl
配置样例内容如下:select id,userid,title,createdatetime,content
from article where
{% if count>1%}
id=?id
{% else%}
userid=?userid
{% endif %}
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().Json()
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("2006/01/02").Json()
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("2006/01/02").XmlIndent("", " ", "article")
###讨论 请加入QQ群:280360085 进行讨论。API设计相关建议可联系本人QQ:50892683