1.Create a database engine just like sql.Open, commonly you just need create once. Please notice, Create function will be deprecated, use NewEngine instead.
_, err = session.Exec("delete from userinfo where username = ?", user2.Username)
if err != nil {
session.Rollback()
return
}
// add Commit() after all actions
err = session.Commit()
if err != nil {
return
}
```
5.Derive mapping
Please see derive.go in examples folder.
## Mapping Rules
<aname="mapping"id="mapping"></a>
1.Struct and struct's fields name should be Pascal style, and the table and column's name default is SQL style.
For example:
The struct's Name 'UserInfo' will turn into the table name 'user_info', the same as the keyname. If the keyname is 'UserName' will turn into the select colum 'user_name'
2.If You want change the mapping rules, you have two methods. One is to implement your own Map struct interface according IMapper, you can find the interface in mapper.go and set it to engine.Mapper
Another is use field tag, field tag support the below keywords which split with space:
<table>
<tr>
<td>name</td><td>column name, if no this name, the name is auto generated according field name and mapper rule.</td>
</tr>
<tr>
<td>pk</td><td>the field is a primary key</td>
</tr>
<tr>
<td>more than 30 column type supported, please see [Column Type](https://github.com/lunny/xorm/blob/master/docs/COLUMNTYPE.md)</td><td>column type</td>
</tr>
<tr>
<td>autoincr</td><td>auto incrment</td>
</tr>
<tr>
<td>[not ]null</td><td>if column can be null value</td>
</tr>
<tr>
<td>unique or unique(uniquename)</td><td>unique or union unique as uniquename</td>
</tr>
<tr>
<td>index or index(indexname)</td><td>index or union index as indexname</td>
</tr>
<tr>
<td>extends</td><td>used in anonymous struct means mapping this struct's fields to table</td>
</tr>
<tr>
<td>-</td><td>this field is not map as a table column</td>
</tr>
<tr>
<td>-></td><td>this field only write to db and not read from db</td>
</tr>
<tr>
<td><-</td><td>this field only read from db and not write to db</td>
</tr>
<tr>
<td>created</td><td>this field will auto fill current time when insert</td>
</tr>
<tr>
<td>updated</td><td>this field will auto fill current time when update</td>
</tr>
<tr>
<td>default 0 or default 'abc'</td><td>default value, use single quote for string</td>
</tr>
</table>
For Example
```Go
type Userinfo struct {
Uid int `xorm:"id pk not null autoincr"`
Username string `xorm:"unique"`
Departname string
Alias string `xorm:"-"`
Created time.Time
}
```
3.For customize table name, use Table() function, for example: