码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate学习笔记5

时间:2015-09-18 00:45:32      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

hql语句的查询(hibernate query language)

hql和sql语句的区别
sql:语言关系型数据库里面的通用查询
,结构化查询语言,查看的是表以及表的列
hql是hibernate中独有的查询语言,其是面向
对象的查询语句,查询的是类以及类中的属性
注意:hql语句不能执行insert语句,且最终
编写的hql语句会转化为相应的sql语句
,以及hibernate的get/load方法只能通过主键的方式
进行查询
from User u where u.name=‘zhangsan‘ and u.age>20
update User u set u.name=‘briup‘ where u.id=10;
delete from User u where u.name=‘briup‘
session.delete

 

Criteria
作用:指定查询某一个类所对应表里面的数据
对象的获得:session可以创建
Criterion
作用:表示查询时用到的条件,每个条件都是一个
Criterion类型的对象 eg:id>5、salary=1000
对象的获取:使用Restrictions创建
Restrictions
作用:专门创建表示查询条件的对象,
也就是Criterion类型的对象
使用的是这个类里面的静态方法来创建表示
条件的对象

高级查询方式
Criteria(接口)
1.概念
Criterion(接口)是criteria的查询条件。
criteria提供了
add(Criterion criterion)方法来添加查询条件

2、创建
Criterion的实例可以通过Restrictions工具类来
创建,Restrictions提供了大量的静态的方法,如
eq(等于)、ge(大于等于)、between 等方法的创建
Criterion查询条件

Restrictions.eq =
Restrictions.gt >
Restrictions.ge >=
Restrictions.lt <
Restrictions.le <=
Restrictions.between BETWEEN
Restrictions.like like
Restrictions.in in
Restrictions.and and
Restrictions.or or
Restrictions.isNull 判断是否为空,为空则返回true
Restrictions.isNotNull 与isNull相反

测试实例:

实体类一:

package com.briup.criteria.bean;
public class Husband {
private long id;
private String name;
private int age;
private double salary;
private Wife wife;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}

实体类二:

package com.briup.criteria.bean;
public class Wife {
private long id;
private String name;
private Husband husband;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}

配置文件一:

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.briup.criteria.bean.Husband" table="s_husband">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="age"/>
<property name="salary"/>
<many-to-one name="wife" class="com.briup.criteria.bean.Wife" column="wife_id" unique="true" cascade="all" ></many-to-one>
</class>
</hibernate-mapping>

配置文件二:

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.briup.criteria.bean.Wife" table="s_wife">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<one-to-one name="husband" class="com.briup.criteria.bean.Husband" cascade="all" fetch="select"></one-to-one>
</class>
</hibernate-mapping>

测试类:

