📄 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 + -