Read Time:40 Second
Persistence Layer api
- 使得微服务在读、写不同的数据库; 如: MySql, MongoDB, SqlLite, PostgreSQL; 都有一统一的接口。
- 各 SQL Server; 如: MySql, SqlLite, PostgreSQL; 接口实现的集中化;避免重复相同或类似的代码实现。
微服务在读、写不同的数据库;如: MySql, MongoDB, SqlLite, PostgreSQL; 都有一统一的接口。
请先参考 5/6 : type SQLHandler struct 实现type OrderDBHandler interface。
请先参考 3/6: type MySQLHandler struct 包含了 *SQLHandler。所以, 当是使用 MySQL 时, 就可回传 &MySQLHandler。
也就是说, 因为, type SQLHandler struct 实现了 type OrderDBHandler interface。所以, 当工厂 func GetDatabaseHandler 的回传是 type OrderDBHandler interface 时, 只要是有包含 *SQLHandler 的 DB Handler, 都可以回传其 DB Handler。
type SQLHandler struct 实现了 type OrderDBHandler interface。
所以, 只要是有包含 *SQLHandler 的 DB Handler; 如: type MySQLHandler struct, 就都可调用 type SQLHandler struct 的实现。
handler.QueryRow(fmt.Sprintf(“select * from Orders where orders = ‘%s‘”, orderno)); 利用 fmt.Sprintf 处理各数据库在Query 命令上的差别。如: ? mysql or sqlite 需要 ?, PostgreSQL 需要 $1。
各SQL Server; 如: MySql, SqlLite, PostgreSQL; 接口实现的集中化; 避免重复相同或类似的代码实现。