package com.briup.criteria.test;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.briup.criteria.bean.Husband;
import com.briup.criteria.bean.Wife;
/**
* 高级查询方式Criteria(接口)测试实例
* 1.普通查询;
* 2.添加条件查询[where\and\or\order by\between and\ > < = null\not null\in\like
* 3.多对象查询和排序
* 4.hql语句分页
* @author Victor
*
*/
public class Criteria_Test {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
private Configuration configuration=new Configuration();
@Before
public void before(){
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
session=sessionFactory.openSession();
transaction=session.beginTransaction();
}
@After
public void after(){
transaction.commit();
session.close();
}
@Test
public void AutoCreateTable(){
SchemaExport schemaExport=new SchemaExport(configuration);
schemaExport.create(true, true);
}
@Test
public void save(){
for(int i=0;i<=8;i++){
Husband hus=new Husband();
hus.setName("briup"+i);
hus.setAge(20+i);
hus.setSalary(2000+(i*300));
Wife wife=new Wife();
wife.setName("lili"+i);
hus.setWife(wife);
wife.setHusband(hus);
session.save(wife);
}
}
@Test
public void singleSave(){
Husband husband=new Husband();
husband.setName("奎因");
husband.setAge(35);
husband.setSalary(3500);
Wife wife=new Wife();
wife.setName("蛮子");
husband.setWife(wife);
wife.setHusband(husband);
//session.save(husband);
session.save(wife);
}
@Test//查询所有的数据
public void criteriaSelectHus(){
//session的作用:
//1执行增删改查操作
//2提供缓存功能
//3创建高级查询接口的实现类
//Criteria是接口eg:select * from s_husband
//Criteria criteria=session.createCriteria(Husband.class);
//这时候就可以查询出所有的结果,在这里没有加任何限定条件
/*List<Husband> list=criteria.list();
for(Husband h:list){
//测试是否是懒加载
//System.out.println(h.getWife().getName());
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary());
}*/
}
@Test
public void criteraSelectWif(){
//Criteria criteria=session.createCriteria(Wife.class);
/*List<Wife> list=criteria.list();
for(Wife w:list){
System.out.println(w.getId()+";"+w.getName());
}*/
}
@Test//添加查询条件
public void criteriaSelect(){
Criteria criteria=session.createCriteria(Husband.class);
/***
* criteria接口 Criterion接口Restrictions类
* Criterion是criteria的查询条件-------where
* Criterion也是一个接口,我们要使用这个接口的实现类对象来表示查询条件
* Criteria中的add(查询条件)
* Criteria提供了add(Criterion criterion)方法来添加查询条件
* Restrictions工具类可以创建Criterion接口类型的对象,也就是创建查询条件对象
* Restrictions类中有很多的静态方法,这些方法的返回值就是我们要查询条件对象
*
* Restrictions.gt("id",2L)方法的返回值是Criterion接口的实现类对象
* select * from s_husband where id>2;
* criteria.add(Restrictions.gt("id",2L));
*
*
* select * from s_husband where salary<=4000;
* criteria.add(Restrictions.le("salary",4000d));
*
* select * from s_husband where name=‘briup1‘;
* criteria.add(Restrictions.eq("name","briup1"));
*
* select * from s_husband where name like ‘briu%‘;
* criteria.add(Restrictions.like("name","briu%"));
*
* select * from s_husband where id in(1,2,3,4);
* criteria.add(Restrictions.in("id",new Long[]{1L,2L,3L,4L}));
*
* select * from s_husband where salary between 4000 and 6000;
* criteria.add(Restrictions.between("salary",4000d,6000d));
* 注意:这里同样可以出现两个日期之间的值
*
* select * from s_husband where salary is null;
* criteria.add(Restrictions.isNull("salary"));
*
*
*/
//吧类和类对应的表字段让其不一样,测试用的是表里的字段还是类里的字段
// criteria.add(Restrictions.gt("id",2L))
// .add(Restrictions.le("salary",4000d));
criteria.add(Restrictions.isNotNull("salary"));
//这时候就可以查询出所有的结果,在这里没有加任何限定条件
/*List<Husband> list=criteria.list();
for(Husband h:list){
//测试是否是懒加载
//System.out.println(h.getWife().getName());
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary());
}*/
}
@Test
public void select(){
Criteria criteria=session.createCriteria(Husband.class);
//.add(Restrictions.le("salary", 3800D))
//.add(Restrictions.gt("id",2L));;
//.add(Restrictions.le("salary",4000d));
//.add(Restrictions.eq("name","周志伟"));
//criteria.add(Restrictions.like("name","briu%"));
//criteria.add(Restrictions.isNotNull("salary"));
criteria.add(Restrictions.in("id",new Long[]{1L,2L,3L}));
//criteria.add(Restrictions.between("salary",4000d,6000d));
/*List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId()+";"+h.getName()+";"+h.getAge()+";"+h.getSalary());
}*/
}
@Test//连着添加查询语句
public void criteia3(){
Criteria criteria=session.createCriteria(Husband.class);
//连着添加条件,默认是以and方式添加链接条件
//select * from s_husband where id>3 and id<7;
criteria.add(Restrictions.gt("id",2L));
criteria.add(Restrictions.lt("id",7L));
//上面的写法等价于下面的写法
//下面的写法可以给我们带来好处,链式编程,eg:String中的replace方法。我们就可以一直替换
//criteria.add(Restrictions.gt("id", 2L))
// .add(Restrictions.lt("id", 7L));
/*List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId());
}*/
}
@Test//添加or的条件
public void cretica4(){
Criteria criteria=session.createCriteria(Husband.class);
//Restrictions.or(条件1,条件2);方法的返回值还是Criterion类型对象
//select * from s_husband where (id>2 or name=‘briup1‘)
//criteria.add(Restrictions.or(Restrictions.gt("id", 2L),Restrictions.eq("name", "briup1")));
//Restrictions.disjunction()后面可以跟多个or
//关联条件
//select * from s_husband where id>3 and id id<7 and ....or...or..or
criteria.add(Restrictions.disjunction()
.add(Restrictions.gt("salary", 2000))
.add(Restrictions.eq("name", "briup2"))
.add(Restrictions.eq("id", 4L))
).add(Restrictions.gt("age", 22));
// criteria.add(Restrictions.gt("id",3L))
// .add(Restrictions.lt("id", 7L))
// .add(Restrictions.disjunction()
// .add(Restrictions.eq("name", "briup1"))
// .add(Restrictions.like("name","bri%"))
// .add(Restrictions.lt("salary",5000D)));
/*List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId());
}*/
}
@Test//连接查询
public void creteria5(){
//fetch="join"
//Criteria criteria=session.createCriteria(Husband.class);
//在查询husband的基础上连接查询把wife也查出来
//这里的wife指的是husband中名字叫wife的属性
//criteria.createCriteria("wife");
/*List<Husband> list=criteria.list();
for(Husband h:list){
//System.out.println(h);
//测试是否是懒加载
//System.out.println(h.getWife().getName());
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary()+"--"+h.getWife().getId()+";"+h.getWife().getName());
}*/
}
@Test//排序
public void creteria6(){
//select * from s_husband
//Criteria criteria=session.createCriteria(Husband.class);
//where name="briup1";
//criteria.add(Restrictions.eq("name", "briup1"));
//order by id asc
//criteria.addOrder(Order.desc("id"));
/*criteria.addOrder(Order.desc("salary"));
List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId());
}*/
}
//hql分页操作
@Test
public void divitePage(){
String hql="from Husband";
Query query=session.createQuery(hql);
//从什么位置开始,默认的是0
int a=9;
//限定返回的条数
int b=3;
query.setFirstResult(a);
query.setMaxResults(b);
/*List<Husband> list=query.list();
for(Husband h:list){
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary());
}*/
}
}

Hibernate学习笔记5

标签:

原文地址:http://www.cnblogs.com/victor963/p/4818002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!