当前位置:  编程语言>java/jsp

struts+spring+hibernate+jquery实现分页功能的几个基本类介绍(异步加载)

 
    发布时间:2014-1-12  


    本文导语: struts+spring+hibernate+jquery实现分页功能的几个基本类介绍,具体实现如下:功能实现:完整分页功能,点击“加载更多”,将下一页的数据加载出来,页面不刷新。用户entity类:package com.test.model;import java.io.Serializable;import javax.persist...

struts+spring+hibernate+jquery实现分页功能的几个基本介绍,具体实现如下:

功能实现:完整分页功能,点击“加载更多”,将下一页的数据加载出来,页面不刷新

用户entity类:

package com.test.model;

import java.io.serializable;

import javax.persistence.column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="t_user",catalog="scott")
public class User implements Serializable{

private int user_id;

private String user_name;

private String user_sex;

private String user_class;

public User(){

}

public User(String user_name,String user_sex,String user_class){
this.user_name = user_name;
this.user_sex=user_sex;
this.user_class=user_class;
}

@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="user_seq")
@SequenceGenerator(sequenceName="user_seq",name="user_seq")
public int getUser_id() {
return user_id;
}

public void setUser_id(int user_id) {
this.user_id = user_id;
}

@Column(name="user_name",length=24)
public String getUser_name() {
return user_name;
}

public void setUser_name(String user_name) {
this.user_name = user_name;
}

@Column(name="user_sex",length=4)
public String getUser_sex() {
return user_sex;
}

public void setUser_sex(String user_sex) {
this.user_sex = user_sex;
}

@Column(name="user_class",length=24)
public String getUser_class() {
return user_class;
}

public void setUser_class(String user_class) {
this.user_class = user_class;
}




}


接口类:

package com.test.user.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.test.model.User;

@Service
public interface UserService {

/*******
* 分页加载
* @param pagesize
* @param pagenow
* @return
* @throws 数据库 iis7站长之家
*/
public List getAllUserByPage(int pagesize,int pagenow) throws Exception;

/*********
* 获取用户总记录数
* @return
*/
public int getAllUserCount();

/*************
* 创建用户
*
* @param user
* @throws Exception
*/
public void createUser(User user) throws Exception;

}


实现上面接口

package com.test.user.service.impl;

import java.util.List;

import javax.annotation.resource;

import org.springframework.stereotype.Component;

import com.test.model.User;
import com.test.user.dao.UserDao;
import com.test.user.service.UserService;

@Component
public class UserServiceImpl implements UserService {

@Resource
private UserDao userDao;

/*******
* 分页加载
* @param pagesize
* @param pagenow
* @return
* @throws Exception
*/
public List getAllUserByPage(int pagesize,int pagenow) throws Exception{
return this.userDao.getAllUserByPage(pagesize, pagenow);
}

/*********
* 获取用户总记录数
* @return
*/
public int getAllUserCount(){
return this.userDao.getAllUserCount();
}

@Override
public void createUser(User user) throws Exception {
this.userDao.createUser(user);

}


}


Dao底层操作类:

package com.test.user.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.test.model.User;

@Repository
public class UserDao extends HibernateDaoSupport {

private JdbcTemplate jdbcTemplate;

@Resource
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Resource
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}


/*******
* 分页加载
* @param pagesize
* @param pagenow
* @return
* @throws Exception
*/
public List getAllUserByPage(int pagesize,int pagenow) throws Exception{
Query q=this.getSession().createQuery("from User");
q.setFirstResult((pagenow-1)*pagesize);
q.setMaxResults(pagesize);
List li=q.list();
return li;
}

/*********
* 获取用户总记录数
* @return
*/
public int getAllUserCount(){
int count=0;
Query q=this.getSession().createQuery("select count(*) from User");
count=Integer.parseInt(q.uniqueResult().toString());
return count;
}

public void createUser(User user) throws Exception {
this.getHibernateTemplate().save(user);

}


public List getAllUser() throws Exception {

// return this.getHibernateTemplate().find("from User");

// 用jdbcTemplate查询
List list = new ArrayList();
SqlRowSet rs = this.jdbcTemplate
.queryForRowSet("select user_id,user_name,user_sex,user_class from t_user");
while (rs.next()) {
User user = new User();
user.setUser_id(rs.getInt("user_id"));
user.setUser_name(rs.getString("user_name"));
user.setUser_sex(rs.getString("user_sex"));
user.setUser_class(rs.getString("user_class"));
list.add(user);
}
return list;
}


}


控制器Action类:

package com.test.user.action;

import java.io.printwriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.jsonarray;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.actionsupport;
import com.test.model.User;
import com.test.user.service.UserService;

@Controller
public class UserAction extends ActionSupport {

@Resource
private UserService userService;

private List userList = new ArrayList();

public List getUserList() {
return userList;
}

public void setUserList(List userList) {
this.userList = userList;
}

/************************分页**************************/
private int pagesize;//每页显示多少条
private int currentPage;//当前页

public int getPagesize() {
return pagesize;
}

public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

/*************
* 初始化加载用户数据
* @return
*/
public String getAllUserInit(){
try {
this.pagesize=7;//先加载 7 条数据
this.currentPage=1;//初始时设置当前页为1
this.userList=this.userService.getAllUserByPage(this.pagesize, this.currentPage);
} catch (Exception e) {
e.printStackTrace();
}
return "all";
}

/***********
* 分页-异步加载
* @return
*/
public String getAllUserByPage(){
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html");
response.setcharacterencoding("utf-8");
//response.setheader("cache-control", "no-cache");
try {
this.pagesize=7;//每次加载7条数据
system.out.println("当前页"+this.currentpage);
this.userList=this.userService.getAllUserByPage(this.pagesize, this.currentPage);
System.out.println("加载数据:"+this.userList.size());
PrintWriter out = response.getWriter();
JSONArray json=JSONArray.fromObject(this.userList);
out.print(json);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


}

相关文章推荐:


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3