springboot集成hibernate
package com.jxd.Boot.hibernate.dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Repository;
import com.jxd.Boot.hibernate.dao.TeacherDao;
import com.jxd.Boot.po.Teacher;
import com.jxd.Boot.util.Pager;
import com.jxd.Boot.util.RenderPager;
@SuppressWarnings("unchecked")
@Repository
public class TeacherDaoImpl implements TeacherDao {
@PersistenceContext
private EntityManager entityManager;
/*
* (非 Javadoc)自定义sql查询 封装VO Description:
*
* @see com.jxd.Boot.hibernate.dao.TeacherDao#listall()
*/
@Override
public Pager listall(PageRequest page) {
// TODO Auto-generated method stub
Query query = entityManager.createNativeQuery("select * from teacher ");
Query query1 = entityManager.createNativeQuery("select count(*) from teacher ");
List<Teacher> list = query.unwrap(SQLQuery.class).setResultTransformer(Transformers.aliasToBean(Teacher.class))
.setMaxResults(page.getPageSize()).setFirstResult(page.getPageSize() * (page.getPageNumber() - 1))
.list();
Integer total = Integer.valueOf(query1.getSingleResult().toString());
Pager pager = RenderPager.jdbcconvertPager(list, page, total);
return pager;
}
}