1. NHibernate 有好几种数据库查询方式
  2. 1、原生SQL
  3. var employeeQuery = Database.Session
  4. .CreateSQLQuery("select * from Employee where FirstName = 'John'")
  5. .AddEntity(typeof(Employee));
  6. var employees = employeeQuery.List<Employee>();
  7.  
  8. 2Hibernate Query Language(HQL)HN自己的一条语言,简称HQL
  9.  
  10. var employeeQuery = Database.Session
  11. .CreateQuery("select e from Employee as e");
  12.  
  13. var employees = Database.Session
  14. .CreateQuery("select e from Employee as e ")
  15. .List<Employee>();
  16. //select e from Employee as e where e.Firstname = 'John'
  17. var firstName = "John";
  18. var employees = Database.Session
  19. .CreateQuery("select e from Employee as e where e.Firstname = '"
  20. + firstName
  21. + "'")
  22. .List<Employee>();
  23.  
  24. var employeeQuery = Database.Session
  25. .CreateQuery("select e from Employee as e where e.Firstname = :firstName");
  26. employeeQuery.SetParameter("firstName", "John");
  27. var employees = employeeQuery.List<Employee>();
  28.  
  29. 3Criteria API 标准API
  30.  
  31. using (var transaction = database.Session.BeginTransaction())
  32. {
  33. var employeeQuery = database.Session.CreateCriteria<Employee>();
  34. employeeQuery.Add(Restrictions.Eq("Firstname", "john"));
  35. var employees = employeeQuery.List<Employee>();
  36. transaction.Commit();
  37. }
  38.  
  39. using (var transaction = database.Session.BeginTransaction())
  40. {
  41. var employees = database.Session.CreateCriteria<Employee>()
  42. .Add(Restrictions.Eq("Firstname", "john"))
  43. .List<Employee>();
  44. transaction.Commit();
  45. }
  46.  
  47. using (var transaction = database.Session.BeginTransaction())
  48. {
  49. var employees = database.Session.CreateCriteria<Employee>()
  50. .Add(Restrictions.Between("DateOfJoining", DateTime.Now.AddYears(-1), DateTime.Now))
  51. .List<Employee>();
  52. transaction.Commit();
  53. }
  54.  
  55. 4The QueryOver API
  56. var employees = Database.Session.QueryOver<Employee>()
  57. .Where(x => x.Firstname == "John")
  58. .List<Employee>();
  59.  
  60. var employees = Database.Session
  61. .QueryOver<Employee>()
  62. .Where(x => x.DateOfJoining > DateTime.Now.AddYears(-1) &&
  63. x.DateOfJoining < DateTime.Now)
  64. .List<Employee>();
  65.  
  66. 5LINQ
  67. var employees = from e in Database.Session.Query<Employee>()
  68. where e.Firstname == "John" select e;
  69. var employees = Database.Session.Query<Employee>()
  70. .Where(e => e.Firstname == "John");
  71.  
  72. var employees = from e in Database.Session.Query<Employee>()
  73. where e.DateOfJoining > DateTime.Now.AddYears(-1) &&
  74. e.DateOfJoining < DateTime.Now
  75. select e;
  76. var employees = Database.Session.Query<Employee>()
  77. .Where(e => e.DateOfJoining > DateTime.Now.AddYears(-1) &&
  78. e.DateOfJoining < DateTime.Now);

NHibernate 有好几种数据库查询方式的更多相关文章

  1. MySql、SqlServer、Oracle 三种数据库查询分页方式

    SQL Server关于分页 SQL 的资料许多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是颠末预编译的,执行效率高,也更灵活 ...

  2. mybatis两种嵌套查询方式

    1,推荐用第一种 <select id="getTeacher2" resultMap="TeacherStudent"> select s.id ...

  3. drill 数据库查询方式简单说明

    1. mysql   select * from mysql-storage.mysqldb.mysqltable   2. oracle    select * from oracle-storag ...

  4. iBatis.Net(C#)数据库查询

    引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html  摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...

  5. IBatis.Net学习笔记五--常用的查询方式

    在项目开发过程中,查询占了很大的一个比重,一个框架的好坏也很多程度上取决于查询的灵活性和效率.在IBatis.Net中提供了方便的数据库查询方式. 在Dao代码部分主要有两种方式:1.查询结果为一个对 ...

  6. 转载 50种方法优化SQL Server数据库查询

    原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...

  7. Oracle 数据库(oracle Database)Select 多表关联查询方式

    Oracle数据库中Select语句语法及介绍 SELECT [ ALL | DISTINCT ] <字段表达式1[,<字段表达式2[,…] FROM <表名1>,<表名 ...

  8. Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加

    Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...

  9. Hibernate的Api以及三种查询方式

    Hibernate  Api |-- Configuration       配置管理类对象 config.configure();    加载主配置文件的方法(hibernate.cfg.xml) ...

随机推荐

  1. 如何更改linux文件的拥有者及用户组(chown和chgrp)

    http://blog.csdn.net/hudashi/article/details/7797393 一.基本知识   在Linux中,创建一个文件时,该文件的拥有者都是创建该文件的用户.该文件用 ...

  2. Hadoop/Spark相关面试问题总结

    面试回来之后把其中比较重要的问题记了下来写了个总结: (答案在后面) 1.简答说一下hadoop的map-reduce编程模型 2.hadoop的TextInputFormat作用是什么,如何自定义实 ...

  3. noah

    1.url:controller/method 2.在index.php中设置display_errors:1 能看到错误提示

  4. GeneralizedLinearAlgorithm in Spark MLLib

    GeneralizedLinearAlgorithm SparkMllib涉及到的算法 Classification Linear Support Vector Machines (SVMs) Log ...

  5. asp.net core mvc 中间件之WebpackDevMiddleware

    asp.net core mvc 中间件之WebpackDevMiddleware WebpackDevMiddleware中间件主要用于开发SPA应用,启用Webpack,增强网页开发体验.好吧,你 ...

  6. EF查询某个时间段内的数据遇到坑!

    第一个问题 var res = pwDb.Set<WorkInfo>().Where(t => t.WorkTime > startTime && t.Work ...

  7. [译] 玩转ptrace (一)

    [本文翻译自这里: http://www.linuxjournal.com/article/6100?page=0,0,作者:Pradeep Padaia] 你是否曾经想过怎样才能拦截系统调用?你是否 ...

  8. hashMap tableSizeFor 实现原理

    基于jdk1.8 hashMap实现,要求容量大小是2的整次方,例如:2/4/8/16/32/64/128...,而不能是中间的某个值.这是为什么呢? map是数组+链表的数据结构,读写数据都需要首先 ...

  9. js 的this

    js的this应该是不好掌握又必须要掌握的东西 主要参考: http://www.cnblogs.com/pssp/p/5216085.html http://www.cnblogs.com/fron ...

  10. WebDriver高级应用实例(3)

    3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...