Spring框架下分页功能的java实现(使用注解)

分页功能

之前写分页功能时,网上的很多都不完整,不容易看明白,解释不清楚。现在自己实现了之歌功能,便详细写写,供大家参考。

注解层

用于自动定位到数据库进行数据的查找。
<关联表>

package cn.itsource.annotation;


import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
/*
 * @Target表示当前注解的适用目标
 * 	ElementType.TYPE 表示适用于类和接口
 *  ElementType.FIELD 表示适用于字段(成员变量)
 *  ElementType.METHOD 表示适用于方法	
 */
@Target(ElementType.TYPE)
/*
 * @Retention表示当前注解对象的生命周期
 * 	RetentionPolicy.SOURCE 生命周期最短,仅在源码中有效
 *  RetentionPolicy.CLASS 生命周期居中, 在源码和编译后的字节码中有效
 *  RetentionPolicy.RUNTIME 生命周期最长,保留到代码运行时
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
	//关联表名称或视图名称
	String value();
}

<关联列>

package cn.itsource.annotation;


import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
	
	//domain实体类属性关联的表或视图的列名称
	String value();
	
	//SQL语句的比较运算符  < <= > >= = != like等,默认值是“=”
	String operation() default "=";

}

以Job对象为例

package cn.itsource.domain;

import java.io.Serializable;
import java.util.Date;

import cn.itsource.annotation.Column;
import cn.itsource.annotation.Table;

@Table("v_job")//select * from xxxx where 1=1
public class Job implements Serializable{
	
	private Long id;
	//职位名称  and jobName like '%+字段值+%'
	@Column(value="jobName",operation="like")
	private String jobName;
	//外键,关联t_address表的主键
	private Long addrId;
	//招聘人数
	private Integer number;
	//薪资待遇
	private Integer salary;
	//职位描述
	private String intro;
	//任职要求
	private String requires;
	//1表示全职  0表示兼职  and jobType = 字段值
	@Column("jobType")
	private Integer jobType;
	//是否启用  1表示启用  0表示禁用
	@Column("isEnabled")
	private Integer isEnabled;
	//发布时间
	private Date publishTime;
	//工作地名
	private String addrName;
	
	//添加一个职位详情静态页面路径字段[后台]
	private String detailsPath;
	//添加一个职位详情静态页面路径字段[前台]
	private String detailsPath2;
	
	
	//薪资范围搜索
	@Column(value="salary",operation=">=")
	private Integer salary1;
	@Column(value="salary",operation="<=")
	private Integer salary2;
	public Integer getSalary1() {
		return salary1;
	}
	public void setSalary1(Integer salary1) {
		this.salary1 = salary1;
	}
	public Integer getSalary2() {
		return salary2;
	}public void setSalary2(Integer salary2) {
		this.salary2 = salary2;
	}
	
	
	
	
	public String getDetailsPath2() {
		return detailsPath2;
	}
	public void setDetailsPath2(String detailsPath2) {
		this.detailsPath2 = detailsPath2;
	}
	public String getDetailsPath() {
		return detailsPath;
	}
	public void setDetailsPath(String detailsPath) {
		this.detailsPath = detailsPath;
	}
	public String getAddrName() {
		return addrName;
	}
	public void setAddrName(String addrName) {
		this.addrName = addrName;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getJobName() {
		return jobName;
	}
	public void setJobName(String jobName) {
		this.jobName = jobName;
	}
	public Long getAddrId() {
		return addrId;
	}
	public void setAddrId(Long addrId) {
		this.addrId = addrId;
	}
	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}
	public Integer getSalary() {
		return salary;
	}
	public void setSalary(Integer salary) {
		this.salary = salary;
	}
	public String getIntro() {
		return intro;
	}
	public void setIntro(String intro) {
		this.intro = intro;
	}
	public String getRequires() {
		return requires;
	}
	public void setRequires(String requires) {
		this.requires = requires;
	}
	public Integer getJobType() {
		return jobType;
	}
	public void setJobType(Integer jobType) {
		this.jobType = jobType;
	}
	public Integer getIsEnabled() {
		return isEnabled;
	}
	public void setIsEnabled(Integer isEnabled) {
		this.isEnabled = isEnabled;
	}
	public Date getPublishTime() {
		return publishTime;
	}
	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}
	@Override
	public String toString() {
		return "Job [id=" + id + ", jobName=" + jobName + ", addrId=" + addrId + ", number=" + number + ", salary="
				+ salary + ", intro=" + intro + ", requires=" + requires + ", jobType=" + jobType + ", isEnabled="
				+ isEnabled + ", publishTime=" + publishTime + ", addrName=" + addrName + ", detailsPath=" + detailsPath
				+ "]";
	}
	

}

封装的Condition

package cn.itsource.utils;

/**
 * 封装查询条件参数
 * @author Administrator
 *
 */
