基于javaweb的web资源库项目——后台资源管理demo
Posted stormzhuo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于javaweb的web资源库项目——后台资源管理demo相关的知识,希望对你有一定的参考价值。
前面完成了后台的用户管理,而资源管理的实现的原理和用户管理是差不多的,这里就直接给出资源管理的代码
表的设计
资源管理需要两个表,一个是管理用户资源的表resource,另一个是管理资源类别的表category
显然,管理用户资源的表需要两个外键,其中一个外键resource_uid对应用户账号user_id,另一个对应资源类别category_id
这样当删除用户时,用户所拥有的资源也会被删除,同理,当删除资源类别时,属于这种资源类别的用户资源也会被删除
管理用户资源表resource如下
字段名称 | 类型 | 约束 | 描述 |
---|---|---|---|
resource_id | int(10) | NOT NULL AUTO_INCREMENT | 资源编号 |
resource_cid | int(10) | NOT NULL FOREIGN KEY (resource_cid ) REFERENCES category (category_id ) ON DELETE CASCADE ON UPDATE CASCADE | 外键对应资源类别编号category |
resource_uid | varchar(20) | NOT NULL FOREIGN KEY (resource_uid ) REFERENCES user (user_id ) ON DELETE CASCADE ON UPDATE CASCADE | 外键对应用户账号user_id |
resource_name | varchar(20) | NOT NULL | 资源名称 |
resource_image | varchar(500) | DEFAULT NULL | 图片路径 |
resource_url | varchar(250) | NOT NULL | 资源下载地址路径 |
resource_desc | varchar(250) | DEFAULT NULL | 资源描述 |
resource_level | varchar(10) | NOT NULL DEFAULT ‘初级’ | 资源等级,默认为初级 |
resource_time | date | NOT NULL | 资源上传时间 |
resource_good | int(10) | NOT NULL DEFAULT ‘0’ | 是否为优秀资源,默认为一般资源 |
资源类别表category如下
字段名称 | 类型 | 约束 | 描述 |
---|---|---|---|
category_id | int(10) | NOT NULL AUTO_INCREMENT | 资源类别编号 |
category_name | varchar(20) | NOT NULL | 资源类别名称 |
category_desc | varchar(250) | DEFAULT NULL | 资源类别描述 |
表的创建
resource表的创建
在mysql命令行使用如下sql语句创建resource表
USE web_resource;
DROP TABLE IF EXISTS `resource`;
CREATE TABLE `resource` (
`resource_id` int(10) NOT NULL AUTO_INCREMENT,
`resource_cid` int(10) NOT NULL,
`resource_uid` varchar(20) NOT NULL,
`resource_name` varchar(20) NOT NULL,
`resource_image` varchar(500) DEFAULT NULL,
`resource_url` varchar(250) NOT NULL,
`resource_desc` varchar(250) DEFAULT NULL,
`resource_level` varchar(10) NOT NULL DEFAULT '初级',
`resource_time` date NOT NULL,
`resource_good` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`resource_id`),
KEY `resource_cid` (`resource_cid`),
KEY `resource_uid` (`resource_uid`),
CONSTRAINT `resource_ibfk_1` FOREIGN KEY (`resource_cid`) REFERENCES `category` (`category_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `resource_ibfk_2` FOREIGN KEY (`resource_uid`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
category表的创建
在mysql命令行使用如下sql语句创建category表
USE web_resource;
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
`category_id` int(10) NOT NULL AUTO_INCREMENT,
`category_name` varchar(20) NOT NULL,
`category_desc` varchar(250) DEFAULT NULL,
PRIMARY KEY (`category_id`,`category_name`),
KEY `category_id` (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
创建实体类
在entity包下,创建用户资源实体resource,如下
package com.zhuo.entity;
public class Resource {
private int resourceId;
private int resourceCid;
private String resourceUid;
private String resourceName;
private String resourceImage;
private String resourceUrl;
private String resourceDesc;
private String resourceLevel;
private String resourceTime;
private int resourceGood;
public Resource() {
super();
}
public Resource(int resourceId, int resourceCid, String resourceUid, String resourceName, String resourceImage,
String resourceUrl, String resourceDesc, String resourceLevel, String resourceTime, int resourceGood) {
super();
this.resourceId = resourceId;
this.resourceCid = resourceCid;
this.resourceUid = resourceUid;
this.resourceName = resourceName;
this.resourceImage = resourceImage;
this.resourceUrl = resourceUrl;
this.resourceDesc = resourceDesc;
this.resourceLevel = resourceLevel;
this.resourceTime = resourceTime;
this.resourceGood = resourceGood;
}
public int getResourceId() {
return resourceId;
}
public void setResourceId(int resourceId) {
this.resourceId = resourceId;
}
public int getResourceCid() {
return resourceCid;
}
public void setResourceCid(int resourceCid) {
this.resourceCid = resourceCid;
}
public String getResourceUid() {
return resourceUid;
}
public void setResourceUid(String resourceUid) {
this.resourceUid = resourceUid;
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
public String getResourceImage() {
return resourceImage;
}
public void setResourceImage(String resourceImage) {
this.resourceImage = resourceImage;
}
public String getResourceUrl() {
return resourceUrl;
}
public void setResourceUrl(String resourceUrl) {
this.resourceUrl = resourceUrl;
}
public String getResourceDesc() {
return resourceDesc;
}
public void setResourceDesc(String resourceDesc) {
this.resourceDesc = resourceDesc;
}
public String getResourceLevel() {
return resourceLevel;
}
public void setResourceLevel(String resourceLevel) {
this.resourceLevel = resourceLevel;
}
public String getResourceTime() {
return resourceTime;
}
public void setResourceTime(String resourceTime) {
this.resourceTime = resourceTime;
}
public int getResourceGood() {
return resourceGood;
}
public void setResourceGood(int resourceGood) {
this.resourceGood = resourceGood;
}
@Override
public String toString() {
return "Resource [resourceId=" + resourceId + ", resourceCid=" + resourceCid + ", resourceUid=" + resourceUid
+ ", resourceName=" + resourceName + ", resourceImage=" + resourceImage + ", resourceUrl=" + resourceUrl
+ ", resourceDesc=" + resourceDesc + ", resourceLevel=" + resourceLevel + ", resourceTime="
+ resourceTime + ", resourceGood=" + resourceGood + "]";
}
}
在entity包下,创建资源类别实体category,如下
package com.zhuo.entity;
public class Category {
private int categoryId;
private String categoryName;
private String categoryDesc;
public Category() {
super();
}
public Category(int categoryId, String categoryName, String categoryDesc) {
super();
this.categoryId = categoryId;
this.categoryName = categoryName;
this.categoryDesc = categoryDesc;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getCategoryDesc() {
return categoryDesc;
}
public void setCategoryDesc(String categoryDesc) {
this.categoryDesc = categoryDesc;
}
@Override
public String toString() {
return "Category [categoryId=" + categoryId + ", categoryName=" + categoryName + ", categoryDesc="
+ categoryDesc + "]";
}
}
资源管理实现
获取所有用户资源信息并分页
后端实现
在controller层中创建resource包,并在resource包下创建DoResourceSelect.java,代码如下
package com.zhuo.controller.resource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhuo.entity.Category;
import com.zhuo.entity.Resource;
import com.zhuo.entity.User;
import com.zhuo.service.impl.ResourceServiceImpl;
@WebServlet("/manage/admin_do_resource_select")
public class DoResourceSelect extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 当前页
int currentPage = 1;
// 每页显示条数
int count = 5;
// array[0]=资源总数,array[1]=总页数,后面会被覆盖
int array[] = { 0, 0 };
// 获取用户指定的页面
String cp = request.getParameter("currentPage");
System.out.println("cp = " + cp);
// 接收用户搜索的关键字
String keyword = request.getParameter("keywords");
/* 用户如果有指定当前页,则把字符串转化为int型 */
if (cp != null) {
currentPage = Integer.parseInt(cp);
}
// 创建资源service层接口的实现类
ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
/*
* 调用业务逻辑方法获取资源记录总数并算出总页数 即(array[0]=资源总数,array[1]=总页数(资源总数/每页显示条数)
*/
try {
array = resourceServiceImpl.getResourcePageTotal(count, keyword);
} catch (SQLException e) {
e.printStackTrace();
}
// 调用业务逻辑方法获取所有资源信息
ArrayList<Resource> listResource = null;
try {
listResource = resourceServiceImpl.getAllResourceInfo(currentPage, count, keyword);
} catch (SQLException e) {
e.printStackTrace();
}
// 调用业务逻辑方法获取所有含有资源的用户信息
ArrayList<User> listUser = null;
try {
listUser = resourceServiceImpl.getAllHasResourceUser(listResource);
} catch (SQLException e) {
e.printStackTrace();
}
/* 遍历所有资源,遍历过程中利用资源id
* 调用业务逻辑方法获取资源类别信息,并添加到list集合中 */
ArrayList<Category> listCategory = new ArrayList<Category>();
for (int i = 0; i < listResource.size(); i++) {
Category category = new Category();
try {
category = resourceServiceImpl.getAllResourceCategoryInfo(listResource.get(i).getResourceCid());
} catch (SQLException e) {
e.printStackTrace();
}
listCategory.add(category);
}
// 放到请求对象域里
request.setAttribute("resourceList", listResource);
request.setAttribute("userList", listUser);
request.setAttribute("categoryList", listCategory);
request.setAttribute("totalStudent", array[0]);
request.setAttribute("totalPage", array[1]);
request.setAttribute("currentPage", currentPage);
request.getRequestDispatcher("/views/after/manage/admin_resource.jsp").forward(request, response);
}
}
在service层中创建ResourceService接口,并在接口中添加如下业务逻辑抽象方法,如下
// 获取所有资源记录总数及页数
public int[] getResourcePageTotal(int count, String keyword) throws SQLException;
// 获取所有资源信息
public ArrayList<Resource> getAllResourceInfo(int currentPage, int count, String keyword) throws SQLException;
// 获取所有含有资源的用户信息
public ArrayList<User> getAllHasResourceUser(ArrayList<Resource> listResource) throws SQLException;
// 获取所有资源的类别信息
public Category getAllResourceCategoryInfo(int categoryId) throws SQLException;
在service层中创建接口的实现类ResourceServiceImpl.java,并在实现类中重写接口方法,如下
// 创建资源dao层接口的实现类
ResourceDao resourceDao = new ResourceDaoImpl();
@Override
public int[] getResourcePageTotal(int count, String keyword) throws SQLException {
// 检索所有资源总数并算出总页数
int[] array = resourceDao.selectResourceTotal(count, keyword);
return array;
}
@Override
public ArrayList<Resource> getAllResourceInfo(int currentPage, int count, String keyword) throws SQLException {
// 检索所有资源信息
ArrayList<Resource> listResource = resourceDao.selectAllResource(currentPage, count, keyword);
return listResource;
}
@Override
public ArrayList<User> getAllHasResourceUser(ArrayList<Resource> listResource) throws SQLException {
// 检索含有资源的所有用户信息
ArrayList<User> listUser = resourceDao.基于javaweb的web资源库项目——后台资源管理demo