博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dapper 多对多查询对象和对象列表
阅读量:5156 次
发布时间:2019-06-13

本文共 3466 字,大约阅读时间需要 11 分钟。

splitOn参数:用来指定列为分隔列,之前的列为前一对象,之后的列为后一对象。 lookup 用来保存中间处理结果,可以理解为将结果归组出Group对象,并为其RightsList添加内容, 注意:lookup中添加的是临时定义的Group对象,并在循环中将此对象从lookup中取出,为其RightsList添加Rights
class Rights{    public string ID{
get;set;} public string Name{ get;set;} }
class Group{    public string ID{
get;set;} public string Name{ get;set;} public List
RightsList{ get;set;} }
CREATE TABLE `t_group_right` (  `rightId` varchar(50) NOT NULL, `groupId` varchar(50) NOT NULL, KEY `FK_group_rights_id` (`rightId`), KEY `FK_rights_group_id` (`groupId`), CONSTRAINT `FK_group_rights_id` FOREIGN KEY (`rightId`) REFERENCES `t_rights` (`id`), CONSTRAINT `FK_rights_group_id` FOREIGN KEY (`groupId`) REFERENCES `t_group` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
查询角色列表 public List
GetAll() { string sql = "SELECT a.*,c.* FROM t_group a left join t_group_right b on a.id = b.groupid left join t_rights c on c.id=b.rightid "; using (MySqlConnection conn = DapperFactory.CrateMySqlConnection()) { var lookup = new Dictionary
(); var list = conn.Query
(sql, (g, r) => { Group tmp; if (!lookup.TryGetValue(g.ID, out tmp)) { tmp = g; lookup.Add(g.ID, tmp); } tmp.RightsList.Add(r); return g; } , splitOn: "id").ToList(); return lookup.Values.ToList(); } }
Operator对象
class Operator{    public string ID{
get;set;} public string Name{ get;set;} [NoWrite] public List
GroupList { get; set; } [NoWrite] public List
RightsList { get; set; } }
查询一个Operator对象
public Operator Get(string id)        {            using (MySqlConnection conn = DapperFactory.CrateMySqlConnection())            {                string sql = "SELECT a.*,c.*,e.* FROM t_operator a left join t_operator_group b on a.id = b.operatorid left join t_group c on c.id=b.groupid " +                        "left join t_operator_rights d on a.id = d.operatorid left join t_rights e on e.id = d.rightid where a.id=@id";                Operator lookup = null;                conn.Query
(sql, (o, g, r) => { if (lookup == null) { lookup = o; } Group tmp = lookup.GroupList.Find(f => f.ID == g.ID); if (tmp == null) { tmp = g; lookup.GroupList.Add(tmp); } tmp.RightsList.Add(r); lookup.RightsList.Add(r); return o; }, param: new { id = id }, splitOn: "id"); return lookup; } }
获取Operator列表: public List
GetAll() { using (MySqlConnection conn = DapperFactory.CrateMySqlConnection()) { string sql = "SELECT a.*,c.*,e.* FROM t_operator a left join t_operator_group b on a.id = b.operatorid left join t_group c on c.id=b.groupid " + "left join t_operator_rights d on a.id = d.operatorid left join t_rights e on e.id = d.rightid"; var lookup = new Dictionary
(); conn.Query
(sql, (o, g, r) => { Operator tmp; if (!lookup.TryGetValue(o.ID,out tmp)) { tmp = o; lookup.Add(o.ID,tmp); } Group tmpG = tmp.GroupList.Find(f => f.ID == g.ID); if (tmpG == null) { tmpG = g; tmp.GroupList.Add(tmpG); } //角色权限列表 Rights tmpR = tmpG.RightsList.Find(f => f.ID == r.ID); if (tmpR == null) { tmpG.RightsList.Add(r); } //用户权限列表 tmpR = tmp.RightsList.Find(f => f.ID == r.ID); if (tmpR == null) { tmp.RightsList.Add(r); } return o; }, splitOn: "id"); return lookup.Values.ToList(); } }

转载于:https://www.cnblogs.com/yyzyou/p/8284118.html

你可能感兴趣的文章
ES5_03_Object扩展
查看>>
Apache-ab 接口性能测试
查看>>
EF 4.1 Code First Walkthrough
查看>>
常用MySQL语法
查看>>
007API网关服务Zuul
查看>>
bzoj 2600: [Ioi2011]ricehub
查看>>
iOS __strong __weak @Strongify @Weakify
查看>>
thinkphp引入PHPExcel类---thinkPHP类库扩展-----引入没有采用命名空间的类库
查看>>
创建数据库,表
查看>>
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)...
查看>>
Luogu 1970 NOIP2013 花匠 (贪心)
查看>>
javascript笔记---貌似大叔
查看>>
生产环境 direct path read 与log file sync等待事件问题处理
查看>>
百度地图的那些坑
查看>>
EJB的beans们
查看>>
EL表达式
查看>>
将指定内容写入目标文件(日志)
查看>>
十分钟释疑Oracle中“小表超慢”之谜(SQL调优/SQL优化)
查看>>
子集和排列
查看>>
C# ?? 操作符示例
查看>>