📄 bookdaoimpl.java
字号:
package com.zzu.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zzu.dao.BookDao;
import com.zzu.dao.entity.Book;
import com.zzu.dao.entity.Student;
public class BookDaoImpl implements BookDao {
Session se=null;
Transaction tx=null;
public void add(Book book) {
try {
se = HibernateSessionFactory.getSession();
tx = se.beginTransaction();
System.out.println(book.getBtitle());
se.save(book);
tx.commit();
System.out.println("留言添加成功!");
} catch (Exception e) {
if (null != tx)
tx.rollback();
e.printStackTrace();
} finally {
se.close();
}
}
public void del(Book book) {
try {
se = HibernateSessionFactory.getSession();
tx = se.beginTransaction();
se.delete(book);
tx.commit();
System.out.println("留言删除成功!");
//ok = 1;
} catch (Exception e) {
if (null != tx)
tx.rollback();
e.printStackTrace();
} finally {
se.close();// 关闭 Session
}
}
public List findAllBook(Book condition) {
List bookList = null;
se = HibernateSessionFactory.getSession();
String hql = "from Book";
if(condition==null)
{
hql=hql;
}
try {
Query query = se.createQuery(hql);
bookList = query.list();
} catch (Exception e) {
System.out.println("bookDaoImpl 类 Exception");
e.printStackTrace();
} finally {
se.close();
}
return bookList;
}
public Book findBookInfoById(int bid) {
Book book=null;
se=HibernateSessionFactory.getSession();
//Transaction tx=se.beginTransaction();
String hql="from Book book where book.bid="+bid;
Query query=se.createQuery(hql);
List list=query.list();
if(list.size()>0)
{
book=(Book)list.get(0);
System.out.println(book.getBtitle());
}
//tx.commit();
return book;
}
public void update(Book book) {
try{
se=HibernateSessionFactory.getSession();
tx=se.beginTransaction();
book.setBtitle("yin");
// student.setSpwd("12");
// student.setSage("23");
// student.setSsex("男");
// student.setSdept("zzu");
se.update(book);
System.out.println("留言信息修改成功!");
tx.commit();
}catch(Exception e)
{
if(null!=tx)
{
tx.rollback();
}
}finally{
se.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -