?? persistenceservice.java
字號:
package com.flowdemo.service;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.flowdemo.po.Expenses;
import com.flowdemo.po.Users;
import com.flowdemo.util.HibernateSessionFactory;
public class PersistenceService {
/**
* 保存一個報銷單對象
* @param expense
*/
public static void save(Expenses expense){
Session hibernateSession = HibernateSessionFactory.getSession();
try{
//hibernateSession.getTransaction().begin();
hibernateSession.beginTransaction();
hibernateSession.saveOrUpdate(expense);
hibernateSession.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
hibernateSession.getTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 更新一個報銷單對象
* @param expense
*/
public static void update(Expenses expense){
Session hibernateSession = HibernateSessionFactory.getSession();
try{
//hibernateSession.getTransaction().begin();
hibernateSession.beginTransaction();
//hibernateSession.saveOrUpdate(expense);
hibernateSession.update(expense);
hibernateSession.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
hibernateSession.getTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 刪除一個報銷單對象
* @param expense
*/
public static void delete(Expenses expense){
Session hibernateSession = HibernateSessionFactory.getSession();
try{
//hibernateSession.getTransaction().begin();
hibernateSession.beginTransaction();
hibernateSession.delete(expense);
hibernateSession.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
hibernateSession.getTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 返回一個報銷單列表
* @return
*/
public static List<Expenses> getExpenses(){
Session hibernateSession = HibernateSessionFactory.getSession();
List<Expenses> expenses = new ArrayList<Expenses>();
try{
//hibernateSession.getTransaction().begin();
hibernateSession.beginTransaction();
//hibernateSession.delete(expense);
Query query = hibernateSession.createQuery("from Expenses");
expenses = query.list();
hibernateSession.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
hibernateSession.getTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
return expenses;
}
/**
* 根據用戶ID返回一個對象
* @param userId
* @return
*/
public static Users getUserById(String userId){
Session hibernateSession = HibernateSessionFactory.getSession();
Users user = new Users();
try{
hibernateSession.beginTransaction();
user = (Users)hibernateSession.get(Users.class, userId);
hibernateSession.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
hibernateSession.getTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
return user;
}
/**
* 檢查用戶是否存在
* @param user
* @return
*/
public static boolean checkUser(Users user){
Users user2 = null;
try{
user2 = getUserById(user.getUserId());
System.out.println("用戶的值==="+user2.getUserName());
}catch(Exception e){
e.printStackTrace();
}finally{
//HibernateSessionFactory.closeSession();
}
if(user2.getUserPass() == user.getUserPass() || user2.getUserPass().equals(user.getUserPass())){
return true;
}else{
return false;
}
}
/**
* 測試主方法
* @param args
*/
public static void main(String[]args){
Session hibernateSession = HibernateSessionFactory.getSession();
System.out.println("測試Session是否啟動1"+hibernateSession.toString());
hibernateSession.close();
System.out.println("測試Session是否啟動2"+hibernateSession.toString());
hibernateSession = HibernateSessionFactory.getSession();
System.out.println("測試Session是否啟動1"+hibernateSession.toString());
HibernateSessionFactory.closeSession();
System.out.println("測試Session是否啟動2"+hibernateSession.toString());
Users user = new Users();
user.setUserId("001");
user.setUserPass("employee");
boolean flag = checkUser(user);
System.out.println("是否是正確的?"+flag);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -