⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台资源管理demo1

Posted stormzhuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台资源管理demo1相关的知识,希望对你有一定的参考价值。

前面完成了后台的用户管理,而资源管理的实现的原理和用户管理是差不多的,资源管理又分为用户资源管理和资源类别管理,这一篇主要介绍用户资源管理的实现

表的设计

资源管理需要两个表,一个是管理用户资源的表resource,另一个是管理资源类别的表category

显然,管理用户资源的表需要两个外键,其中一个外键resource_uid对应用户账号user_id,另一个对应资源类别category_id

这样当删除用户时,用户所拥有的资源也会被删除,同理,当删除资源类别时,属于这种资源类别的用户资源也会被删除

管理用户资源表resource如下

字段名称类型约束描述
resource_idint(10)NOT NULL AUTO_INCREMENT资源编号
resource_cidint(10)NOT NULL FOREIGN KEY (resource_cid) REFERENCES category (category_id) ON DELETE CASCADE ON UPDATE CASCADE外键对应资源类别编号category
resource_uidvarchar(20)NOT NULL FOREIGN KEY (resource_uid) REFERENCES user (user_id) ON DELETE CASCADE ON UPDATE CASCADE外键对应用户账号user_id
resource_namevarchar(20)NOT NULL资源名称
resource_imagevarchar(500)DEFAULT NULL图片路径
resource_urlvarchar(250)NOT NULL资源下载地址路径
resource_descvarchar(250)DEFAULT NULL资源描述
resource_levelvarchar(10)NOT NULL DEFAULT ‘初级’资源等级,默认为初级
resource_timedatetimeNOT NULL资源上传时间
resource_goodint(10)NOT NULL DEFAULT ‘0’是否为优秀资源,默认为一般资源

资源类别表category如下

字段名称类型约束描述
category_idint(10)NOT NULL AUTO_INCREMENT资源类别编号
category_namevarchar(20)NOT NULL资源类别名称
category_descvarchar(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` datetime 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,代码如下

@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");
		// 接收用户搜索的关键字
		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.getAllResource(currentPage, count, keyword);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		/*
		 * 遍历所有资源,遍历过程中利用资源id 调用业务逻辑方法获取资源用户信息,并添加到list集合中
		 */
		ArrayList<User> listUser = new ArrayList<User>();
		for (int i = 0; i < listResource.size(); i++) {
			User user = new User();
			try {
				user = resourceServiceImpl.getResourceUser(listResource.get(i).getResourceUid());
			} catch (SQLException e) {
				e.printStackTrace();
			}
			listUser.add(user);
		}
		/*
		 * 遍历所有资源,遍历过程中利用资源id 调用业务逻辑方法获取资源类别信息,并添加到list集合中
		 */
		ArrayList<Category> listCategory = new ArrayList<Category>();
		for (int i = 0; i < listResource.size(); i++) {
			Category category = new Category();
			try {
				category = resourceServiceImpl.getResourceCategory(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);
		/* 若用户是通过搜索的,把关键词放在请求域中 */
		if (keyword != null) {
			request.setAttribute("searchParams", "&keywords=" + keyword);
		}
		// 请求转发到资源管理页面
		request.getRequestDispatcher("/WEB-INF/manage/admin_resource.jsp").forward(request, response);
	}
}

在service层中创建ResourceService接口,并在接口中添加如下业务逻辑抽象方法,如下

	// 获取所有资源记录总数及页数
	public int[] getResourcePageTotal(int count, String keyword) throws SQLException;
	
	// 获取所有资源信息
	public ArrayList<Resource> getAllResource(int currentPage, int count, String keyword) throws SQLException;
	
	// 获取资源的用户信息
	public User getResourceUser(String resourceUid) throws SQLException;
	
	// 获取资源的类别信息
	public Category getResourceCategory(int categoryId) throws SQLException;

在service层中创建接口的实现类ResourceServiceImpl.java,并在实现类中重写接口方法,如下

	/* 创建资源dao层接口的实现类,并赋给接口对象变量
	 * 实现上转型,也就面向接口编程,而不关心它的实现类是谁 */
	ResourceDao resourceDao = new ResourceDaoImpl();
	/*
	 * 创建用户dao层接口的实现类,并赋给接口对象变量。
	 *  实现上转型,也就面向接口编程,而不关心它的实现类是谁
	 */
	UserDao userDao = new UserDaoImpl();
	@Override
	public int[] getResourcePageTotal(int count, String keyword) throws SQLException {
		// 检索所有资源总数并算出总页数
		int[] array = resourceDao.selectResourceTotal(count, keyword);
		return array;
	}
	
	@Override
	public ArrayList<Resource> getAllResource(int currentPage, int count, String keyword) throws SQLException {
		// 检索所有资源信息
		ArrayList<Resource> listResource = resourceDao.selectAllResource(currentPage, count, keyword);
		return listResource;
		
	}
	
	@Override
	public User getResourceUser(String resourceUid) throws SQLException {
		// 检索用户信息
		User user = userDao.selectUser(resourceUid, "");
		return user;
	}
	
	@Override
	public Category getResourceCategory(int catagotyId) throws SQLException{
		// 检索资源的资源类别
		Category category = resourceDao.selectResourceCategory(catagotyId);
		return category;
	}

这里需要说一下获取资源用户信息的业务功能,它调用了检索用户信息dao层方法。

在前面的用户管理中已经在dao层的UserDao编写了检索用户的方法,在这里就可以直接用了,而不需要重复在dao层的ResourceDao编写检索用户信息的方法了

但是呢,UserDaoImpl是需要一个身份标识的参数的,这里只需传入空值

以上是关于⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台资源管理demo1的主要内容,如果未能解决你的问题,请参考以下文章

基于内容与基于协作的过滤?

为啥 Kafka 是基于拉的而不是基于推的?

推荐算法简介:基于用户的协同过滤基于物品的协同过滤基于内容的推荐

依存句法分析:基于图的依存句法分析基于转移的依存句法分析基于神经网络的依存句法分析

基于项目和基于内容的协同过滤有啥区别?

事件处理:基于功能的组件与基于类的组件