dapper关联关系查询小测试

测试实体类(表结构)

public class User
{
    public int user_id { get; set; }
    public string user_name { get; set; }
}

public class Book
{
    public int book_id { get; set; }
    public string book_name { get; set; }
    public int user_id { get; set; }
}

//一对多关系
public class UserBooks : User
{
    public List<Book> Books;
    public UserBooks()
    {
        Books = new List<Book>();
    }
}

//一对一关系
public class UserBook : User
{
    public Book MyBook;
}

测试程序

    public static void TestOneToMany()
    {
        using (var con = new MySqlConnection("Server=182.180.57.3;Database=test;port=3306;user id=root;password=123;"))
        {
            Console.WriteLine("==========下面是一对多的Demo===========");
            var userBooks = con.Query<UserBooks, Book, UserBooks>(@"SELECT a.user_name,a.user_id,b.user_id,b.book_id, b.book_name FROM tab_user a INNER JOIN tab_book b ON a.user_id=b.user_id"
                , (user, book) =>
                     {
                         user.Books.Add(book);
                         return user;
                     }, null, null, true, "user_id");
            foreach (UserBooks ub in userBooks)
            {
                Console.Write(ub.user_id + "\t" + ub.user_name + "\t");
                foreach (Book book in ub.Books)
                {
                    Console.Write(book.book_id + "      " + book.book_name + "\n");
                }
            }
            Console.WriteLine("==========下面是一对一的Demo===========");
            var userBook = con.Query<UserBook, Book, UserBook>(@"SELECT a.user_name,a.user_id,b.user_id,b.book_id, b.book_name FROM tab_user a INNER JOIN tab_book b ON a.user_id=b.user_id where a.user_id=3"
                , (user, book) =>
                {
                    user.MyBook = book;
                    return user;
                }, null, null, true, "user_id").First<UserBook>();
            Console.WriteLine(userBook.user_id + "\t" + userBook.user_name + "\t" + userBook.MyBook.book_id + "\t" + userBook.MyBook.book_name);
            Console.Read();
        }
    }

 

转载于:https://www.cnblogs.com/wujf/p/5213207.html

博文地址: https://www.cnblogs.com/cl-blogs/p/10219126.html 简单栗子: [Test] public void 三分页测试() { LockPers lpmodel = new LockPers() { Name = "%蛋蛋%", IsDel = false}; Users umodel = new Users() { UserName = "jiaojiao" }; SynNote snmodel = new SynNote() { Name = "%木头%" }; Expression<Func<LockPers, Users, SynNote, bool>> where = PredicateBuilder.WhereStart<LockPers, Users, SynNote>(); where = where.And((lpw, uw, sn) => lpw.Name.Contains(lpmodel.Name)); where = where.And((lpw, uw, sn) => lpw.IsDel == lpmodel.IsDel); where = where.And((lpw, uw, sn) => uw.UserName == umodel.UserName); where = where.And((lpw, uw, sn) => sn.Name.Contains(snmodel.Name)); DapperSqlMaker<LockPers, Users, SynNote> query = LockDapperUtilsqlite<LockPers, Users, SynNote> .Selec() .Column((lp, u, s) => // null) //查询所有字段 new { lp.Id, lp.InsertTime, lp.EditCount, lp.IsDel, u.UserName, s.Content, s.Name }) .FromJoin(JoinType.Left, (lpp, uu, snn) => uu.Id == lpp.UserId , JoinType.Inner, (lpp, uu, snn) => uu.Id == snn.UserId) .Where(where) .Order((lp, w, sn) => new { lp.EditCount, lp.Name, sn.Content }); var result = query.ExcuteSelect(); //1. 执行查询 WriteJson(result); // 打印查询结果 Tuple<StringBuilder, DynamicParameters> resultsqlparams = query.RawSqlParams(); WriteSqlParams(resultsqlparams); // 打印生成sql和参数 int page = 2, rows = 3, records; var result2 = query.LoadPagelt(page, rows, out records); //2. 分页查询 WriteJson(result2); // 查询结果 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值