二。Hibernate 查询 HQL、SQL方式

hibernate的查询
1.HQL方式:所有查询都是根据java对象名来完成,对象名替换表名
2.SQL方式:保留原来的sql查询风格
3.可以通过设置第一条和最大条数来实现各种数据库的分页查询
4.通过BaseDAO来实现dao的代码优化

 

实例1:hql、sql查询

  Test.java

package com.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.pojo.Student;

public class Test {

    public static void main(String[] args) {
        // 配置对象,用来获取核心配置文件的
        // 如果核心配置文件名是:hibernate.cfg.xml,并且该文件在src根目录下,configure()可以不带参数
        Configuration configuration = new Configuration().configure();
        ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                .buildServiceRegistry();
        SessionFactory sf = configuration.buildSessionFactory(sr);
        Session session = sf.openSession();
        
        //1.hql:是hibernate的专用查询语句,是针对对象的查询方法,其中没有表名,全都是对象名
        String hql = "from Student";
//        String hql = "select count(*) from Student a";
        Query query = session.createQuery(hql);
        query.setFirstResult(5);
        query.setMaxResults(5);
        List<Student> list = query.list();
//        System.out.println(list.get(0));
//        for (Student stu : list) {
//            System.out.println(stu.getClassId()+"\t"+stu.getStudentName());
//        }
        
        //2.sql:用传统的sql语句来完成查询
        String sql = "select * from Student_info";
        SQLQuery squery = session.createSQLQuery(sql);
        squery.addEntity(Student.class);//指明结果封装对象
        squery.setFirstResult(5);
        squery.setMaxResults(5);
        List<Student> list2 = squery.list();
        for (Student stu : list2) {
            System.out.println(stu.getClassId()+"\t"+stu.getStudentName());
        }
        
        session.flush();
        session.close();
    }

}

 

实例2:写一个分页查询的dao方法

  Test2.java

package com.dao;

import java.util.List;

import com.pojo.Student;

public class Test2 {
    public static void main(String[] args) {
        IStudentDAO stuDAO = new StudentDAO();
List
<Student> list = stuDAO.queryStudentByPage(2, 5, "from Student"); for (Student stu : list) { System.out.println(stu.getStudentName()); } } }

  IStudentDAO.java

package com.dao;

import java.util.List;

import com.pojo.Student;

public interface IStudentDAO extends IBaseDAO<Student>{
    public List<Student> queryStudentByPage(int index,int pagecount,String hql);
}

  Student.java

package com.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.pojo.Student;

public class StudentDAO extends BaseDAO<Student> implements IStudentDAO{

    @Override
    public List<Student> queryStudentByPage(int index, int pagecount,String hql) {
        Session session = getSession();
Query hq
= session.createQuery(hql); hq.setFirstResult((index-1)*pagecount); hq.setMaxResults(pagecount);
List
<Student> list = hq.list();
session.flush();
     session.close();
return list; } }

 

附:IBaseDao.java

package com.dao;

import java.util.List;

import org.hibernate.Session;

public interface IBaseDAO<T> {

    Session getSession();

    void saveObject(T t);

    void delObject(T t);

    void updateObject(T t);

    List<T> queryObjectByHql(String hql);

    List<T> queryObjectBySql(String sql, Class t);

}

  BaseDao.java

package com.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.pojo.Student;


public class BaseDAO<T> implements IBaseDAO<T> {
    public Session getSession() {
        // 配置对象,用来获取核心配置文件的
        // 如果核心配置文件名是:hibernate.cfg.xml,并且该文件在src根目录下,configure()可以不带参数
        Configuration configuration = new Configuration().configure();
        ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                .buildServiceRegistry();
        SessionFactory sf = configuration.buildSessionFactory(sr);
        Session session = sf.openSession();
        return session;
    }
    
    public void saveObject(T t) {
        Session session = getSession();
        session.save(t);
        session.beginTransaction().commit();
        session.close();
    }

    public void delObject(T t) {
        Session session = getSession();
        session.delete(t);
        session.beginTransaction().commit();
        session.close();
    }

    public void updateObject(T t) {
        Session session = getSession();
        session.update(t);
        session.beginTransaction().commit();
        session.close();
    }

    public List<T> queryObjectByHql(String hql) {
        Session session = getSession();
        Query query = session.createQuery(hql);
        List<T> list = query.list();
        session.flush();
        session.close();
        return list;
    }

    public List<T> queryObjectBySql(String sql,Class t) {
        Session session = getSession();
        SQLQuery sq = session.createSQLQuery(sql);
        sq.addEntity(t);
        List<T> list = sq.list();
        session.flush();
        session.close();
        return list;
    }
}

 

转载于:https://www.cnblogs.com/wlxslsb/p/10790065.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值