迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:shorm开源软件地址:https://gitee.com/jongsz/shorm开源软件介绍:shormshorm is a orm tool wriitted in golang, which supports database sharding and master-slave mode Installationgo get github.com/shengzhi/shorm Features
Drivers support
Change logQuick start
cluster := &Cluster{TotalGroups: 5} cluster.Groups = append(cluster.Groups, &DbGroup{ IsDefault: true, RangeFrom: 0, RangeTo: 3, Name: "Group1", Nodes: []*DbNode{ { Name: "G1_Master", ConnStr: connstr, Type: NodeType_Master, }, { Name: "G1_Node1", ConnStr: connstr, Type: NodeType_Slave, }, }, }, ) cluster.Groups = append(cluster.Groups, &DbGroup{ RangeFrom: 3, RangeTo: 5, Name: "Group2", Nodes: []*DbNode{ { Name: "G2_Master", ConnStr: connstr2, Type: NodeType_Master, }, { Name: "G2_Node1", ConnStr: connstr2, Type: NodeType_Slave, }, }, }) engine := shorm.NewEngine("mssql", &cluster)
engine, err := shorm.NewEngineFromConfig("cluster_config.json")
Create table T_User( UserId bigint not null, UserName varchar(50) not null default '', Age int not null default 0, CreatedTime datetime not null default getdate(), ) Alter table T_User create cluster index PK_User(UserId asc) type User struct{ TabName shorm.TableName `shorm:"T_User"` Id int64 `shorm:"UserId,pk,shard"` UserName string Age int32 CreatedTime time.Time } func (u User) GetShardValue() int64{ return u.Id }
s := engine.StartSession() defer engine.EndSession(s) u := User{} s.ShardValue(1).Id(1).Get(&u) it will generate sql as below: select top 1 * from T_User where UserId=1 and will locate the database "G1_node1"
![]() |
请发表评论