NHibernate 有好几种数据库查询方式
- NHibernate 有好几种数据库查询方式
- 1、原生SQL
- var employeeQuery = Database.Session
- .CreateSQLQuery("select * from Employee where FirstName = 'John'")
- .AddEntity(typeof(Employee));
- var employees = employeeQuery.List<Employee>();
- 2、Hibernate Query Language(HQL)HN自己的一条语言,简称HQL
- var employeeQuery = Database.Session
- .CreateQuery("select e from Employee as e");
- var employees = Database.Session
- .CreateQuery("select e from Employee as e ")
- .List<Employee>();
- //select e from Employee as e where e.Firstname = 'John'
- var firstName = "John";
- var employees = Database.Session
- .CreateQuery("select e from Employee as e where e.Firstname = '"
- + firstName
- + "'")
- .List<Employee>();
- var employeeQuery = Database.Session
- .CreateQuery("select e from Employee as e where e.Firstname = :firstName");
- employeeQuery.SetParameter("firstName", "John");
- var employees = employeeQuery.List<Employee>();
- 3、Criteria API 标准API
- using (var transaction = database.Session.BeginTransaction())
- {
- var employeeQuery = database.Session.CreateCriteria<Employee>();
- employeeQuery.Add(Restrictions.Eq("Firstname", "john"));
- var employees = employeeQuery.List<Employee>();
- transaction.Commit();
- }
- using (var transaction = database.Session.BeginTransaction())
- {
- var employees = database.Session.CreateCriteria<Employee>()
- .Add(Restrictions.Eq("Firstname", "john"))
- .List<Employee>();
- transaction.Commit();
- }
- using (var transaction = database.Session.BeginTransaction())
- {
- var employees = database.Session.CreateCriteria<Employee>()
- .Add(Restrictions.Between("DateOfJoining", DateTime.Now.AddYears(-1), DateTime.Now))
- .List<Employee>();
- transaction.Commit();
- }
- 4、The QueryOver API
- var employees = Database.Session.QueryOver<Employee>()
- .Where(x => x.Firstname == "John")
- .List<Employee>();
- var employees = Database.Session
- .QueryOver<Employee>()
- .Where(x => x.DateOfJoining > DateTime.Now.AddYears(-1) &&
- x.DateOfJoining < DateTime.Now)
- .List<Employee>();
- 5、LINQ
- var employees = from e in Database.Session.Query<Employee>()
- where e.Firstname == "John" select e;
- var employees = Database.Session.Query<Employee>()
- .Where(e => e.Firstname == "John");
- var employees = from e in Database.Session.Query<Employee>()
- where e.DateOfJoining > DateTime.Now.AddYears(-1) &&
- e.DateOfJoining < DateTime.Now
- select e;
- var employees = Database.Session.Query<Employee>()
- .Where(e => e.DateOfJoining > DateTime.Now.AddYears(-1) &&
- e.DateOfJoining < DateTime.Now);
NHibernate 有好几种数据库查询方式的更多相关文章
- MySql、SqlServer、Oracle 三种数据库查询分页方式
SQL Server关于分页 SQL 的资料许多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是颠末预编译的,执行效率高,也更灵活 ...
- mybatis两种嵌套查询方式
1,推荐用第一种 <select id="getTeacher2" resultMap="TeacherStudent"> select s.id ...
- drill 数据库查询方式简单说明
1. mysql select * from mysql-storage.mysqldb.mysqltable 2. oracle select * from oracle-storag ...
- iBatis.Net(C#)数据库查询
引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html 摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...
- IBatis.Net学习笔记五--常用的查询方式
在项目开发过程中,查询占了很大的一个比重,一个框架的好坏也很多程度上取决于查询的灵活性和效率.在IBatis.Net中提供了方便的数据库查询方式. 在Dao代码部分主要有两种方式:1.查询结果为一个对 ...
- 转载 50种方法优化SQL Server数据库查询
原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...
- Oracle 数据库(oracle Database)Select 多表关联查询方式
Oracle数据库中Select语句语法及介绍 SELECT [ ALL | DISTINCT ] <字段表达式1[,<字段表达式2[,…] FROM <表名1>,<表名 ...
- Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加
Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...
- Hibernate的Api以及三种查询方式
Hibernate Api |-- Configuration 配置管理类对象 config.configure(); 加载主配置文件的方法(hibernate.cfg.xml) ...
随机推荐
- 如何更改linux文件的拥有者及用户组(chown和chgrp)
http://blog.csdn.net/hudashi/article/details/7797393 一.基本知识 在Linux中,创建一个文件时,该文件的拥有者都是创建该文件的用户.该文件用 ...
- Hadoop/Spark相关面试问题总结
面试回来之后把其中比较重要的问题记了下来写了个总结: (答案在后面) 1.简答说一下hadoop的map-reduce编程模型 2.hadoop的TextInputFormat作用是什么,如何自定义实 ...
- noah
1.url:controller/method 2.在index.php中设置display_errors:1 能看到错误提示
- GeneralizedLinearAlgorithm in Spark MLLib
GeneralizedLinearAlgorithm SparkMllib涉及到的算法 Classification Linear Support Vector Machines (SVMs) Log ...
- asp.net core mvc 中间件之WebpackDevMiddleware
asp.net core mvc 中间件之WebpackDevMiddleware WebpackDevMiddleware中间件主要用于开发SPA应用,启用Webpack,增强网页开发体验.好吧,你 ...
- EF查询某个时间段内的数据遇到坑!
第一个问题 var res = pwDb.Set<WorkInfo>().Where(t => t.WorkTime > startTime && t.Work ...
- [译] 玩转ptrace (一)
[本文翻译自这里: http://www.linuxjournal.com/article/6100?page=0,0,作者:Pradeep Padaia] 你是否曾经想过怎样才能拦截系统调用?你是否 ...
- hashMap tableSizeFor 实现原理
基于jdk1.8 hashMap实现,要求容量大小是2的整次方,例如:2/4/8/16/32/64/128...,而不能是中间的某个值.这是为什么呢? map是数组+链表的数据结构,读写数据都需要首先 ...
- js 的this
js的this应该是不好掌握又必须要掌握的东西 主要参考: http://www.cnblogs.com/pssp/p/5216085.html http://www.cnblogs.com/fron ...
- WebDriver高级应用实例(3)
3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...