(转)Hibernate中的Query一些基本用法

/**
* 添加
*/
public void save(Stu stu){
try {

tran=this.GetSession().beginTransaction();

this.GetSession().save(stu);
tran.commit();
} catch
(HibernateException e) {
throw e;
}finally{

this.CloseSession();
}
}

/**
* 使用HQL全查询
*/
public List getallbyHQL(){
List
arr=null;
try {
String hql="from Stu";
Query
query=this.GetSession().createQuery(hql);
arr=query.list();
} catch
(HibernateException e) {
throw e;
}finally{

this.CloseSession();
}
return arr;
}
/**
*
根据主键查询
*/
public Stu getbyID(int id){
Stu stu=null;
try {

stu=(Stu) this.GetSession().get(Stu.class, id);
} catch
(HibernateException e) {
throw e;
}finally{

this.CloseSession();
}
return stu;
}

/**
* 根据对象属性查询(使用Query)
*/
public List getbyPropertyQuery(String
name){
List arr=null;
try {
//这里不能像SQL语一样select * from Stu
where SName=:name,这是不对的。
// Query
query=this.GetSession().createQuery("from Stu where SName=:name");
//
query.setString("name", name);
//或者
Query
query=this.GetSession().createQuery("from Stu where SName=?");

query.setString(0, name);
arr=query.list();
} catch
(HibernateException e) {
throw e;
}finally{

this.CloseSession();
}
return arr;
}

/**
* 根据对象属性查询(使用Criteria)
*/
public List
getbyPropertyCriteria(String name){
List arr=null;
try {

Criteria cri=this.GetSession().createCriteria(Stu.class);
Criterion
c1=Expression.eq("SName", name);
cri.add(c1);
arr=cri.list();

} catch (HibernateException e) {
throw e;
}finally{

this.CloseSession();
}
return arr;
}

/**
* 查询部分属性
*/
public List getProperty(){
List arr=new
ArrayList();
try {
String hql="select s.SName,s.SSex from Stu as
s";
Query query=this.GetSession().createQuery(hql);
List
list=query.list();
Iterator iter=list.iterator();

while(iter.hasNext()){
Object[] obj=(Object[]) iter.next();
Stu
s=new Stu();
s.setSName(obj[0].toString());

s.setSSex(obj[1].toString());
arr.add(s);
}
} catch
(HibernateException e) {
this.CloseSession();
}
return
arr;
}
/**
* 查询一个属性
*/
public List getoneProperty(){
List
arr=new ArrayList();
try {
String hql="select s.SName from Stu as
s";
Query query=this.GetSession().createQuery(hql);
Iterator
iter=query.iterate();
while(iter.hasNext()){
Object obj=(Object)
iter.next();
Stu s=new Stu();
s.setSName(obj.toString());

arr.add(s);
}
} catch (HibernateException e) {

this.CloseSession();
}
return arr;
}

/**
*查询一个对象一个属性值
*/
public Object getonlyProprotyValue(int
s_id){
Object obj=null;
try {
String hql="select s.SName from
Stu as s where s.SId=?";
Query
query=this.GetSession().createQuery(hql);
query.setInteger(0, s_id);

obj=query.uniqueResult();
} catch (HibernateException e) {
throw
e;
}finally{
this.CloseSession();
}
return
obj;
}
/**
* SQL查询
*/
public List getallBYSQL(){
List
arr=null;
try {
String sql="select {c.*} from stu as c";

SQLQuery sqlquery=this.GetSession().createSQLQuery(sql);

sqlquery.addEntity("c",Stu.class);
arr=sqlquery.list();
} catch
(HibernateException e) {
throw e;
}finally{

this.CloseSession();
}
return arr;
}

/**
* 根据对象查询
*/
public List getallByObject(Stu s){
List
arr=null;
try {
String hql="from Stu as s where s=:stuentity";

//或者
//String hql="from Stu as s where s.SId=:stuentity";
Query
query=this.GetSession().createQuery(hql);
query.setEntity("stuentity",
s);
arr=query.list();
} catch (HibernateException e) {
throw
e;
}finally{
this.CloseSession();
}
return
arr;
}

/**
* 模糊查询
*/
public List getallQueryLike(String name){
List
arr=null;
try {
String hql="from Stu as s where s.SName like
:name";
Query query=this.GetSession().createQuery(hql);

query.setString("name", "%"+name+"%");
//不能
//query.setString("name",
"‘%"+name+"%‘");
arr=query.list();
} catch (HibernateException e)
{
throw e;
}finally{
this.CloseSession();
}

return arr;
}
/**
* 统计函数
*/
public int CountStu(){
int
count=0;
try {
String hql="select count(*) from Stu";
Query
query=this.GetSession().createQuery(hql);
count=(Integer)
query.uniqueResult();
} catch (HibernateException e) {
throw e;

}finally{
this.CloseSession();
}
return count;
}