public class Condition {
	
	private String id;
	
	//isEnabled为1表示前台访问,为null表示后台访问
	private Integer isEnabled;

	private Long pageNo = 1L;
	
	private Long pageSize = 5L;
	
	//以后如果遇到还要通过其他条件去查询数据的时候,只需要在这儿添加字段和set、get方法即可
	private String jobName;
	private Integer jobType = -1;
	
	//薪资范围
	private Integer salary1;
	private Integer salary2;

	public Integer getSalary1() {
		return salary1;
	}

	public void setSalary1(Integer salary1) {
		this.salary1 = salary1;
	}

	public Integer getSalary2() {
		return salary2;
	}

	public void setSalary2(Integer salary2) {
		this.salary2 = salary2;
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public Integer getJobType() {
		return jobType < 0 ? null : jobType;
	}

	public void setJobType(Integer jobType) {
		this.jobType = jobType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Integer getIsEnabled() {
		return isEnabled;
	}

	public void setIsEnabled(Integer isEnabled) {
		this.isEnabled = isEnabled;
	}

	public Long getPageNo() {
		return pageNo;
	}

	public void setPageNo(Long pageNo) {
		this.pageNo = pageNo;
	}

	public Long getPageSize() {
		return pageSize;
	}

	public void setPageSize(Long pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public String toString() {
		return "Condition [id=" + id + ", isEnabled=" + isEnabled + ", pageNo=" + pageNo + ", pageSize=" + pageSize
				+ "]";
	}

	
}

封装page工具类

package cn.itsource.utils;

import java.util.List;

public class Page<T> {
	//当前页码
	private Long pageNo;
	//每页显示的数据行数
	private Long pageSize;
	//总页数
	private Long pageCount;
	//总行数
	private Long total;
	//当前页要显示的数据集合
	private List<T> list;
	//上一页
	private Long prevPage;
	//下一页
	private Long nextPage;
	public Page() {}
	public Page(Long pageNo, Long pageSize, Long total, List<T> list) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.total = total;
		this.list = list;
		this.pageCount = total % pageSize == 0 ? total/pageSize : total/pageSize + 1;
		this.prevPage = this.pageNo == 1 ? 1 : this.pageNo - 1;
		this.nextPage = this.pageNo == this.pageCount ? this.pageCount : this.pageNo + 1;
	}
	public Long getPageNo() {
		return pageNo;
	}
	public void setPageNo(Long pageNo) {
		this.pageNo = pageNo;
	}
	public Long getPageSize() {
		return pageSize;
	}
	public void setPageSize(Long pageSize) {
		this.pageSize = pageSize;
	}
	public Long getPageCount() {
		return pageCount;
	}
	public void setPageCount(Long pageCount) {
		this.pageCount = pageCount;
	}
	public Long getTotal() {
		return total;
	}
	public void setTotal(Long total) {
		this.total = total;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public Long getPrevPage() {
		return prevPage;
	}
	public void setPrevPage(Long prevPage) {
		this.prevPage = prevPage;
	}
	public Long getNextPage() {
		return nextPage;
	}
	public void setNextPage(Long nextPage) {
		this.nextPage = nextPage;
	}
	@Override
	public String toString() {
		return "Page [pageNo=" + pageNo + ", pageSize=" + pageSize + ", pageCount=" + pageCount + ", total=" + total
				+ ", list=" + list + "]";
	}
	

}

前端分页功能

<!--分页-->
			<nav class="navbar-right">
				<ul class="pagination" id="paging">
					<li>
						<span>当前第${page.pageNo}页</span>
					</li>
					<li><a href="${basePath}show/index?pageNo=1&pageSize=${page.pageSize}"><span aria-hidden="true">首页</span></a></li>
					<li><a href="${basePath}show/index?pageNo=${page.prevPage}&pageSize=${page.pageSize}" aria-label="上一页"><span aria-hidden="true">上一页</span></a></li>
					<li><a href="${basePath}show/index?pageNo=${page.nextPage}&pageSize=${page.pageSize}" aria-label="下一页"><span aria-hidden="true">下一页</span></a></li>
					<li><a href="${basePath}show/index?pageNo=${page.pageCount}&pageSize=${page.pageSize}" aria-label="尾页"><span aria-hidden="true">尾页</span></a></li>
					<li>
						<span>总页数:共${page.pageCount}页</span>
						<span>总数据:共${page.total}条</span>
					</li>
				</ul>
			</nav>
		</div>

后端分页接口

这里condition是封装了数据对象,传参进去使用,带有page的数据。

package cn.itsource.ibase;

import java.util.List;

import cn.itsource.utils.Condition;

public interface IBaseDao<T> extends IBase<T> {

	/**
	 * 分页查询多个对象信息
	 * @param con  封装的查询条件
	 * @return
	 */
	List<T> loadEntitys(Condition con);
	
	/**
	 * 分页查询-总行数
	 * @param con	封装的查询条件
	 * @return
	 */
	Long loadCount(Condition con);
	
}

service层接口

package cn.itsource.ibase;



import cn.itsource.utils.Condition;
import cn.itsource.utils.Page;

public interface IBaseService<T> extends IBase<T> {

	/**
	 * 分页查询数据 		
	 * @param con	封装的查询条件
	 * @return
	 */
	Page<T> loadEntitys(Condition con);
	
	
	
	
}

dao层接口实现

package cn.itsource.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.itsource.dao.IJobDao;
import cn.itsource.domain.Job;
import cn.itsource.utils.Condition;
import cn.itsource.utils.SqlGenerator;

@Repository
public class JobDaoImpl implements IJobDao {

	@Autowired
	private JdbcTemplate jt;
	
	@Override
	public List<Job> loadEntitys(Condition con) {
		String sql = SqlGenerator.generate(con, Job.class, true);
		System.out.println(sql);
		return jt.query(sql, new BeanPropertyRowMapper(Job.class), (con.getPageNo()-1)*con.getPageSize(), con.getPageSize());
	}

	@Override
	public Long loadCount(Condition con) {
		String sql = SqlGenerator.generate(con, Job.class, false);
		System.out.println(sql);
		return jt.queryForLong(sql);
	}

}

service层接口实现

package cn.itsource.service.impl;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.itsource.dao.IJobDao;
import cn.itsource.domain.Job;
import cn.itsource.service.IJobService;
import cn.itsource.utils.Condition;
import cn.itsource.utils.EhcacheUtil;
import cn.itsource.utils.FreeMarkerUtils;
import cn.itsource.utils.Page;
import net.sf.ehcache.CacheManager;

@Service
public class JobServiceImpl implements IJobService {

	@Autowired
	private IJobDao jobDao;
	
	@Override
	public Page<Job> loadEntitys(Condition con) {
		//List<Job> list = jobDao.loadEntitys(con);
		//Long total = jobDao.loadCount(con);
		Long total = (Long)EhcacheUtil.getCacheData(cm, "jobCache", "totalOfJob:"+con.toString(), jobDao, "loadCount", con);
		List<Job> list = (List<Job>)EhcacheUtil.getCacheData(cm, "jobCache", "pageOfJob:"+con.toString(), jobDao, "loadEntitys", con);
		return new Page<Job>(con.getPageNo(), con.getPageSize(), total, list);
	}
	
}

Controller层调用

package cn.itsource.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itsource.domain.Carousel;
import cn.itsource.domain.Job;
import cn.itsource.service.ICarouselService;
import cn.itsource.service.IJobService;
import cn.itsource.utils.Condition;
import cn.itsource.utils.Page;

@Controller
@RequestMapping("/show")
public class ShowController {
	
	@Autowired
	private ICarouselService carouselService;
	@Autowired
	private IJobService jobService;
	
	
	@RequestMapping("/{path}")
	public String index(@PathVariable("path")String path, Model model, Condition con){
			con.setPageSize(10L);
			Page<Job> page = jobService.loadEntitys(con);
			//保存
			model.addAttribute("page", page);
			//保存Condition对象,为了在搜索表单中回填搜索条件【方便查看】
			model.addAttribute("con", con);
		//因为springMVC.xml中配置了视图解析器,所以会跳转到/WEB-INF/admin/之下的jsp页面中,所以不能直接return
		return "forward:/" + path + ".jsp"; // /index.jsp
	}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值