package com.Hibernate.util;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.Hibernate.persistence.*;
public class Gethibernate {
private Session session;
private Hibernate hib = new Hibernate();
public List finplacardId(int intid) {
Transaction tx = null;
List list = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
Query query = session.createQuery("from Consumer as p where p.id=:intid"); //创建Query对象
query.setInteger("intid",intid); //动态绑定参数。
list = query.list(); //调用list方法执行查询语句,该方法返回List类型
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return list;
}
//按id号查找文章类型
public List finAritId(int intid) {
Transaction tx = null;
List list = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
Query query = session.createQuery("from AriticleType as p where p.id=:intid"); //创建Query对象
query.setInteger("intid",intid); //动态绑定参数。
list = query.list(); //调用list方法执行查询语句,该方法返回List类型
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return list;
}
//修改文章信息
public void updateAri(Article ariticle) {
Transaction tx = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
session.update(ariticle);
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
}
//删除文章信息
public void deleAri(Article ariticle) {
Transaction tx = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
session.delete(ariticle);
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
}
//删除文章信息
public void deleAritype(AriticleType atype) {
Transaction tx = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
session.delete(atype);
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
}
// 按照用户名寻找用户
public List findUser(String strUserName) {
Transaction tx = null;
List list = null;
try {
session = hib.openSession(); //调用公共类的开启session方法.
tx = (Transaction) session.beginTransaction(); //开启事物
Query query = session.createQuery("from Consumer as u where u.account=:strUserName"); //应用HQL检索查找满足条件的集合
query.setString("strUserName", strUserName); //动态绑定参数
list = query.list(); //list方法用来执行HQL语句
tx.commit(); //事务的提交
hib.closeSession(session); //关闭session
} catch (Exception e) {
e.printStackTrace(); //输出异常信息
tx.rollback(); //事务的回滚
}
return list;
}
// 修改博主信息
public List updateCons(Consumer cons) {
Transaction tx = null;
List list = null;
try {
session = hib.openSession(); //调用公共类的开启session方法.
tx = (Transaction) session.beginTransaction(); //开启事物
session.update(cons); //动态绑定参数
//list方法用来执行HQL语句
tx.commit(); //事务的提交
hib.closeSession(session); //关闭session
} catch (Exception e) {
e.printStackTrace(); //输出异常信息
tx.rollback(); //事务的回滚
}
return list;
}
// 修改公告信息
public List updateDiss(Discuss discuss) {
Transaction tx = null;
List list = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
session.update(discuss);
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return list;
}
// 删除公告信息
public List deleDiss(Discuss discuss) {
Transaction tx = null;
List list = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
session.delete(discuss);
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return list;
}
// 保存评论信息
public void saveComm(Comm comm) {
Transaction tx = null;
try {
session = hib.openSession();
tx = (Transaction) session.beginTransaction();
session.save(comm);
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
}
// 保存公告信息
public void saveDiss(Discuss diss) {
Transaction tx = null;
try {
session = hib.openSession(); //调用公共类的开启session方法.
tx = (Transaction) session.beginTransaction(); //开启事物
session.save(diss); //动态绑定参数
//list方法用来执行HQL语句
tx.commit(); //事务的提交
hib.closeSession(session); //关闭session
} catch (Exception e) {
e.printStackTrace(); //输出异常信息
tx.rollback(); //事务的回滚
}
}
//按照用户名和密码寻找用户
public List findUser(String strUserName, String strPwd) {
Transaction tx = null;
List list = null;
try {
session = hib.openSession(); //调用公共类的开启session方法.
tx = (Transaction) session.beginTransaction(); //开启事物
Query query = session.createQuery("from Consumer as u where u.account=:strUserName and u.passWord=:strPwd"); //应用HQL检索查找满足条件的集合
query.setString("strUserName", strUserName); //动态绑定参数
query.setString("strPwd", strPwd);
list = query.list(); //list方法用来执行HQL语句
tx.commit(); //事务的提交
hib.closeSession(session); //关闭session
} catch (Exception e) {
e.printStackTrace(); //输出异常信息
tx.rollback(); //事务的回滚
}
return list;
}
//寻找朋友圈方法
public List findFrinend() {
Transaction tx = null;
List list = null;
try {
session = hib.openSession(); //调用公共类的开启session方法.
tx = (Transaction) session.beginTransaction(); //开启事物
Query query = session.createQuery("from Friend"); //应用HQL检索查找满足条件的集合
//动态绑定参数
list = query.list(); //list方法用来执行HQL语句
tx.commit(); //事务的提交
hib.closeSession(session); //关闭session
} catch (Exception e) {
e.printStackTrace(); //输出异常信息
tx.rollback(); //事务的回滚
}
return list