项目源码
This commit is contained in:
50
ksafepack-system/src/main/java/com/kelp/base/BaseEntity.java
Normal file
50
ksafepack-system/src/main/java/com/kelp/base/BaseEntity.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 基类
|
||||
*/
|
||||
package com.kelp.base;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public class BaseEntity implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@Column(name="id")
|
||||
private Long id = null;
|
||||
|
||||
@Column(updatable = false)
|
||||
private Long createTime = System.currentTimeMillis();
|
||||
|
||||
private Long updateTime = System.currentTimeMillis();
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
public void setCreateTime(Long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public Long getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
public void setUpdateTime(Long updateTime) {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
|
||||
}
|
||||
115
ksafepack-system/src/main/java/com/kelp/base/Page.java
Normal file
115
ksafepack-system/src/main/java/com/kelp/base/Page.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.kelp.base;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Page<T> {
|
||||
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
private int total;
|
||||
|
||||
/**
|
||||
* 总页数
|
||||
*/
|
||||
private int totalPage;
|
||||
|
||||
/**
|
||||
* 记录开始索引
|
||||
*/
|
||||
private int start;
|
||||
|
||||
/**
|
||||
* pageIndex
|
||||
*/
|
||||
private int pageIndex;
|
||||
|
||||
/**
|
||||
* 每页有多少数据
|
||||
*/
|
||||
private int limit = 10;
|
||||
|
||||
/**
|
||||
* 返回的结果数据
|
||||
*/
|
||||
private List<T> data = new ArrayList<T>();
|
||||
|
||||
/**
|
||||
* 数据状态,默认为200
|
||||
*/
|
||||
private String status = "200";
|
||||
|
||||
public int getTotal() {
|
||||
return total;
|
||||
}
|
||||
public void setTotal(int total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public int getTotalPage() {
|
||||
return totalPage;
|
||||
}
|
||||
public void setTotalPage(int totalPage) {
|
||||
this.totalPage = totalPage;
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
public void setStart(int start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public int getPageIndex() {
|
||||
return pageIndex;
|
||||
}
|
||||
public void setPageIndex(int pageIndex) {
|
||||
this.pageIndex = pageIndex;
|
||||
}
|
||||
|
||||
public int getLimit() {
|
||||
return limit;
|
||||
}
|
||||
public void setLimit(int limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
public List<T> getData() {
|
||||
return data;
|
||||
}
|
||||
public void setData(List<T> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Page [total=" + total + ", totalPage=" + totalPage + ", start=" + start + ", pageIndex=" + pageIndex
|
||||
+ ", limit=" + limit + ", data=" + data + ", status=" + status + "]";
|
||||
}
|
||||
|
||||
public boolean hasPrevious(){
|
||||
return start > 0;
|
||||
}
|
||||
|
||||
|
||||
public boolean hasNext(){
|
||||
return pageIndex < totalPage;
|
||||
}
|
||||
|
||||
public int current(){
|
||||
return pageIndex;
|
||||
}
|
||||
|
||||
public int getPages(){
|
||||
return totalPage;
|
||||
}
|
||||
}
|
||||
238
ksafepack-system/src/main/java/com/kelp/base/dao/BaseDao.java
Normal file
238
ksafepack-system/src/main/java/com/kelp/base/dao/BaseDao.java
Normal file
@@ -0,0 +1,238 @@
|
||||
package com.kelp.base.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.kelp.base.Page;
|
||||
|
||||
/**
|
||||
* Dao基类接口
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
* @param <T> 泛型,比如我们对实体类Course进行数据库操作,则在子类中传入Course,见CourseDaoBean代码,T必须实现持久化接口
|
||||
* @param <PK> 泛型,主键,可以是任意类型
|
||||
*/
|
||||
public interface BaseDao<T extends Serializable, PK> {
|
||||
|
||||
/**
|
||||
* 保存实体对象.
|
||||
*
|
||||
* @param entity 对象
|
||||
* @return ID
|
||||
*/
|
||||
public boolean add(T entity);
|
||||
|
||||
/**
|
||||
* 添加实体对象集合.
|
||||
*
|
||||
* @return 实体对象集合
|
||||
*/
|
||||
public boolean adds(List<T> ts);
|
||||
|
||||
/**
|
||||
* 更新实体对象.
|
||||
*
|
||||
* @param entity 对象
|
||||
*/
|
||||
public boolean update(T entity);
|
||||
|
||||
/**
|
||||
* 更新多个对象
|
||||
*
|
||||
* @param ts
|
||||
*/
|
||||
public boolean updates(List<T> ts);
|
||||
|
||||
/**
|
||||
* 删除实体对象.
|
||||
*
|
||||
* @param entity 对象
|
||||
* @return
|
||||
*/
|
||||
public boolean delete(T entity);
|
||||
|
||||
/**
|
||||
* 根据ID删除实体对象.
|
||||
*
|
||||
* @param id 记录ID
|
||||
*/
|
||||
public boolean delete(PK id);
|
||||
|
||||
/**
|
||||
* 根据ID数组删除实体对象.
|
||||
*
|
||||
* @param ids ID数组
|
||||
*/
|
||||
public boolean delete(List<PK> ids);
|
||||
|
||||
/**
|
||||
* 根据ID获取实体对象.
|
||||
*
|
||||
* @param id 记录ID
|
||||
* @return 实体对象
|
||||
*/
|
||||
public T get(PK id);
|
||||
|
||||
/**
|
||||
* 根据ID获取实体对象,有懒加载.
|
||||
*
|
||||
* @param id 记录ID
|
||||
* @return 实体对象
|
||||
*/
|
||||
public T load(PK id);
|
||||
|
||||
/**
|
||||
* 根据ID数组获取实体对象集合.
|
||||
*
|
||||
* @param ids ID对象数组
|
||||
*
|
||||
* @return 实体对象集合
|
||||
*/
|
||||
public List<T> get(List<PK> ids);
|
||||
|
||||
/**
|
||||
* 根据属性名和属性值获取实体对象.
|
||||
*
|
||||
* @param propertyName 属性名称
|
||||
* @param value 属性值
|
||||
* @return 实体对象
|
||||
*/
|
||||
public T get(String propertyName, Object value);
|
||||
|
||||
/**
|
||||
* 根据属性名和属性值获取实体对象集合.
|
||||
*
|
||||
* @param propertyName 属性名称
|
||||
* @param value 属性值
|
||||
* @return 实体对象集合
|
||||
*/
|
||||
public List<T> getList(String propertyName, Object value);
|
||||
|
||||
/**
|
||||
* 获取所有实体对象集合.
|
||||
*
|
||||
* @return 实体对象集合
|
||||
*/
|
||||
public List<T> getAll();
|
||||
|
||||
/**
|
||||
* 按某个字段排序
|
||||
*
|
||||
* @param propertyName
|
||||
* @param orderType
|
||||
* @return
|
||||
*/
|
||||
public List<T> getAll(String propertyName, String orderType);
|
||||
|
||||
/**
|
||||
* 获取所有实体对象总数.
|
||||
*
|
||||
* @return 实体对象总数
|
||||
*/
|
||||
public Long getTotalCount();
|
||||
|
||||
/**
|
||||
* 获取所有实体对象数量
|
||||
*
|
||||
* @param propertyName
|
||||
* @param propertyValue
|
||||
* @return
|
||||
*/
|
||||
public Long getTotalCount(String propertyName, String propertyValue);
|
||||
|
||||
/**
|
||||
* 根据属性名、修改前后属性值判断在数据库中是否唯一(若新修改的值与原来值相等则直接返回true).
|
||||
*
|
||||
* @param propertyName 属性名称
|
||||
* @param oldValue 修改前的属性值
|
||||
* @param oldValue 修改后的属性值
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isUnique(String propertyName, Object oldValue, Object newValue);
|
||||
|
||||
/**
|
||||
* 根据属性名判断数据是否已存在.
|
||||
*
|
||||
* @param propertyName 属性名称
|
||||
* @param value 值
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExist(String propertyName, Object value);
|
||||
|
||||
/**
|
||||
* 刷新session.
|
||||
*
|
||||
*/
|
||||
public void flush();
|
||||
|
||||
/**
|
||||
* 清除Session.
|
||||
*
|
||||
*/
|
||||
public void clear();
|
||||
|
||||
/**
|
||||
* 查找符合条件的记录数
|
||||
*
|
||||
* @param sql
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public int getCount(String sql, List<Object> parameters);
|
||||
|
||||
/**
|
||||
* 查找符合条件的记录数
|
||||
*
|
||||
* @param sql
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public int getNativeCount(String sql, List<Object> parameters);
|
||||
|
||||
/**
|
||||
* 执行原生SQL得到符合条件的记录
|
||||
*
|
||||
* @param sql
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public List<T> getListByNativeSql(String sql, List<Object> parameters);
|
||||
|
||||
/**
|
||||
* 查找符合条件的记录
|
||||
*
|
||||
* @param sql
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public List<T> getList(String sql, List<Object> parameters);
|
||||
|
||||
public List<T> getList(String sql, List<Object> parameters, int firstResult, int maxResult);
|
||||
|
||||
/**
|
||||
* 分页取得符合条件的记录
|
||||
*
|
||||
* @param pageNumber
|
||||
* @param pageSize
|
||||
* @param sql
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public Page<T> getBeanPage(int pageNumber, int pageSize, String sql, List<Object> parameters);
|
||||
|
||||
public Page<T> getBeanPage(int pageNumber, int pageSize, String columns,String sql, List<Object> parameters);
|
||||
|
||||
/**
|
||||
* 分页取得符合条件的记录
|
||||
*
|
||||
* @param pageNumber
|
||||
* @param pageSize
|
||||
* @param sql
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public Page<T> getNativeBeanPage(int pageNumber, int pageSize, String classz, String sql,
|
||||
List<Object> parameters);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,464 @@
|
||||
package com.kelp.base.dao.impl;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.hibernate.query.internal.NativeQueryImpl;
|
||||
import org.hibernate.transform.Transformers;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.kelp.base.Page;
|
||||
import com.kelp.base.dao.BaseDao;
|
||||
|
||||
/**
|
||||
* 基类Dao的实现,是抽象类
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
* @param <T>
|
||||
* @param <PK>
|
||||
*/
|
||||
public abstract class BaseDaoImpl<T extends Serializable, PK> implements BaseDao<T, PK> {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(BaseDaoImpl.class);
|
||||
|
||||
//注意,这里unitName是Factory的别名
|
||||
@PersistenceContext(unitName = "entityManagerFactory")
|
||||
protected EntityManager em;
|
||||
|
||||
private Class<T> entityClass;
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
public BaseDaoImpl() {
|
||||
this.entityClass = null;
|
||||
Type type = getClass().getGenericSuperclass();
|
||||
if (type instanceof ParameterizedType) {
|
||||
Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
|
||||
this.entityClass = (Class<T>) parameterizedType[0];
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public boolean add(T t) {
|
||||
try {
|
||||
em.persist(t);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public boolean adds(List<T> ts) {
|
||||
try {
|
||||
for (T t : ts) {
|
||||
if (null != t) {
|
||||
em.persist(t);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public boolean update(T entity) {
|
||||
try {
|
||||
em.merge(entity);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public boolean updates(List<T> ts) {
|
||||
try {
|
||||
for (T t : ts) {
|
||||
if (null != ts) {
|
||||
em.merge(t);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public boolean delete(T entity) {
|
||||
try {
|
||||
em.remove(entity);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public boolean delete(PK id) {
|
||||
try {
|
||||
T entity = load(id);
|
||||
em.remove(entity);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public boolean delete(List<PK> ids) {
|
||||
try {
|
||||
for (PK id : ids) {
|
||||
T entity = load(id);
|
||||
em.remove(entity);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(PK id) {
|
||||
return (T) em.find(entityClass, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(PK id) {
|
||||
return (T) em.getReference(entityClass, id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> get(List<PK> ids) {
|
||||
String hql = "select o from " + entityClass.getName() + " o where o.id in(:ids)";
|
||||
return em.createQuery(hql).setParameter("ids", ids).getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T get(String propertyName, Object value) {
|
||||
String sql = "select o from " + entityClass.getName() + " o where o." + propertyName + " = :" + propertyName;
|
||||
try {
|
||||
return (T) em.createQuery(sql).setParameter(propertyName, value).getSingleResult();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> getList(String propertyName, Object value) {
|
||||
String sql = "select o from " + entityClass.getName() + " o where o." + propertyName + " = :" + propertyName;
|
||||
return em.createQuery(sql).setParameter(propertyName, value).getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> getAll() {
|
||||
String sql = "select o from " + entityClass.getName() + " o";
|
||||
return em.createQuery(sql).getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> getAll(String propertyName, String orderType) {
|
||||
String sql = "select o from " + entityClass.getName() + " o order by " + propertyName + " " + orderType;
|
||||
return em.createQuery(sql).getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getTotalCount() {
|
||||
String sql = "select count(*) from " + entityClass.getName();
|
||||
return (Long) em.createQuery(sql).getSingleResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getTotalCount(String propertyName, String propertyValue) {
|
||||
String sql = "select count(*) from " + entityClass.getName() + " where " + propertyName + " = :"
|
||||
+ propertyName;
|
||||
return (Long) em.createQuery(sql).setParameter(propertyName, propertyValue).getSingleResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUnique(String propertyName, Object oldValue, Object newValue) {
|
||||
if (newValue == oldValue || newValue.equals(oldValue)) {
|
||||
return true;
|
||||
}
|
||||
T object = get(propertyName, newValue);
|
||||
return (object == null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExist(String propertyName, Object value) {
|
||||
T object = get(propertyName, value);
|
||||
return (object != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
em.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
em.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount(String sql, List<Object> parameters) {
|
||||
|
||||
Query query = em.createQuery(sql);
|
||||
if (parameters != null && parameters.size() > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return Integer.parseInt(query.getSingleResult().toString());
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNativeCount(String sql, List<Object> parameters) {
|
||||
|
||||
Query query = em.createNativeQuery(sql);
|
||||
if (parameters != null && parameters.size() > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return Integer.parseInt(query.getSingleResult().toString());
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> getListByNativeSql(String sql, List<Object> parameters) {
|
||||
Query query = em.createNativeQuery(sql, entityClass);
|
||||
if (parameters != null && parameters.size() > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> getList(String sql, List<Object> parameters) {
|
||||
Query query = em.createQuery(sql);
|
||||
if (parameters != null && parameters.size() > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> getList(String sql, List<Object> parameters, int firstResult, int maxResult) {
|
||||
Query query = em.createQuery(sql);
|
||||
if (parameters != null && parameters.size() > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
query.setFirstResult(firstResult);
|
||||
query.setMaxResults(maxResult);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Map<String, Object>> getMapList(String sql, Object... parameters) {
|
||||
Query query = em.createNativeQuery(sql);
|
||||
if (parameters != null && parameters.length > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
return query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<T> getBeanList(String sql, List<Object> parameters, int firstResult, int maxResult) {
|
||||
Query query = em.createQuery(sql);
|
||||
if (parameters != null && parameters.size() > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
query.setFirstResult(firstResult);
|
||||
query.setMaxResults(maxResult);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<T> getNativeBeanList(String sql, List<Object> parameters, int firstResult, int maxResult) {
|
||||
Query query = em.createNativeQuery(sql);
|
||||
if (parameters != null && parameters.size() > 0) {
|
||||
int i = 1;
|
||||
for (Object parameter : parameters) {
|
||||
query.setParameter(i++, parameter);
|
||||
}
|
||||
}
|
||||
query.setFirstResult(firstResult);
|
||||
query.setMaxResults(maxResult);
|
||||
return query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(entityClass)).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> getBeanPage(int pageNumber, int pageSize, String sql, List<Object> params) {
|
||||
Page<T> page = new Page<T>();
|
||||
// 总条数
|
||||
int count = this.getCount("select count(1) " + sql, params);
|
||||
if (count == 0) {
|
||||
return page;
|
||||
}
|
||||
|
||||
// 总页数
|
||||
int pageCount = count / pageSize + ((count % pageSize) == 0 ? 0 : 1);
|
||||
if (pageNumber < 1) {
|
||||
pageNumber = 1;
|
||||
}
|
||||
|
||||
// 页码
|
||||
if (pageNumber > pageCount) {
|
||||
pageNumber = pageCount;
|
||||
}
|
||||
|
||||
// 第一条记录索引
|
||||
int firstResult = pageSize * (pageNumber - 1);
|
||||
// 最后一条记录索引
|
||||
int maxResult = Math.min(pageSize, count - firstResult);
|
||||
|
||||
// 生成PageForm
|
||||
List<T> listBaseBean = this.getBeanList("select o " + sql, params, firstResult, maxResult);
|
||||
|
||||
page.setLimit(pageSize);
|
||||
page.setTotal(count);
|
||||
page.setPageIndex(pageNumber);
|
||||
page.setData(listBaseBean);
|
||||
page.setTotalPage(pageCount);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> getBeanPage(int pageNumber, int pageSize,String columns, String sql, List<Object> params) {
|
||||
|
||||
// 总条数
|
||||
int count = this.getCount("select count(1) " + sql, params);
|
||||
if (count == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 总页数
|
||||
int pageCount = count / pageSize + ((count % pageSize) == 0 ? 0 : 1);
|
||||
if (pageNumber < 1) {
|
||||
pageNumber = 1;
|
||||
}
|
||||
|
||||
// 页码
|
||||
if (pageNumber > pageCount) {
|
||||
pageNumber = pageCount;
|
||||
}
|
||||
|
||||
// 第一条记录索引
|
||||
int firstResult = pageSize * (pageNumber - 1);
|
||||
// 最后一条记录索引
|
||||
int maxResult = Math.min(pageSize, count - firstResult);
|
||||
|
||||
// 生成PageForm
|
||||
List<T> listBaseBean = this.getBeanList("select " + columns + " " + sql, params, firstResult, maxResult);
|
||||
Page<T> page = new Page<T>();
|
||||
page.setLimit(pageSize);
|
||||
page.setTotal(count);
|
||||
page.setPageIndex(pageNumber);
|
||||
page.setData(listBaseBean);
|
||||
page.setTotalPage(pageCount);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> getNativeBeanPage(int pageNumber, int pageSize, String columns, String sqlTail,
|
||||
List<Object> params) {
|
||||
|
||||
// 总条数
|
||||
int count = this.getNativeCount("select count(1) " + sqlTail, params);
|
||||
if (count == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 总页数
|
||||
int pageCount = count / pageSize + ((count % pageSize) == 0 ? 0 : 1);
|
||||
if (pageNumber < 1) {
|
||||
pageNumber = 1;
|
||||
}
|
||||
|
||||
// 页码
|
||||
if (pageNumber > pageCount) {
|
||||
pageNumber = pageCount;
|
||||
}
|
||||
|
||||
// 第一条记录索引
|
||||
int firstResult = pageSize * (pageNumber - 1);
|
||||
// 最后一条记录索引
|
||||
int maxResult = Math.min(pageSize, count - firstResult);
|
||||
|
||||
// 生成PageForm
|
||||
List<T> listBaseBean = this.getNativeBeanList("select " + columns + sqlTail, params, firstResult, maxResult);
|
||||
Page<T> page = new Page<T>();
|
||||
page.setLimit(pageSize);
|
||||
page.setTotal(count);
|
||||
page.setPageIndex(pageNumber);
|
||||
page.setData(listBaseBean);
|
||||
page.setTotalPage(pageCount);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user