itemdaoimp.java
来自「struts hibernate框架 商场买卖」· Java 代码 · 共 342 行
JAVA
342 行
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.Item;
public class ItemDaoImp implements UnifyDao {
public boolean deleteTableById(int id) {
Session session = HibernateSessionFactory.getSession();
Transaction tran =null;
try {
Item item = (Item) session.load(Item.class, id);
tran=session.beginTransaction();
session.delete(item);
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 Item as u order by u.pdate desc";
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 Item 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 Item 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 Item as s where s.name like '%"+keyword+"%'";
totalRows = (Integer)session.createQuery(query).uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return totalRows;
}
public List getTable(int catId,int pageSize, int startRow) {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Item as u where u.category.id=:catId order by u.pdate desc";
Query q = session.createQuery(hql);
q.setInteger("catId", catId);
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 catId) {
int totalRows = 0;
try {
Session session = HibernateSessionFactory.getSession();
String query = "select count(*) from Item u where u.category.id="+catId;
totalRows = (Integer)session.createQuery(query).uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return totalRows;
}
public List getTable2(int catId,int pageSize, int startRow) {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Item as u where u.category.pid=:catId order by u.pdate desc";
Query q = session.createQuery(hql);
q.setInteger("catId", catId);
q.setMaxResults(pageSize);
q.setFirstResult(startRow);
List l = q.list();
return l;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return null;
}
public int getRows2(int catId) {
int totalRows = 0;
try {
Session session = HibernateSessionFactory.getSession();
String query = "select count(*) from Item u where u.category.pid="+catId;
totalRows = (Integer)session.createQuery(query).uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return totalRows;
}
public List getTable(String keyword,int id ,int pageSize, int startRow) {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Item as u where u.category.id=:catId and u.name like '%"+keyword+"%'";
Query q = session.createQuery(hql);
q.setInteger("catId", id);
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 id) {
int totalRows = 0;
try {
Session session = HibernateSessionFactory.getSession();
String hql = "select count(*) from Item u where u.category.id=:catId and u.name like '%"+keyword+"%'";
Query q = session.createQuery(hql);
q.setInteger("catId", id);
totalRows = (Integer)q.uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return totalRows;
}
public List getTable2(String keyword,int id ,int pageSize, int startRow) {
try {
Session session = HibernateSessionFactory.getSession();
String hql = "from Item as u where u.category.pid=:catId and u.name like '%"+keyword+"%' order by u.pdate desc";
Query q = session.createQuery(hql);
q.setInteger("catId", id);
q.setMaxResults(pageSize);
q.setFirstResult(startRow);
List l = q.list();
return l;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return null;
}
public int getRows2(String keyword,int id) {
int totalRows = 0;
try {
Session session = HibernateSessionFactory.getSession();
String hql = "select count(*) from Item u where u.category.pid=:catId and u.name like '%"+keyword+"%'";
Query q = session.createQuery(hql);
q.setInteger("catId", id);
totalRows = (Integer)q.uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return totalRows;
}
public Object getTableById(int id) {
Session session = HibernateSessionFactory.getSession();
Item item =null;
try {
item = (Item) session.get(Item.class, id);
return item;
} 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 Item as s order by u.pdate desc";
Query q = session.createQuery(hql);
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();
Item item =null;
String hql = "from Item u where u.name=:itemname";
try {
AbstractQueryImpl q = (AbstractQueryImpl) session.createQuery(hql);
q.setString("itemname", name);
item = (Item) q.uniqueResult();
return item;
} catch (HibernateException e) {
e.printStackTrace();
return null;
}finally {
HibernateSessionFactory.closeSession();
}
}
public List getTable(int categoryId) {
return null;
}
public boolean deleteTableById(String id) {
// TODO Auto-generated method stub
return false;
}
public boolean deleteTableByPid(int pid) {
// TODO Auto-generated method stub
return false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?