首页 > 代码库 > struts2操作数据库

struts2操作数据库

struts2操作数据库是初学者的一个难点也是一个重点,现在我为大家讲解一下struts2操作数据库,使用struts2对数据库进行增、删、改、查和分页查询,请看下面的代码:

User类


public class UserAction extends ActionSupport{
    List<User> userList;
    UserDao userDao=new UserDao();
    User user;
    User2 user2;
public User2 getUser2() {
return user2;
}








public void setUser2(User2 user2) {
this.user2 = user2;
}








public User getUser() {

return user;
}








public void setUser(User user) {
this.user = user;
}








public int getFirstRecord() {
return firstRecord;
}








public void setFirstRecord(int firstRecord) {
this.firstRecord = firstRecord;
}


private  int currentPage =1;

private int Records ;

private int  pageSize =5;

private  int pages ;

int firstRecord;



public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getRecords() {
return Records;
}
public void setRecords(int records) {
Records = records;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
    public void pages(){
    Records=userDao.getAllCount();
 
 
  pages = Records/pageSize+((Records%pageSize==0)?0:1);
  firstRecord =(currentPage-1)*pageSize;
    }
public String listAll() throws Exception {


        this.pages();
userList =userDao.doList(firstRecord, pageSize);
return SUCCESS;
}
  public String addUser(){
userDao.addUser(user);
return SUCCESS;
}
    public String deleteUser(){
    userDao.deleteUser(user);
    return SUCCESS;
    }
    public String updateUser(){
    userDao.updateUser(user);
    return SUCCESS;
    }
    public String getInfo(){   
   
    user=userDao.getInfo(user.uid);
   
    return SUCCESS;
    }

数据库连接自己写就可以了,之后再action里配置一下就可以运行了,如果需要源代码可以到http://blog.csdn.net/u014626784资源中下载。