Hibernate基本查询(学习笔记)
2016-05-28 16:14
351 查看
Query接口
HQL基本查询
Hibernate投影查询
where条件查询
Query接口
Query接口是hibernate专门用来执行HQL语句的查询接口,使用方式:
Query query = session.createQuery("HQL语句");
Query.setParameter(…);
List resultList = query.list();
HQL基本查询
Hibernate Query Language(HQL):官方推荐的查询语言,使用类似sql的查询语言,以面向对象的方式从数据库中查询,HQL具有以下功能:
条件查询
支持投影查询(可以检索出对象的部分属性)
分页查询
连接查询
分组查询
子查询
内置了一些聚集函数
支持动态绑定参数
查询实体类的所有实例对象,方式:
Query query = session.createQuery("from Student");
List list = query.list();
或 Iterator it = query.iterate();
@Test
public void select1(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
String hql="from Student"; //Studnet是实体类名称,类似于sql里select*from 表名
Query query=session.createQuery(hql); //执行hql语句
List<Student>list=(List<Student>)query.list(); //返回所有数据,返回的是一个list集合
for(Student stu:list){ //Student实体类已经重写了toString方法,循环输出当前Student对象
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
查询实体类的单个实例对象,方式:
Query query = seeison.createQuery("from Student");
Student stu = (Student)query.setMaxResults(l).uniqueResult();
@Test
public void select2(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="from Student";
Studentstu=(Student)session.createQuery(hql).setMaxResults(1).uniqueResult();
System.out.println(stu);
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
指定别名:HQL可以指定要查询的实体类的别名
用关键子AS指定别名;也可以省略AS,直接加别名
String hql="SELECT stu.id,stu.name FROM Student AS stu";
Query query = session.createQuery(hql);
List list = query.list();
@Test
public void select1(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
String hql="select stu from Studentas stu"; //表示查询所有的列
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
函数的应用
HQL常见函数
1.字符串相关
upper(s)
、lower(s)、
concat(s1, s2)
、substring(s,offset,length)
、 length(s)、
trim([[both|leading|trailing] char[from]] s)、locate(search,
s, offset)
2.数字
abs(n)
、sqrt(n)、mod(dividend, divisor)
3.集合
size(c)
返回集合中元素的个数
4.日期时间
current_date()、current_time()、current_timestamp()
返回数据库系统的日期、时间、时间戳
year(d)、month(d)、day(d)、hour(d)、minute(d)、second(d)
从指定的参数中提取相应的值、
@Test
public void select6(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select new Student(upper(s.stuName))from Student s";
//使用投影查询,对投影查询出来的所有的姓名进行大写的转换,需要在Student类中添加构造函数来初始化姓名参数
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
Student.java
public Student(String stuName){
this.stuName=stuName;
}
distinct关键字:可以去掉结果中的重复值。
String hql = "SELECT distinct s.age FROM Student AS s";
@Test
public void select9(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select distinct s.age,s.stuName from Student s";
Queryquery=session.createQuery(hql);
List<Object[]>list=query.list();
for(Objectobjs[]:list){
System.out.println(objs[0]); //age
System.out.println(objs[1]); //stu_name
System.out.println("-------------------");
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
Order by
关键字:对结果进行排序,默认为升序(ASC)
"FROM Student AS s ORDER BY s.id DESC"
Hibernate投影查询
投影查询:即查询类的几个属性,通过用select语句只选择类的部分属性来实现的。方式:
Query query = session.createQuery("SELECT id,
name FROM Student");
List list = query.list();
@Test
public void select3(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select a.stuName,a.age from Student a";
Queryquery=session.createQuery(hql);
List<Object[]>list=query.list(); //返回的数据放在object数组中,使用不灵活
for(Object objs[]:list){ //首先循环集合得到所有的object对象
for(Object obj:objs){ //然后object数组进行迭代,得到数组中所有的元素
System.out.println(obj);
}
System.out.println("--------------------------");
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
实例化投影查询结果:在对应实体类中添加初始化这些属性的构造方法即可
Student.java
public Student(String stuName,int age){ //对stuName和age的初始化
this.stuName=stuName;
this.age=age;
}
@Test
public void select4(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select new Student(a.stuName,a.age) from Student a";
/*把投影查询age和stuName分别存放在了Studnet对象里,
* 这种方式查询出来的数据返回的就是一个对象集合
*这样操作对象就会非常容易,可以简化投影查询数据的操作 */
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
where条件查询
在where子句中可以指定
.号
比较运算符:
=、>、>=、<、<=、<>
、is null
、is notnull
范围运算符:
in (值1,
值2 …)
:
等于列表中的某一个值
not in(值1,
值2 …)
:
不等于列表中的任意一个值
between
值1 and
值2
:
在值1到值2的范围内(包括值1和值2)
not between
值1 and
值2
:
不在值1到值2的范围内
字符串模式匹配: like '字符串模式'
字符串模式中可用“%”代表任意长度的字符串,“_”代表任意单个字符。
逻辑运算: and (与)、
or (或)、not
(非)
用于集合的运算符:is empty、is
not empty
where条件语句:查询符合条件的条件。
@Test
public void select5(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
String hql="from Student a wherea.id not between 3 and 5"; //id不在3到5之间
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
参数绑定:where子句中经常需要动态设置查询参数,Hibernate提供了两种参数绑定的方式。
1.按参数名字绑定
2.按参数位置绑定(占位符的方式)
@Test
public void select7(){ //按参数位置绑定
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="from Student a where a.id=? and a.stuName=?";
//用?进行占位符的设定,可以通过占位符的顺序来个占位符分别赋值
Queryquery=session.createQuery(hql);
query.setInteger(0,3); //id的位置为3
query.setString(1, "wangwu");//stuName的位置为wangwu
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
@Test
public void select8(){ //按参数名字绑定
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
//查询操作,首先要获取一个Query接口实例对象
Stringhql="from Student a where a.id=:id and a.stuName=:name";
//:id是参数名称
Queryquery=session.createQuery(hql);
query.setInteger("id",3); //参数名为id的值为 :3
query.setString("name","wangwu");//参数名为name的值为 :wangwu
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
HQL基本查询
Hibernate投影查询
where条件查询
Query接口
Query接口是hibernate专门用来执行HQL语句的查询接口,使用方式:
Query query = session.createQuery("HQL语句");
Query.setParameter(…);
List resultList = query.list();
HQL基本查询
Hibernate Query Language(HQL):官方推荐的查询语言,使用类似sql的查询语言,以面向对象的方式从数据库中查询,HQL具有以下功能:
条件查询
支持投影查询(可以检索出对象的部分属性)
分页查询
连接查询
分组查询
子查询
内置了一些聚集函数
支持动态绑定参数
查询实体类的所有实例对象,方式:
Query query = session.createQuery("from Student");
List list = query.list();
或 Iterator it = query.iterate();
@Test
public void select1(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
String hql="from Student"; //Studnet是实体类名称,类似于sql里select*from 表名
Query query=session.createQuery(hql); //执行hql语句
List<Student>list=(List<Student>)query.list(); //返回所有数据,返回的是一个list集合
for(Student stu:list){ //Student实体类已经重写了toString方法,循环输出当前Student对象
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
查询实体类的单个实例对象,方式:
Query query = seeison.createQuery("from Student");
Student stu = (Student)query.setMaxResults(l).uniqueResult();
@Test
public void select2(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="from Student";
Studentstu=(Student)session.createQuery(hql).setMaxResults(1).uniqueResult();
System.out.println(stu);
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
指定别名:HQL可以指定要查询的实体类的别名
用关键子AS指定别名;也可以省略AS,直接加别名
String hql="SELECT stu.id,stu.name FROM Student AS stu";
Query query = session.createQuery(hql);
List list = query.list();
@Test
public void select1(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
String hql="select stu from Studentas stu"; //表示查询所有的列
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
函数的应用
HQL常见函数
1.字符串相关
upper(s)
、lower(s)、
concat(s1, s2)
、substring(s,offset,length)
、 length(s)、
trim([[both|leading|trailing] char[from]] s)、locate(search,
s, offset)
2.数字
abs(n)
、sqrt(n)、mod(dividend, divisor)
3.集合
size(c)
返回集合中元素的个数
4.日期时间
current_date()、current_time()、current_timestamp()
返回数据库系统的日期、时间、时间戳
year(d)、month(d)、day(d)、hour(d)、minute(d)、second(d)
从指定的参数中提取相应的值、
@Test
public void select6(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select new Student(upper(s.stuName))from Student s";
//使用投影查询,对投影查询出来的所有的姓名进行大写的转换,需要在Student类中添加构造函数来初始化姓名参数
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
Student.java
public Student(String stuName){
this.stuName=stuName;
}
distinct关键字:可以去掉结果中的重复值。
String hql = "SELECT distinct s.age FROM Student AS s";
@Test
public void select9(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select distinct s.age,s.stuName from Student s";
Queryquery=session.createQuery(hql);
List<Object[]>list=query.list();
for(Objectobjs[]:list){
System.out.println(objs[0]); //age
System.out.println(objs[1]); //stu_name
System.out.println("-------------------");
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
Order by
关键字:对结果进行排序,默认为升序(ASC)
"FROM Student AS s ORDER BY s.id DESC"
Hibernate投影查询
投影查询:即查询类的几个属性,通过用select语句只选择类的部分属性来实现的。方式:
Query query = session.createQuery("SELECT id,
name FROM Student");
List list = query.list();
@Test
public void select3(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select a.stuName,a.age from Student a";
Queryquery=session.createQuery(hql);
List<Object[]>list=query.list(); //返回的数据放在object数组中,使用不灵活
for(Object objs[]:list){ //首先循环集合得到所有的object对象
for(Object obj:objs){ //然后object数组进行迭代,得到数组中所有的元素
System.out.println(obj);
}
System.out.println("--------------------------");
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
实例化投影查询结果:在对应实体类中添加初始化这些属性的构造方法即可
Student.java
public Student(String stuName,int age){ //对stuName和age的初始化
this.stuName=stuName;
this.age=age;
}
@Test
public void select4(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="select new Student(a.stuName,a.age) from Student a";
/*把投影查询age和stuName分别存放在了Studnet对象里,
* 这种方式查询出来的数据返回的就是一个对象集合
*这样操作对象就会非常容易,可以简化投影查询数据的操作 */
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
where条件查询
在where子句中可以指定
.号
比较运算符:
=、>、>=、<、<=、<>
、is null
、is notnull
范围运算符:
in (值1,
值2 …)
:
等于列表中的某一个值
not in(值1,
值2 …)
:
不等于列表中的任意一个值
between
值1 and
值2
:
在值1到值2的范围内(包括值1和值2)
not between
值1 and
值2
:
不在值1到值2的范围内
字符串模式匹配: like '字符串模式'
字符串模式中可用“%”代表任意长度的字符串,“_”代表任意单个字符。
逻辑运算: and (与)、
or (或)、not
(非)
用于集合的运算符:is empty、is
not empty
where条件语句:查询符合条件的条件。
@Test
public void select5(){
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
String hql="from Student a wherea.id not between 3 and 5"; //id不在3到5之间
Queryquery=session.createQuery(hql);
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
参数绑定:where子句中经常需要动态设置查询参数,Hibernate提供了两种参数绑定的方式。
1.按参数名字绑定
2.按参数位置绑定(占位符的方式)
@Test
public void select7(){ //按参数位置绑定
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Stringhql="from Student a where a.id=? and a.stuName=?";
//用?进行占位符的设定,可以通过占位符的顺序来个占位符分别赋值
Queryquery=session.createQuery(hql);
query.setInteger(0,3); //id的位置为3
query.setString(1, "wangwu");//stuName的位置为wangwu
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
@Test
public void select8(){ //按参数名字绑定
Transactiontx=null;
Sessionsession=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
//查询操作,首先要获取一个Query接口实例对象
Stringhql="from Student a where a.id=:id and a.stuName=:name";
//:id是参数名称
Queryquery=session.createQuery(hql);
query.setInteger("id",3); //参数名为id的值为 :3
query.setString("name","wangwu");//参数名为name的值为 :wangwu
List<Student>list=(List<Student>)query.list();
for(Studentstu:list){
System.out.println(stu);
}
tx.commit();
}catch(HibernateExceptionhe){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
相关文章推荐
- 排序算法性能和使用场景总结
- 使用FFTW的fftw_plan_dft_c2r_1d()由于未归一化结果错误的解决方案
- 消息通道looper
- Android权限适配方案(API 4+)
- 面试问题
- HELLO WORLD!
- Hadoop数据读写原理
- reflect(反射)
- getter-setter方法/点语法
- 文字转换为二进制/十六进制(Uncode编码 java实现)
- 查看CentOS系统版本是32位的还是64位的
- 关于C++的类的几点注意点
- Android中更安全的使用AsyncTask
- js数据类型
- Systemd 进程管理相关
- 软件研发的安全过程之一
- 对Spring的IoC和DI最生动的解释
- Activity生命周期
- Ubuntu系统的安装
- ACM ubuntu环境配置