HQL执行增删改查的步骤

HQL执行增删改查的步骤
2008-07-24 03:17

对与HQL首先要明白HQL是什么?

HQLHibernate Query LanuageHibernate查询语言

使用HQL用到的类

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

查询:

1.单体查询

public Object findById(Class cls,int id) {  //这里的class是指一个实体类,
   //1、读取配置文件
   Configuration conf = newConfiguration().configure();
   //2、创建SessionFactory
   SessionFactory sf =conf.buildSessionFactory();
   //3、打开Session
   Session session =sf.openSession();
   Object rtObj =session.get(cls,id);    //使用get方法查询
   session.close();
   return rtObj;
}

2.批量查询

public List query(String hql){
//   1、读取配置文件
   Configuration conf = newConfiguration().configure();
   //2、创建SessionFactory
   SessionFactory sf =conf.buildSessionFactory();
   //3、打开Session
   Session session =sf.openSession();

//Query query =session.createQuery(hql);

//List list = query.list();
   return session.createQuery(hql).list();

//查询createQuery返回的是一个Query,通过query的list方法得到查询的结果
}

3.模糊查询

只需将HQL语句定义为   whereentity.title like'%健翔桥%'

新增:

public booleanadd(Object obj) {
//   1、读取配置文件
   Configuration conf = newConfiguration().configure();
   //2、创建SessionFactory
   SessionFactory sf =conf.buildSessionFactory();
   //3、打开Session
   Session session =sf.openSession();
   Transaction tx = null;
   try {
   //4、开始一个事务
    tx =session.beginTransaction();
   session.save(obj);
    //6、提交事务
   tx.commit();     //不提交将不能执行增加记录的操作
   } catch (Exception e) {
    if (null !=tx) {
    tx.rollback();
    }
   e.printStackTrace();
    returnfalse;
   } finally {
   //7、关闭Session
   session.close();
   }
   return true;
}

 

删除:

public boolean delete(Class cls,int id) {
//   1、读取配置文件
   Configuration conf = newConfiguration().configure();
   //2、创建SessionFactory
   SessionFactory sf =conf.buildSessionFactory();
   //3、打开Session
   Session session =sf.openSession();
   Transaction tx = null;
   try {
   //4、开始一个事务
    tx =session.beginTransaction();
   session.delete(this.findById(cls,id));
    //6、提交事务
   tx.commit();     
   } catch (Exception e) {
    if (null !=tx) {
    tx.rollback();
    }
   e.printStackTrace();
    returnfalse;
   } finally {
   //7、关闭Session
   session.close();
   }
   return true;
}

 

 

修改:

public boolean update(Object obj){
//   1、读取配置文件
   Configuration conf = newConfiguration().configure();
   //2、创建SessionFactory
   SessionFactory sf =conf.buildSessionFactory();
   //3、打开Session
   Session session =sf.openSession();
   Transaction tx = null;
   try {
   //4、开始一个事务
    tx =session.beginTransaction();
   session.update(obj);
    //6、提交事务
   tx.commit();     
   } catch (Exception e) {
    if (null !=tx) {
    tx.rollback();
    }
   e.printStackTrace();
    returnfalse;
   } finally {
   //7、关闭Session
   session.close();
   }
   return true;
}

 

hibernate的配置文件为

hibernate.cfg.xml

其中存放的是hibernate 的连接数据库的信息

<hibernate-configuration>

<session-factory>
<propertyname="connection.username">sa</property>
<propertyname="connection.url">
  jdbc:microsoft:sqlserver://localhost:1433
</property>
<property name="dialect">方言
  org.hibernate.dialect.SQLServerDialect
</property>
<propertyname="myeclipse.connection.profile">myCon</property>
<propertyname="connection.driver_class">
  com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<propertyname="show_sql">true</property>
<mappingresource="com/zhong/dao/entity/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值