HQL执行增删改查的步骤
2008-07-24 03:17
对与HQL首先要明白HQL是什么?
HQL是Hibernate Query Lanuage(Hibernate查询语言)
使用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>