/**
* 条件统计
*/
public int CountByWhere(String sex){
int
count=0;
try {
Query query=this.GetSession().createQuery("select
count(*) from Stu where SSex=:sex");
query.setString("sex", sex);

count=(Integer)query.uniqueResult();
} catch (HibernateException e) {

throw e;
}finally{
this.CloseSession();
}
return
count;
}

/**
* 统计平均值
*/
public float VagAge(){
float vag=0;
try
{
Query query=this.GetSession().createQuery("select avg(SAge) from
Stu");
vag=(Float)query.uniqueResult();
} catch (HibernateException
e) {
throw e;
}finally{
this.CloseSession();
}

return vag;
}

/**
* 求和函数
*/
public int sumage(){
int sum=0;
try {

Query query=this.GetSession().createQuery("select sum(SAge) from Stu");

sum=(Integer)query.uniqueResult();
} catch (HibernateException e) {

throw e;
}finally{
this.CloseSession();
}
return
sum;
}

转自http://hlbng.iteye.com/blog/401548

(转)Hibernate中的Query一些基本用法,布布扣,bubuko.com

时间: 2024-07-31 14:35:11

(转)Hibernate中的Query一些基本用法的相关文章

hibernate 中的query的分页查询

//方法描述:根据会员名称和页容量分页查询代理人 public List<HbUser> findUserByPage(int page,int pageSize, String userName)   throws Exception {  Session session = sessionFactory.getCurrentSession();  StringBuffer hql = new StringBuffer("from HbUser where isDelete = 0

分享知识-快乐自己:Hibernate 中Criteria Query查询详解

1):Hibernate 中Criteria Query查询详解 当查询数据时,人们往往需要设置查询条件.在SQL或HQL语句中,查询条件常常放在where子句中. 此外,Hibernate还支持Criteria查询(Criteria Query),这种查询方式把查询条件封装为一个Criteria对象. 在实际应用中,使用Session的createCriteria()方法构建一个org.hibernate.Criteria实例,然后把具体的查询条件通过Criteria的add()方法加入到Cr

hibernate 中createQuery与createSQLQuery的用法

hibernate 中createQuery与createSQLQuery两者区别是:前者用的hql语句进行查询,后者可以用sql语句查询前者以hibernate生成的Bean为对象装入list返回后者则是以对象数组进行存储 比如我们在获得session后: createQuery: try{ Session session=this.hibernateTemplate.getSessionFactory().getCurrentSession(); //这里的表名dcn_flow是数据库里的表

Hibernate 中Criteria Query查询详解【转】

当查询数据时,人们往往需要设置查询条件.在SQL或HQL语句中,查询条件常常放在where子句中.此外,Hibernate还支持Criteria查询(Criteria Query),这种查询方式把查询条件封装为一个Criteria对象.在实际应用中,使用Session的createCriteria()方法构建一个org.hibernate.Criteria实例,然后把具体的查询条件通过Criteria的add()方法加入到Criteria实例中.这样,程序员可以不使用SQL甚至HQL的情况下进行

Hibernate中的query.setFirstResult(),query.setMaxResults();

一.query.scroll()和query.setFirstResult(),query.setMaxResults();这两种方法都可以取到一定范围内的数据,用来数据分页显示.那么两者区别,以及两者的效率如何? 答:1.scroll是用JDBC2.0的可滚动结果集实现:query.setMaxResults();query.setFirstResult()是数据库SQL语句实现. 2.你说是在数据库就分页好呢?还是把结果集都取到内存再分页好呢?(应该是在数据库就分了好些吧,但是如果在内存分页

Hibernate中Criteria的完整用法

Hibernate中Criteria的完整用法 转载 criteria 英[kra??t??r??] 美[kra??t?r??] 标准 1,Criteria Hibernate 设计了 CriteriaSpecification 作为 Criteria 的父接口,下面提供了 Criteria和DetachedCriteria . 2,DetachedCriteria Spring 的框架提供了getHibernateTemplate ().findByCriteria(detachedCrite

Hibernate中createCriteria即QBC查询的详细用法 .Hibernate中createCriteria即QBC查询的详细用法 .

现在假设有一个Student类,内有id,name,age属性String hql = "from Student s";按照以前的做法,我们通常是Query query = session.createQuery(hql);或者要按照条件检索的话.String hql = "from Student s where s.name like '王%'"Query query = session.createQuery(hql);不用HQL而使用QBC的话,那么代码为

Hibernate中Criteria的用法

Hibernate中Criteria的用法 criteria英[kra??t??r??]美[kra??t?r??]标准 1,Criteria Hibernate 设计了 CriteriaSpecification 作为 Criteria 的父接口,下面提供了 Criteria和DetachedCriteria . 2,DetachedCriteria Spring 的框架提供了getHibernateTemplate ().findByCriteria(detachedCriteria) 方法可

hibernate 中createQuery与createSQLQuery两个用法

hibernate 中createQuery与createSQLQuery两者区别是:前者用的hql语句进行查询,后者可以用sql语句查询前者以hibernate生成的Bean为对象装入list返回后者则是以对象数组进行存储 比如我们在获得session后: createQuery: try{ Session session=this.hibernateTemplate.getSessionFactory().getCurrentSession(); //这里的表名dcn_flow是数据库里的表