欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Dapper扩展Dapper.Common框架 Linq To Sql 底层源码.net ORM框架

程序员文章站 2022-11-13 20:55:11
源代码:https://github.com/1448376744/Dapper.CommonNUGET: Dapper.CommonQQ群:642555086 一、基本结构,此处可用委托,或动态代理完成 class Program { //类加载时配置一次 static Program() { /... ......
源代码:https://github.com/1448376744/dapper.common
nuget: dapper.common
qq群:642555086
一、基本结构,此处可用委托,或动态代理完成
class program
{
    //类加载时配置一次
    static program()
    {
        //配置数据源为mysql
        sessionfactory.datasource = ()=>new mysqlconnection("server=127.0.0.1;user id=root;password=1024;database=test;");
        //下划线不敏感,默认不区分大小写
        sessionfactory.matchnameswithunderscores = true;
        //session使用静态代理,记录会话日志,生产模式设置false
        sessionfactory.sessionproxy = true;

    }
    static void main(string[] args)
    {
        //变量声明
        isession session = null;
        try
        {
            //开启一次数据会话
            session = sessionfactory.getsession();
            //开启事物,取消自动提交
            session.open(false);
            //事物操作1
            //事物操作2
            //事物操作3
            //.......
            //提交事物
            session.commit();
        }
        catch (exception e)
        {
            //异常回滚
            if (session!=null)
            {
                session.rollback();
            }
            throw e;
        }
        finally
        {
            //资源释放
            if (session!=null)
            {
                session.close();
            }
        }
    }
}

二、映射,此处可用自动模板完成
 /// <summary>
/// 对应表student,同名可以省略,字段名不分大小写,不分下划线
/// </summary>
[table("student")]
public class student
{
    /// <summary>
    /// 对应字段id,主键标识列identity=true
    /// </summary>
    [column("id", true)]
    public int? id { get; set; }
    /// <summary>
    /// 对应字段me_name,通过column校正为name,
    /// </summary>
    [column("me_name", false)]
    public string mename { get; set; }
    /// <summary>
    /// 对应数据字段age无需校正
    /// </summary>
    public int? age { get; set; }
    /// <summary>
    /// 对应字段create_time,不分大小写,下划线不敏感
    /// </summary>
    [column("create_time", false)]
    public datetime? createtime { get; set; }
}
三、常用api
     var sesion = sessionfactory.getsession();
    /*****************insert*******************/
    //dapper
    var row1 = sesion.execute("insert into student(age,me_name)values(@age,@mename)", new { age = 20, mename = "dapper" });
    //扩展
    var row2 = sesion.from<student>().insert(new student()
    {
        name = "dapper.common",
        age = 50,
        createtime = datetime.now
    });
    var identity = sesion.from<student>().insertbyid(new student()
    {
        age = 20,
        name = "identity"
    });
    //list
    var list = new list<student>();
    list.add(new student()
    {
        name = "dapper.common",
        age = 50,
        createtime = datetime.now
    });
    var row3 = sesion.from<student>().insert(list);

    /*****************update*******************/
    //根据主键修改全部列
    var row4 = sesion.from<student>().update(new student()
    {
        id = 27,
        name = "update"
    });
    //list
    var list2 = new list<student>();
    list2.add(new student()
    {
        name = "update list",
        id = 27
    });
    list2.add(new student()
    {
        name = "update list",
        id = 28
    });
    var row5 = sesion.from<student>().update(list2);
    //修改部分列+条件更新
    var entity = new
    {
        age = 20,
        name = "admin"
    };
    var row6 = sesion.from<student>()
        //如果第一个条件为true,则更新mename为entity.name
        .set(!string.isnullorempty(entity.name), a => a.name, entity.name)
        //age在原来的基础上加20
        .set(a => a.age.eq(a.age + entity.age))
        //条件id=30
        .where(a => a.id == 30)
        //要执行的操作
        .update();
    /*****************delete*******************/
    //更据实体id删除
    var row7 = sesion.from<student>().delete(new student() { id = 30 });
    //条件删除
    var row8 = sesion.from<student>()
        .where(a => a.age > 20)
        .delete();
    /*****************select*******************/
    //查询单个
    var student = sesion.from<student>().single();
    var list1 = sesion.from<student>().select();
    //复杂查询
    list = sesion.from<student>()
        //查询条件
        .where(a => a.age > 20 && a.id.in(new int[] { 1, 2, 3 }.tolist()))
        //排序
        .desc(a => a.id)
        //悲观锁
        .xlock()
        //分页
        .limit(1, 10)
        //部分列
        .select(s => new { s.name });
    //分页查询,返回总记录数
    var total = 10;
    list = sesion.from<student>()
        .skip(1, 5, out total)
        .select();
    /*****************动态查询*******************/
    var query = new wherequery<student>();
    query
        .and(a => a.name.like("%aa%"))
        .or(a => a.id > 0)
        .or(a => a.id < 10)
        .or(a => a.id.in(new[] { 1, 2, 3 }))
        .and(a => 1 > 2 ? a.name.like("cc%") : a.id > 100);
    var res = sesion.from<student>().where(query).exists();
    /*****************会话日志*******************/
    var aa = sesion.logger();