1.用到了两个类,一个pojo:maps.Maps,一个dao:maps.MapsDAO
2.主要起作用的核心代码两行:
queryObject.setFirstResult(pageSize*pageNumber);
queryObject.setMaxResults(pageSize);
3.全部代码:
package maps;
/**
* Maps entity.
*
* @author MyEclipse Persistence Tools
*/
public class Maps implements java.io.Serializable {
// Fields
private Long mapId;
private Building building;
private Floor floor;
private Region region;
private MapType mapType;
private UnitInfo unitInfo;
private Room room;
private SubRegion subRegion;
private String mapName;
private String mapAddr;
private Integer mapLayer;
private String mapBak;
// Constructors
/** default constructor */
public Maps() {
}
/** minimal constructor */
public Maps(String mapName, String mapAddr, Integer mapLayer) {
this.mapName = mapName;
this.mapAddr = mapAddr;
this.mapLayer = mapLayer;
}
/** full constructor */
public Maps(Building building, Floor floor, Region region, MapType mapType,
UnitInfo unitInfo, Room room, SubRegion subRegion, String mapName,
String mapAddr, Integer mapLayer, String mapBak) {
this.building = building;
this.floor = floor;
this.region = region;
this.mapType = mapType;
this.unitInfo = unitInfo;
this.room = room;
this.subRegion = subRegion;
this.mapName = mapName;
this.mapAddr = mapAddr;
this.mapLayer = mapLayer;
this.mapBak = mapBak;
}
// Property accessors
public Long getMapId() {
return this.mapId;
}
public void setMapId(Long mapId) {
this.mapId = mapId;
}
public Building getBuilding() {
return this.building;
}
public void setBuilding(Building building) {
this.building = building;
}
public Floor getFloor() {
return this.floor;
}
public void setFloor(Floor floor) {
this.floor = floor;
}
public Region getRegion() {
return this.region;
}
public void setRegion(Region region) {
this.region = region;
}
public MapType getMapType() {
return this.mapType;
}
public void setMapType(MapType mapType) {
this.mapType = mapType;
}
public UnitInfo getUnitInfo() {
return this.unitInfo;
}
public void setUnitInfo(UnitInfo unitInfo) {
this.unitInfo = unitInfo;
}
public Room getRoom() {
return this.room;
}
public void setRoom(Room room) {
this.room = room;
}
public SubRegion getSubRegion() {
return this.subRegion;
}
public void setSubRegion(SubRegion subRegion) {
this.subRegion = subRegion;
}
public String getMapName() {
return this.mapName;
}
public void setMapName(String mapName) {
this.mapName = mapName;
}
public String getMapAddr() {
return this.mapAddr;
}
public void setMapAddr(String mapAddr) {
this.mapAddr = mapAddr;
}
public Integer getMapLayer() {
return this.mapLayer;
}
public void setMapLayer(Integer mapLayer) {
this.mapLayer = mapLayer;
}
public String getMapBak() {
return this.mapBak;
}
public void setMapBak(String mapBak) {
this.mapBak = mapBak;
}
}
/******************************************************************************************************/
package maps;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
/**
* A data access object (DAO) providing persistence and search support for Maps
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*
* @see maps.Maps
* @author MyEclipse Persistence Tools
*/
public class MapsDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(MapsDAO.class);
// property constants
public static final String MAP_NAME = "mapName";
public static final String MAP_ADDR = "mapAddr";
public static final String MAP_LAYER = "mapLayer";
public static final String MAP_BAK = "mapBak";
public void save(Maps transientInstance) {
log.debug("saving Maps instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Maps persistentInstance) {
log.debug("deleting Maps instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Maps findById(java.lang.Long id) {
log.debug("getting Maps instance with id: " + id);
try {
Maps instance = (Maps) getSession().get("maps.Maps", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Maps instance) {
log.debug("finding Maps instance by example");
try {
List results = getSession().createCriteria("maps.Maps").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Maps instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Maps as model where model."
+ propertyName + "= ?"+"order by mapType";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByMapNameUseLike(String name){
log.debug("finding Maps instance with property: " + MAP_NAME
+ ", value: " + name);
try {
String queryString = "from Maps as model where model."
+ MAP_NAME + " LIKE ? "+"order by mapType";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, "%"+name+"%");
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findAllListByPage(int pageSize,int pageNumber){
log.debug("finding Maps instance with pageSize and pageNumber");
try{
String queryString="from Maps order by id";
Query queryObject=getSession().createQuery(queryString);
queryObject.setFirstResult(pageSize*pageNumber);
queryObject.setMaxResults(pageSize);
return queryObject.list();
}catch(RuntimeException re){
log.error("find all failed");
throw re;
}
}
public List findByMapName(Object mapName) {
return findByProperty(MAP_NAME, mapName);
}
public List findByMapAddr(Object mapAddr) {
return findByProperty(MAP_ADDR, mapAddr);
}
public List findByMapLayer(Object mapLayer) {
return findByProperty(MAP_LAYER, mapLayer);
}
public List findByMapBak(Object mapBak) {
return findByProperty(MAP_BAK, mapBak);
}
public List findAll() {
log.debug("finding all Maps instances");
try {
String queryString = "from Maps";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Maps merge(Maps detachedInstance) {
log.debug("merging Maps instance");
try {
Maps result = (Maps) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Maps instance) {
log.debug("attaching dirty Maps instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Maps instance) {
log.debug("attaching clean Maps instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
/*************************************************************************************************************/
package test;
import maps.*;
/**
* @说明 本方法用hibernate实现分页
* @author 蒋明原
* @see maps.MapsDAO
* @see maps.Maps
* @version 1.0
*/
public class MapsFindAllByPage {
/**
* @param pageSize
* 页面数据大小
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MapsDAO dao = new MapsDAO();
int pageSize = 3;
System.out.println("sizeOf dao'list: " + dao.findAll().size());
for (int i = 0; i < ((dao.findAll().size()
.findAll().size()
/ pageSize : (dao.findAll().size() / pageSize) + 1)); i++) {
java.util.List
list = dao.findAllListByPage(pageSize, i);
java.util.Iterator
it = list.iterator();
while (it.hasNext()) {
Maps map = (Maps) it.next();
System.out.println(map.getMapId() + " " + map.getMapName());
}
System.out.println("________________________________");
}
}
}
/***********************************************************************************************************/