### 名稱映射規則
名稱映射規則主要負責結構體名稱到表名和結構體field到表字段的名稱映射。由core.IMapper接口的實現者來管理,xorm內置了三種IMapper實現:`core.SnakeMapper` , `core.SameMapper`和`core.GonicMapper`。SnakeMapper支持struct為駝峰式命名,表結構為下劃線命名之間的轉換;SameMapper支持結構體名稱和對應的表名稱以及結構體field名稱與對應的表字段名稱相同的命名。
當前SnakeMapper為默認值,如果需要改變時,在engine創建完成后使用
~~~
engine.SetMapper(core.SameMapper{})
~~~
同時需要注意的是:
- 如果你使用了別的命名規則映射方案,也可以自己實現一個IMapper。
- 表名稱和字段名稱的映射規則默認是相同的,當然也可以設置為不同,如:
~~~
engine.SetTableMapper(core.SameMapper{})
engine.SetColumnMapper(core.SnakeMapper{})
~~~
When a struct auto mapping to a database's table, the below table describes how they change to each other:
| go type's kind | value method | xorm type |
|-----|-----|-----|
| implemented Conversion | Conversion.ToDB / Conversion.FromDB | Text |
| int, int8, int16, int32, uint, uint8, uint16, uint32 | | Int |
| int64, uint64 | | BigInt |
| float32 | | Float |
| float64 | | Double |
| complex64, complex128 | json.Marshal / json.UnMarshal | Varchar(64) |
| []uint8 | | Blob |
| array, slice, map except []uint8 | json.Marshal / json.UnMarshal | Text |
| bool | 1 or 0 | Bool |
| string | | Varchar(255) |
| time.Time | | DateTime |
| cascade struct | primary key field value | BigInt |
|
| struct | json.Marshal / json.UnMarshal | Text |
| Others | | Text |