📄 catdaoimp.java
字号:
package com.shopping.dao.imp;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.impl.AbstractQueryImpl;
import com.shopping.dao.UnifyDao;
import com.shopping.util.HibernateSessionFactory;
import com.shopping.vo.Category;
import com.shopping.vo.Item;
public class CatDaoImp implements UnifyDao {
public boolean deleteTableById(int id) {
Session session = HibernateSessionFactory.getSession();
Transaction tran =null;
try {
Category cat = (Category) session.get(Category.class, id);
tran=session.beginTransaction();
session.delete(cat);
tran.commit();
return true;
} catch (HibernateException e) {
e.printStackTrace();
tran.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
return false;
}
public boolean deleteTableByPid(int pid) {
Session session = HibernateSessionFactory.getSession();
Transaction tran =null;
String hql = "delete Category c where c.pid=:pid";
try {
tran=session.beginTransaction();
Query q = session.createQuery(hql);
q.setInteger("pid", pid);
q.executeUpdate();
tran.commit();
return true;
} catch (HibernateException e) {
e.printStackTrace();
tran.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
return false;
}
public List getTable(int pageSize, int startRow) {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Category as u";
Query q = session.createQuery(hql);
q.setMaxResults(pageSize);
q.setFirstResult(startRow);
List l = q.list();
return l;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return null;
}
public int getRows() {
int totalRows = 0;
try {
Session session = HibernateSessionFactory.getSession();
String query = "select count(*) from Category u";
totalRows = (Integer)session.createQuery(query).uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return totalRows;
}
public List getTable(String keyword,int pageSize, int startRow) {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Category as s where s.name like '%"+keyword+"%'";
Query q = session.createQuery(hql);
q.setMaxResults(pageSize);
q.setFirstResult(startRow);
List l = q.list();
return l;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return null;
}
public int getRows(String keyword) {
int totalRows = 0;
try {
Session session = HibernateSessionFactory.getSession();
String query = "select count(*) from Category as s where s.name like '%"+keyword+"%'";
totalRows = (Integer)session.createQuery(query).uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return totalRows;
}
public Object getTableById(int id) {
Session session = HibernateSessionFactory.getSession();
Category cat =null;
try {
cat = (Category) session.get(Category.class, id);
return cat;
} catch (HibernateException e) {
e.printStackTrace();
}finally {
HibernateSessionFactory.closeSession();
}
return null;
}
public boolean insertTable(Object object) {
Session session = HibernateSessionFactory.getSession();
Transaction tran =null;
try {
tran=session.beginTransaction();
session.save(object);
tran.commit();
return true;
} catch (HibernateException e) {
tran.rollback();
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return false;
}
public boolean updateTable(Object object) {
Session session = HibernateSessionFactory.getSession();
Transaction tran =null;
try {
tran=session.beginTransaction();
session.update(object);
tran.commit();
return true;
} catch (HibernateException e) {
tran.rollback();
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return false;
}
public List getTable() {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Category as s ";
Query q = session.createQuery(hql);
List l = q.list();
return l;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return null;
}
public List getTable(int pid) {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Category as s where s.pid=:pid";
Query q = session.createQuery(hql);
q.setInteger("pid", pid);
List l = q.list();
return l;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return null;
}
public Object getTableByName(String name) {
Session session = HibernateSessionFactory.getSession();
Category cat =null;
String hql = "from Category u where u.name=:catname";
try {
AbstractQueryImpl q = (AbstractQueryImpl) session.createQuery(hql);
q.setString("catname", name);
cat = (Category) q.uniqueResult();
return cat;
} catch (HibernateException e) {
e.printStackTrace();
return null;
}finally {
HibernateSessionFactory.closeSession();
}
}
public int getRows(int catId) {
// TODO Auto-generated method stub
return 0;
}
public List getTable(int catId, int pageSize, int startRow) {
// TODO Auto-generated method stub
return null;
}
public int getRows2(int catId) {
// TODO Auto-generated method stub
return 0;
}
public List getTable2(int catId, int pageSize, int startRow) {
// TODO Auto-generated method stub
return null;
}
public int getRows(String keyword, int id) {
// TODO Auto-generated method stub
return 0;
}
public int getRows2(String keyword, int id) {
// TODO Auto-generated method stub
return 0;
}
public List getTable(String keyword, int id, int pageSize, int startRow) {
// TODO Auto-generated method stub
return null;
}
public List getTable2(String keyword, int id, int pageSize, int startRow) {
// TODO Auto-generated method stub
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -