JavaEE框架---Hibernate---單表 增、刪、該、查
阿新 • • 發佈:2018-12-13
如果利用Hibernate修改資料庫時,需要使用事務處理,一個事務提交時才真正將修改過的記錄更新到資料庫中。
區別一下openSession和currentSession
在我測試增刪改查時,發現openSession和currentSession是不一樣的。我的測試結論:
經測試發現,openSession獲得到的 session在查詢時可以不用開啟事務, 而 currentSession在查詢時必須要開啟事務,並且在事務提交後會自動關閉session。
- openSession:每次獲取到的都是新新的,所以為實現同currentSession一樣單執行緒共享。所以,我在HibernateUtil工具類中使用ThreadLocal執行緒區域性變數容器。
- currentSession:只要當前執行緒中的session沒有提交事務,後面獲得到的都是同一個。
Hibernate物件的三種狀態
* transient:沒有 id值的物件,不受session管理 * persistent: 有id值的物件,且受session管理 * detached:有id值的物件,但是不受session管理
增加記錄
// 使瞬時態的物件 轉化成 持久態 @Test public void save() { //Student student = new Student("S010","張三",23,"D004"); //Student student = new Student("S011","張飛",33,"D005"); Student student = new Student("S077","張飛",33,"D005"); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); System.out.println("11111111"); //session.save(student); //插入資料庫 --- 非延遲載入主鍵,但是延遲查詢 session.persist(student); //插入資料庫---延遲載入主鍵 /* 由於前面儲存了,所以 student物件處於persist狀態,受session管理, * 所以當student物件呼叫 setXXX方法時會發起修改的資料庫操作。 */ student.setAge(66); // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // e.printStackTrace(); // } tx.commit(); }
修改記錄
@Test public void update() { Student student = new Student("S010","張-三",12,"D006"); //Student student = new Student("S011","李三",16,"D006"); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); session.saveOrUpdate(student); //新增或者修改 //session.merge(student); //修改 tx.commit(); }
刪除記錄
//刪除 就是把 持久態-->託管態
@Test
public void delet() {
//Student student = new Student("S010","sssad-三",0,"0");
Student student = new Student(null,"sssad-三",0,"0"); //主鍵沒有值是,認為該實體是空
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.delete(student); //刪除,根據物件的主鍵去刪除
tx.commit();
}
查詢
@Test
public void querySingle() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Student student = session.get(Student.class, "s002");
System.out.println(student);
tx.commit();
}
@Test
public void queryAll() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
String hql = "from Student";
Query query = session.createQuery(hql);
List<?> list = query.list();
System.out.println( list );
tx.commit();
}
@Test
public void qeuryByCondition() {
Student student = new Student();
// student.setStudId("s002");
// student.setAge(20);
student.setStudName("張");
// student.setDeptId("D002");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
//用flag的各位二進位制數 判斷該欄位是否有條件
int flag=0;
String hql = "from Student s where 1=1 ";
//進行按位或運算
if( student.getStudId() != null ) {
hql += "and s.studId=:studId ";
flag |= 1; //2^0
}
if( student.getStudName() != null ) {
hql += "and s.studName like :studName ";
flag |= 2; //2^1
}
if( student.getAge() != null ) {
hql += "and s.age>:age ";
flag |= 4; //2^2
}
if( student.getDeptId() != null ) {
hql += "and s.deptId=:deptId";
flag |= 8; //2^3
}
System.out.println(hql);
Query query = session.createQuery(hql);
//進行按位與運算
if( (flag&1) == 1 ) { //2^0
query.setParameter("studId", student.getStudId() );
}
if( (flag&2) == 2 ) { //2^1
query.setParameter("studName", "%"+student.getStudName()+"%" );
}
if( (flag&4) == 4 ) { //2^2
query.setParameter("age", student.getAge() );
}
if( (flag&8) == 8 ) { //2^3
query.setParameter("deptId", student.getDeptId() );
}
List<?> list = query.list();
System.out.println( list );
tx.commit();
}