Spring:版本3.2.9 Hibernate:版本4.2.19
BaseDaoImpl.java
public class BaseDaoImpl<T> implements BaseDao<T> {
public SessionFactory sessionFactory;
protected Class<T> entityClass;
@Override
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public Session getSession(){
return sessionFactory.getCurrentSession();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public BaseDaoImpl() {
Class c = getClass();
Type type = c.getGenericSuperclass();
if (type instanceof ParameterizedType) {
Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
this.entityClass = (Class<T>) parameterizedType[0];
}
}
@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public boolean save(T entity) {
try {
getSession().save(entity);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean delete(T entity) {
try {
getSession().delete(entity);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean update(T entity) {
try {
getSession().update(entity);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@SuppressWarnings("unchecked")
@Override
public T get(Integer id) {
return (T) getSession().get(entityClass, id);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findByIdSet(Integer[] ids) {
Criteria criteria=getSession().createCriteria(entityClass);
criteria.add(Restrictions.in("id", ids));
return (List<T>)criteria.list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> findAllList() {
return getSession().createCriteria(entityClass).list();
}
@SuppressWarnings("unchecked")
@Override
public Pager findByPager(Pager pager) {
Criteria criteria=getSession().createCriteria(entityClass);
criteria.setProjection(Projections.rowCount());
int totalCount = ((Long) criteria.uniqueResult()).intValue();
pager.setTotalCount(totalCount);
criteria.setProjection(null);
pager.init();
criteria.setFirstResult((pager.getPageIndex()-1)*pager.getPageSize());
criteria.setMaxResults(pager.getPageSize());
List<T> te =(List<T>)criteria.list();
pager.setDatas((List<T>)criteria.list());
return pager;
}
}
Pager.java
import java.util.List;
public class Pager {
private int pageSize;
private int pageIndex;
private int totalCount;
private int totalPage;
private List<?> datas;
private boolean hasNextPage;
private boolean hasPreviousPage;
public void init(){
pageIndex=1;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<?> getDatas() {
return datas;
}
public void setDatas(List<?> datas) {
this.datas = datas;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
}
实体:Student.java
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name="student")
public class Student {
private int stu_id;
private String stu_name;
private String stu_password;
private Department department;
private Major major;
private String stu_sex;
private String stu_per_sig;
private String stu_head_img;
private Date stu_regist_time;
private String stu_attn_crs_ids;
private String stu_pw_question;
private String stu_pw_answer;
public Student() {
}
public Student(int stu_id, String stu_name, String stu_password,
Department department, Major major, String stu_sex,
String stu_per_sig, String stu_head_img, Date stu_regist_time,
String stu_attn_crs_ids, String stu_pw_question,
String stu_pw_answer) {
this.stu_id = stu_id;
this.stu_name = stu_name;
this.stu_password = stu_password;
this.department = department;
this.major = major;
this.stu_sex = stu_sex;
this.stu_per_sig = stu_per_sig;
this.stu_head_img = stu_head_img;
this.stu_regist_time = stu_regist_time;
this.stu_attn_crs_ids = stu_attn_crs_ids;
this.stu_pw_question = stu_pw_question;
this.stu_pw_answer = stu_pw_answer;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
@Column(length=20,nullable=false)
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
@Column(length=20,nullable=false)
public String getStu_password() {
return stu_password;
}
public void setStu_password(String stu_password) {
this.stu_password = stu_password;
}
@Column(length=2,nullable=false)
public String getStu_sex() {
return stu_sex;
}
public void setStu_sex(String stu_sex) {
this.stu_sex = stu_sex;
}
@Column(length=255,nullable=true)
public String getStu_per_sig() {
return stu_per_sig;
}
public void setStu_per_sig(String stu_per_sig) {
this.stu_per_sig = stu_per_sig;
}
@Column(length=255,nullable=false)
public String getStu_head_img() {
return stu_head_img;
}
public void setStu_head_img(String stu_head_img) {
this.stu_head_img = stu_head_img;
}
@Temporal(value=TemporalType.TIMESTAMP)
@Column(nullable=false)
public Date getStu_regist_time() {
return stu_regist_time;
}
public void setStu_regist_time(Date stu_regist_time) {
this.stu_regist_time = stu_regist_time;
}
@Column(length=255,nullable=true)
public String getStu_attn_crs_ids() {
return stu_attn_crs_ids;
}
public void setStu_attn_crs_ids(String stu_attn_crs_ids) {
this.stu_attn_crs_ids = stu_attn_crs_ids;
}
@Column(length=64,nullable=false)
public String getStu_pw_question() {
return stu_pw_question;
}
public void setStu_pw_question(String stu_pw_question) {
this.stu_pw_question = stu_pw_question;
}
@Column(length=64,nullable=false)
public String getStu_pw_answer() {
return stu_pw_answer;
}
public void setStu_pw_answer(String stu_pw_answer) {
this.stu_pw_answer = stu_pw_answer;
}
@ManyToOne()
@JoinColumn(name="stu_dept_id",nullable=false)
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@ManyToOne()
@JoinColumn(name="stu_major_id",nullable=false)
public Major getMajor() {
return major;
}
public void setMajor(Major major) {
this.major = major;
}
}
我写BaseDaoImpl.java
来实施interface
BaseDao.java
调试项目时,我发现te
list
的大小为0。
findByPager
的{{1}}方法。
为什么方法BaseDaoImpl.java
会返回空criteria.list()
?
为什么不是list
List<Student>
?
这让我很困惑。我是一个新人,可以帮助我解决这个问题。
答案 0 :(得分:1)
您的代码问题是这一行:
Criteria criteria=getSession().createCriteria(entityClass);
因为当您使用条件查询时,您正在构建针对特定持久化类的引用的查询,而您的变量entityClass
则不是。
protected Class<T> entityClass;
构建条件查询的正确方法是:
Criteria criteria=getSession().createCriteria(Entity.class);
然后您需要更改代码的那一行。您需要将.class
添加到变量中:
Criteria criteria=getSession().createCriteria(entityClass.class);
我与您分享了有关条件查询的一些链接。
https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Criteria.html
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html
我希望这些信息可以帮到你。
祝你好运。