diff --git a/README.md b/README.md index 7c04d89..4059b66 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要 ```Plain Text ./model ├── mappers - │ └── ttmapper.xml - ├── ttmodel.go + │ └── testuserinfomapper.xml + ├── testuserinfomodel.go └── vars.go ``` @@ -35,38 +35,44 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要 * 生成代码示例 ```go - package model import ( "encoding/json" + "time" "github.com/wenj91/gobatis" ) type ( - TtModel interface { - Insert(conn gobatis.GoBatis, data *Tt) (id int64, affected int64, err error) - InsertSelective(conn gobatis.GoBatis, data *Tt) (id int64, affected int64, err error) - FindOne(conn gobatis.GoBatis, id int64) (*Tt, error) - Update(conn gobatis.GoBatis, data *Tt) (affected int64, err error) - UpdateSelective(conn gobatis.GoBatis, data *Tt) (affected int64, err error) - Delete(conn gobatis.GoBatis, id int64) (affected int64, err error) + TestUserInfoModel interface { + WithConn(conn gobatis.GoBatis) TestUserInfoModel + Insert(data *TestUserInfo) (id int64, affected int64, err error) + InsertSelective(data *TestUserInfo) (id int64, affected int64, err error) + FindOne(id int64) (*TestUserInfo, error) + FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error) + FindSelective(data *TestUserInfo) ([]*TestUserInfo, error) + Update(data *TestUserInfo) (affected int64, err error) + UpdateSelective(data *TestUserInfo) (affected int64, err error) + Delete(id int64) (affected int64, err error) } - defaultTtModel struct { + defaultTestUserInfoModel struct { + conn gobatis.GoBatis table string } - Tt struct { - Id int64 `field:"id" json:"id"` - Aid int64 `field:"aid" json:"aid"` - C int64 `field:"c" json:"c"` - NewColumn int64 `field:"new_column" json:"newColumn"` + TestUserInfo struct { + Id int64 `field:"id" json:"id"` + Nanosecond int64 `field:"nanosecond" json:"nanosecond"` + Data string `field:"data" json:"data"` + Content gobatis.NullString `field:"content" json:"content"` + CreateTime time.Time `field:"create_time" json:"createTime"` + UpdateTime time.Time `field:"update_time" json:"updateTime"` } ) - func (m *Tt) ToString() string { + func (m *TestUserInfo) ToString() string { str := "" bs, err := json.Marshal(m) @@ -77,46 +83,68 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要 return str } - func NewTtModel() TtModel { - return &defaultTtModel{ - table: "`tt`", + func NewTestUserInfoModel(conn gobatis.GoBatis) TestUserInfoModel { + return &defaultTestUserInfoModel{ + conn: conn, + table: "`test_user_info`", } } - func (m *defaultTtModel) method(mt string) string { - return "TtMapper." + mt + func (m *defaultTestUserInfoModel) method(mt string) string { + return "TestUserInfoMapper." + mt + } + + func (m *defaultTestUserInfoModel) WithConn(conn gobatis.GoBatis) TestUserInfoModel { + return &defaultTestUserInfoModel{ + conn: conn, + table: "test_user_info", + } } - func (m *defaultTtModel) Insert(conn gobatis.GoBatis, data *Tt) (id int64, affected int64, err error) { - id, affected, err = conn.Insert(m.method("save"), data) + func (m *defaultTestUserInfoModel) Insert(data *TestUserInfo) (id int64, affected int64, err error) { + id, affected, err = m.conn.Insert(m.method("save"), data) return } - func (m *defaultTtModel) InsertSelective(conn gobatis.GoBatis, data *Tt) (id int64, affected int64, err error) { - id, affected, err = conn.Insert(m.method("saveSelective"), data) + func (m *defaultTestUserInfoModel) InsertSelective(data *TestUserInfo) (id int64, affected int64, err error) { + id, affected, err = m.conn.Insert(m.method("saveSelective"), data) return } - func (m *defaultTtModel) FindOne(conn gobatis.GoBatis, id int64) (*Tt, error) { - var resp *Tt - err := conn.Select(m.method("findOne"), map[string]interface{}{ + func (m *defaultTestUserInfoModel) FindOne(id int64) (*TestUserInfo, error) { + var resp *TestUserInfo + err := m.conn.Select(m.method("findOne"), map[string]interface{}{ "Id": id, })(&resp) return resp, err } - func (m *defaultTtModel) Update(conn gobatis.GoBatis, data *Tt) (affected int64, err error) { - affected, err = conn.Update(m.method("update"), data) + func (m *defaultTestUserInfoModel) FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error) { + var resp *TestUserInfo + err := m.conn.Select(m.method("findOneByNanosecond"), map[string]interface{}{ + "Nanosecond": nanosecond, + })(&resp) + return resp, err + } + + func (m *defaultTestUserInfoModel) FindSelective(data *TestUserInfo) ([]*TestUserInfo, error) { + resp := make([]*TestUserInfo, 0) + err := m.conn.Select(m.method("findSelective"), data)(&resp) + return resp, err + } + + func (m *defaultTestUserInfoModel) Update(data *TestUserInfo) (affected int64, err error) { + affected, err = m.conn.Update(m.method("update"), data) return } - func (m *defaultTtModel) UpdateSelective(conn gobatis.GoBatis, data *Tt) (affected int64, err error) { - affected, err = conn.Update(m.method("updateSelective"), data) + func (m *defaultTestUserInfoModel) UpdateSelective(data *TestUserInfo) (affected int64, err error) { + affected, err = m.conn.Update(m.method("updateSelective"), data) return } - func (m *defaultTtModel) Delete(conn gobatis.GoBatis, id int64) (affected int64, err error) { - affected, err = conn.Delete(m.method("delete"), map[string]interface{}{ + func (m *defaultTestUserInfoModel) Delete(id int64) (affected int64, err error) { + affected, err = m.conn.Delete(m.method("delete"), map[string]interface{}{ "Id": id, }) return @@ -130,91 +158,120 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要 - - - - id,aid,c,new_column - - - - - insert into tt (id, aid, c, new_column) - values (#{id},#{aid},#{c},#{new_column}) - - - - insert into tt - - - id, - - - aid, - - - c, - - - new_column, - - - - - #{Id}, - - - #{Aid}, - - - #{C}, - - - #{NewColumn}, - - - - - - - update tt - set aid = #{Aid}, - c = #{C}, - new_column = #{NewColumn} - where id = #{Id} - - - - update tt - - - aid = #{Aid}, - - - c = #{C}, - - - new_column = #{NewColumn}, - - - where id = #{Id} - - - - - delete from tt - where id = #{Id} - - - - + + + id,nanosecond,data,content,create_time,update_time + + + insert into test_user_info (id, nanosecond, data, content, create_time, update_time) + values (#{id},#{nanosecond},#{data},#{content},#{create_time},#{update_time}) + + + insert into test_user_info + + + id, + + + nanosecond, + + + data, + + + content, + + + create_time, + + + update_time, + + + + + #{Id}, + + + #{Nanosecond}, + + + #{Data}, + + + #{Content}, + + + #{CreateTime}, + + + #{UpdateTime}, + + + + + update test_user_info + set nanosecond = #{Nanosecond}, + data = #{Data}, + content = #{Content} + where id = #{Id} + + + update test_user_info + + + nanosecond = #{Nanosecond}, + + + data = #{Data}, + + + content = #{Content}, + + + where id = #{Id} + + + delete from test_user_info + where id = #{Id} + + + + - ``` ## 用法