package com.java.domain.DAO;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.java.domain.HSFctory.HibernateSessionFactory;
import com.java.domain.POJO.Person;
public class Insert {
private Session session;
// 在构造方法之中实例化session对象
public Insert() {
//获得Session:
this.session = HibernateSessionFactory.getSession();
}
public void insert(Person p) {
//事务开始:
Transaction transaction = this.session.beginTransaction();
//执行事务:
this.session.save(p);
//事务提交
transaction.commit();
//关闭session:
this.session.close();
}
}
package com.java.domain.DAO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.java.domain.HSFctory.HibernateSessionFactory;
import com.java.domain.POJO.Person;
public class Delete {
private Session session;
public Delete() {
this.session = HibernateSessionFactory.getSession();
}
//该删除要先查出才删除。性能不高。
public void delete(Person p) {
//事务开始:
Transaction transaction = this.session.beginTransaction();
this.session.delete(p);
transaction.commit();
this.session.close();
}
// 在Hibernate 3之中根据HQL中的语句进行了修改,增加了删除指令。按ID删除
public void delete(Integer id)
{
String hql = "DELETE Person WHERE id=?" ;
Query q = this.session.createQuery(hql) ;
// 把参数设置
q.setInteger(0,id) ;
// 执行更新语句
q.executeUpdate() ;
// 进行事务处理
this.session.beginTransaction().commit() ;
this.session.close();
}
}
package com.java.domain.DAO;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.java.domain.HSFctory.HibernateSessionFactory;
import com.java.domain.POJO.Person;
public class Modify {
private Session session;
// 在构造方法之中实例化session对象
public Modify() {
//获得Session:
this.session = HibernateSessionFactory.getSession();
}
public void update(Person p) {
//事务开始:
Transaction transaction = this.session.beginTransaction();
//执行事务:
this.session.update(p);
//事务提交
transaction.commit();
//关闭session:
this.session.close();
}
}
package com.java.domain.DAO;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.java.domain.HSFctory.HibernateSessionFactory;
import com.java.domain.POJO.Person;
public class QueryUpdate {
private Session session;
// 在构造方法之中实例化session对象
public QueryUpdate() {
// 获得Session:
this.session = HibernateSessionFactory.getSession();
}
// 按ID查询
@SuppressWarnings("unchecked")
public Person queryById(Integer id) {
Person p = null;
//Person的 p要大写。因为Person类是大写开头。
String hsql = "from Person as p where p.id=?";
Query q = this.session.createQuery(hsql);
q.setInteger(0, id);
List list = q.list();
Iterator iter = list.iterator();
while (iter.hasNext()) {
p = (Person) iter.next();
}
return p;
}
// 查询全部数据,写HQL
@SuppressWarnings("unchecked")
public List queryAll() {
List list = null;
String hql = "FROM Person as p";
Query q = this.session.createQuery(hql);
list = q.list();
return list;
}
// 模糊查询,按名称模糊查询:
@SuppressWarnings("unchecked")
public List queryByLike(String cond) {
List list = null;
String hql = "FROM Person as p WHERE p.name like ?";
Query q = this.session.createQuery(hql);
q.setString(0, "%" + cond + "%");
list = q.list();
return list;
}
}
测试类
package com.java.domain.test;
import java.util.Iterator;
import java.util.List;
import com.java.domain.DAO.Insert;
import com.java.domain.DAO.Modify;
import com.java.domain.DAO.QueryUpdate;
import com.java.domain.POJO.Person;
public class TestPojo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();
Modify mod = new Modify();
Insert it = new Insert();
// p.setId(2);
// p.setName("周ff23s斌");
// p.setPassword("bbbbbbb");
// p.setSex("女");
// p.setEmail("
bbbb.@gmail.com");
// it.insert(p);
//
//
// it.update(p);
QueryUpdate qud = new QueryUpdate();
// Delete dl = new Delete();
// Person ps = qud.queryById(2);
// System.out.println(ps.getName());
// dl.delete(3);
// List list = qud.queryAll();
// Iterator<Person> iter = list.iterator();
// while (iter.hasNext()) {
// Person person = (Person) iter.next();
// System.out.println(person.getName());
// System.out.println(person.getId());
// }
List list = qud.queryByLike("v");
Iterator<Person> iter = list.iterator();
while (iter.hasNext()) {
Person person = (Person) iter.next();
System.out.println(person.getName());
}
}
}
使用myeclipse自动生成HibernateSessionFactory.java