Nhibernate与Dapper对比,及Nhibernate增删改和9种查询语法

1,Sql语法。

NH:HQL

Dapper:原生Sql.

点评:原生Sql可以直接放在数据库里执行,Hql不行,且Hql增加学习负担。(Hn也可以原生Sql,但好像用的不多呀)

2,开发速度。

         NH:手工编写配置文件。

         Dapper:使用CodeSmith批量生成。

         点评:NH耗时,Dapper迅速。

3,自定义查询。

NH :

return _session.CreateQuery("select c.Firstname, count(c.Fi

rstname) from Customer c group by c.Firstname")

.List<object[]>();

Dapper:

String executeSql = @" SELECT ID, DevRequireId, PrjTypeId

FROM PrjOrder

                          WHERE ID = @ID ";

                var conditon = new { ID = ID };

                return conn.Query<PrjOrderModel>(executeSql, conditon).SingleOrDefault();

    点评:Nh返回结果Object,Dapper返回实体,未查询列值为空。常用于数据表字段过多时有选择查询。

4,操作多个数据库。

           NH: http://www.codeproject.com/Articles/14846/Using-NHibernate-with-Multiple-Databases

           Dapper:

            public static readonly string connPlmString = System.Configuration.ConfigurationManager.ConnectionStrings["PlmConnStr"].ToString();

        public static readonly string connSrmString = System.Configuration.ConfigurationManager.ConnectionStrings["SrmConnStr"].ToString();

 

        public static SqlConnection CratePlmOpenConnection()

        {

            var connection = new SqlConnection(connPlmString);

            connection.Open();

            return connection;

        }

 

        public static SqlConnection CrateSrmOpenConnection()

        {

            var connection = new SqlConnection(connSrmString);

            connection.Open();

            return connection;

        }

     点评:Nh配置复杂,Dapper简单易用。

5,传参:

         NH: Session.CreateQuery(strSql).SetString("BuId", BuId);

     Dapper:               

var conditon = new { ID = id, Name = name };

              return conn.Query<PrjOrderModel>(executeSql, conditon).SingleOrDefault();

         点评:NH单条设置,Dapper多条设置,且不需要指定类型,类型由变量类型自动推算。

 

Nhibernate 九种查询示例及源码如下:

        public static IList<wufei_testTb> GetAll()

        {           

            IQuery iq = session.CreateQuery("from wufei_testTb");



            return iq.List<wufei_testTb>();

        } 



        public static wufei_testTb GetById(int userId)

        {   

            return session.Get<wufei_testTb>(userId);

        }



        public static wufei_testTb GetByName1()

        {

            //返回唯一数据,如果数据有多条,程序将报错

            ISession session = NHelper.GetCurrentSession();

            var iq = session.CreateQuery("select t from wufei_testTb t where t.USER_ID=10000");



            return iq.UniqueResult<wufei_testTb>();

        }



        public static IList<Object[]> GetByName2()

        {

            //原生Sql自定义字段查询,指定返回结果,无法返回实体集            

            var iq = session.CreateSQLQuery("select t.USER_ID,t.USER_NAME from wufei_testTb t")

                .AddScalar("USER_ID", NHibernateUtil.Int32)

                .AddScalar("USER_NAME", NHibernateUtil.String);

            return iq.List<Object[]>();

        }



        public static IList GetByName3()

        {

            //原生Sql自定义字段查询,无法返回实体集            

            var iq = session.CreateSQLQuery("select t.USER_ID,t.USER_NAME from wufei_testTb t")

                .AddScalar("USER_ID", NHibernateUtil.Int32)

                .AddScalar("USER_NAME", NHibernateUtil.String);

            return iq.List();

        }



        public static IList<wufei_testTb> GetByName4()

        {

            //创建ICriteria实例,查询前50行数据

            ICriteria crit = session.CreateCriteria(typeof(wufei_testTb));

            crit.SetMaxResults(50);

            IList<wufei_testTb> testList=crit.List<wufei_testTb>();

            return testList;

        }



        public static IList<wufei_testTb> GetByName5(String userName)

        {

            //创建ICriteria实例,模糊查询,精确查询,排序

            IList<wufei_testTb> testList = session.CreateCriteria(typeof(wufei_testTb))

                .Add(Restrictions.Like("USER_NAME", "%0")) //按USER_NAME模糊查询

                //.Add(Restrictions.Eq("USER_NAME", userName)) //按USER_NAME精确查询

                .AddOrder(new NHibernate.Criterion.Order("USER_NAME", true)) //按USER_NAME排序,第二个参数True代表asc,False代表desc

                //.Add(Restrictions.Between("","",""))

                .List<wufei_testTb>();

            return testList;

        }



        public static IList<wufei_testTb> GetByName6()

        {

            //数据库里明明有结果,但却找不出数据,奇怪,求高人指点???

            wufei_testTb testSample = new wufei_testTb()

            {

                USER_ID=1

            };



           var result= session.CreateCriteria(typeof(wufei_testTb))

                    .Add(Example.Create(testSample))

                    .List<wufei_testTb>();



           return result;

        }
View Code

源码下载地址如下:

http://download.csdn.net/detail/wufei_8898/5952481

你可能感兴趣的:(Hibernate)