?? powerdao.java
字號:
package dao;
import java.util.List;
import org.hibernate.*;
import vo.*;
import factory.*;
public class PowerDao {
public void insertPower(Power power) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(power);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
System.out.println(e);
} finally {
session.close();
}
}
public void updatePower(Power power) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(power);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
System.out.println(e);
} finally {
session.close();
}
}
public void deletePower(Power power) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(power);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
System.out.println(e);
} finally {
session.close();
}
}
public Power searchPower(int id) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
Power power = null;
try {
tx = session.beginTransaction();
power = (Power) session.get(Power.class, Integer.valueOf(id));
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
System.out.println(e);
} finally {
session.close();
}
return power;
}
public List searchAll() {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
List list = null;
try {
tx = session.beginTransaction();
list = session.createQuery("from Power").list();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
System.out.println(e);
} finally {
session.close();
}
return list;
}
public List searchByTitle(String title) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
List list = null;
try {
tx = session.beginTransaction();
list = session.createQuery(
"from Power p where p.title like '%"+title+"%'").list();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
System.out.println(e);
} finally {
session.close();
}
return list;
}
public static void main(String args[]) {
Power power = new Power();
// power.setTitle("用戶管理");
// power.setContent("userManager");
PowerDao pdao = new PowerDao();
// pdao.insertPower(power);
// power=pdao.searchPower(1);
// power.setContent("userManage");
// pdao.updatePower(power);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -