如何使用ssm实现基于Java的微博网站的设计与实现+vue

136 篇文章 0 订阅
82 篇文章 0 订阅

@TOC

ssm483基于Java的微博网站的设计与实现+vue

第1章 绪论

1.1 课题背景

二十一世纪互联网的出现,改变了几千年以来人们的生活,不仅仅是生活物资的丰富,还有精神层次的丰富。时代进步的标志,就是让人们过上更好的生活。在互联网诞生之前,地域位置往往是人们思想上不可跨域的鸿沟,信息的传播速度极慢,信息处理的速度和要求还是通过人们骑马或者是信鸽传递,这些信息传递都是不可控制的,中间很有可能发生丢失,信息的传递水平决定了人们生活的水平。现如今,大家都在用互联网来实现自己的目的,从内部管理设置计算机管理,提高内部信息管理水平,从外部市场也可以用计算机获取相关数据进行处理,如今各行各业已经严重依赖于计算机了。

本课题研究和开发微博网站,让安装在计算机上的该系统变成管理人员的小帮手,提高用户分享与交流信息处理速度,规范用户分享与交流信息处理流程,让管理人员的产出效益更高。

1.2 课题意义

最火的新浪微博的蒸蒸日上,以及腾讯微博的停运,标志着微博这个行业需要有一个新颖的展现。本课题研发的微博网站,就是提供用户分享与交流信息处理的解决方案,它可以短时间处理完信息,并且只需要使用者动动鼠标和键盘就能获取自己需要的信息,并且这些信息都有专门的存储设备,而且数据的备份和迁移都可以设定为无人值守,从人力角度和信息处理角度以及信息安全角度,微博网站是完胜传统纸质操作的,2022年的“新浪微博”便是完胜的证明。

1.3 研究内容

本文对微博网站的设计与实现分成六个章节进行描述。

第1章:研究微博网站的背景,以及开发微博网站的意义。

第2章:对开发微博网站的环境还有技术进行说明。

第3章:分析微博网站的可行性,性能,流程以及功能。

第4章:设计微博网站的功能结构,设计数据库E-R图以及对数据表的存储结构进行设计。

第5章:实现微博网站的功能并进行功能界面展示。

第6章:对系统测试进行阐述,以及对本系统部分功能进行检测。

第2章 开发环境与技术

本章节对开发微博网站需要搭建的开发环境,还有微博网站开发中使用的编程技术等进行阐述。

2.1 MYSQL数据库

MySQL数据库是关系型数据库的一种,也是传统的行式数据模式,获取一些数据是先一行一行的获取,然后一行一行的显示,与最近大数据兴起的列式数据库有着明显的不同。行式数据库主要是处理最重要的数据逻辑部分,并且必须是有效数据,这样每一处的数据关联都是不可损坏,如果对数据安全性比较高的肯定是需要选择MySQL数据库,列式数据库的发明仅仅是因为读取效率高,与传统的MySQL数据库比起来在数据写入方面并不会高明到哪里。MySQL虽然比起oracle或者SQL SERVER来讲,安装包只是几十兆甚至几百兆,有点小,但是功能并不会弱到哪里,严格遵循SQL标准语法。MySQL的数据存放形式从大向小的说是数据库最大,然后是表,每个表里面存放数据是有一定的规则的,数据存放是表格形式的,也就是说有横也有竖,横着的为行,一般表示一条数据,每个表都有字段,而字段是以列的形式存在,这样能保证一条数据每一个字段对应的是相同数据类型的数据。表与表之间还可以进行关联,进行分表操作,如果一条数据相关项目属性太多,那么可以把有效的相关联系做成关联,可以设定是否唯一。

2.2 IDEA开发工具

IDEA是捷克共和国的Java程序员开发人员创造的一个开发软件,刚开始主要是对于用Eclipse软件他们用得不顺手,所以直接开发了这款软件。之所以不顺手原因在于没有代码提升功能,原因是Eclipse只是把代码提示作为一种插件形式的存在,如果有些程序开发人员不清楚代码提示插件可能会出问题,并且代码提示只是用来作为插件,所以功能上有所欠缺。IDEA不仅仅代码提示做的很好,在代码重构上面更上如虎添翼,程序开发人员可以选择一段代码然后IDEA就会对代码进行分解重构,有效的把代码弄得更够层次感,复用性更高,用着更简洁和方便,大大的减少了代码工作量,提升了代码开发效率。当然,IDEA对于使用者这么好,肯定也是有目的的,原因在于插件越多越友好,就需要花费大量的金钱来使用,所以说IDEA使用主要是看自己喜好。

2.3 SSM框架

开发一个业务逻辑比较简单的应用,采用这几年最广为流传的SSM框架是很合适的。SSM框架就是Spring MVC框架和Spring框架,以及持久层常用的MyBatis框架。

三个框架有三个不同的作用。普通用户一般都是操作浏览器进行浏览自己喜欢的内容,也可以通过浏览器提交自己输入的信息,而显示或者提交,都会被Spring MVC框架进行拦截和处理,进入到更深的一个层次就是控制层,根据不同的提交内容可以访问不同的处理逻辑,让不同的代码进行执行,如果是普通的比较逻辑不需要从数据库里获取内容是不需要传递给MyBatis框架的,如果需要与数据库内容进行交互,就会从Java的POJO对象通过MyBatis自动转换数据库对应字段的数据类型,具体是该增删改查还是其他操作,都会通过MyBatis进行处理,处理结果是反馈给控制层,然后通过控制层再提交给视图层,反馈到用户希望看到的结果。

第3章 系统分析

我在和夏老师确定了研究的课题之后,便开始从图书馆下载文献阅读,并了解同类型的网站具备的大致功能,然后与本系统用户的实际需求结合进行分析,得出本系统要研究的具体功能与性能。虽然分析系统这一阶段性工作主要是确定功能,但它却影响着后面系统开发环节的进展,它也是系统开发流程中比较重要的一个环节。

3.1 可行性分析

以下部分是从三个角度来进行可行性分析,确保开发成功的前提是有可行性分析,只有进行提前分析,符合程序开发流程才不至于开发过程的中断。

3.1.1 技术可行性

在技术实现层次,分析了好几种技术实现方法,并且都有对应的成功案例,也有很多开源模块可以进行参考,所以从技术可行性分析来讲,实现微博网站是没有问题的。

3.1.2 经济可行性

对于身为学生的我而言,在经济资源上面可支配资源很少,为了开发微博网站,从技术分析发现可以用自己用的电脑进行开发。最重要的是技术资源一般都是开源免费使用的,因此得出结论,经济方面是具有可行性的。

3.1.3 操作可行性

微博网站的具体实现,本身参考人类的普遍操作逻辑,把常用的操作习惯当做主要的导航实现,可以让使用者更快速的理解并且上手操作,实现符合逻辑的操作流程是操作可行性的具体体现。

3.2 系统流程

微博网站投入使用后,其各个功能的内部操作逻辑需要使用者通过流程图来进行了解。

3.2.1 操作流程

使用者在操作微博网站中,应该按照本系统提供的操作流程(图3.1即为本系统的操作流程图)进行操作,这样可以减少使用者操作中出现的错误,从而节省进入微博网站的时间。

图3.1 系统操作流程

3.2.2 登录流程

微博网站通过登录功能(图3.2即为其登录的流程)引导使用者进入指定的功能操作区,也避免非本系统的用户享受本系统提供的服务以及查看本系统提供的信息,从而保障本系统用户的安全使用。

图3.2 登录流程

3.2.3 删除信息流程

微博网站在经过长期使用后,会产生很多的数据信息。为了腾出存储空间存放更多的数据,本系统数据库中存储的数据,一些没有参考价值的数据需要进行删除(图3.3即为删除信息的流程),删除数据过程中,为避免误删,使用者要根据系统的提示来决定是否删除数据。

图3.3 删除信息流程

3.2.4 添加信息流程

微博网站提供可视化的功能操作区,非常方便使用者进行数据操作,当使用者往系统中录入数据时(图3.4即为添加信息的流程),本系统也会进行数据合法性的判断,符合要求的数据才能够在数据库指定表中进行登记。

图3.4 添加信息流程

3.3 性能需求

在需求分析中就应该对项目所需服务器性能进行分析,这样才符合正常的分析流程。只谈功能需求不谈性能需求是一件很严重的事情,可能会导致使用过程中出现一系列不可预测的问题,所以性能需求也是需要考虑的重要项。

下面就是从几个方面来进行系统的性能分析,从每个角度来分析系统性能。

(1)系统数据的容量:从数据角度来分析,每个表和每个数据库,达到的数据量到一定的程度,是否需要分表或者是分库,超过了数据的设定限度,可能会导致数据反映迟钝,容错量增加。

(2)数据精度的要求:需要对需求分析里面数据设定环节,考虑相应的数据精度问题,需要发现数据是常用的精度还是非常用的精度,进而设定不同的数值。数据的精度问题,会直接导致设计的性能问题。

(3)时间响应要求:从用户提交操作,到页面反映,中间有个数据处理的问题,如果数据量大,那么考虑索引问题和分库问题,数据量再大就要考虑增加列式数据库的问题,这些都要根据数据量的增加以及逻辑的严密性来进行判断,才能符合用户的要求,毕竟响应时间太久操作起来令人不适。

系统的性能需求从业务需求之初就能大致了解到性能需求相关的概念,再从系统性能需求来逐条实现,可以让设计的系统有使用价值。

3.4 功能需求

微博网站根据使用权限的角度进行功能分析,并运用用例图来展示各个权限需要操作的功能。

图3.5即为管理员用例图,管理员权限操作的功能包括对用户信息,对文章,公告等信息的管理。

图3.5 管理员用例图

图3.6即为用户用例图,用户权限操作的功能包括查看文章,收藏文章,对文章留言,发布微博正文,评论微博正文,查看公告等。

图3.6 用户用例图

第4章 系统设计

系统的设计一切都是为了用户的使用,虽然用户使用过程中可能只是面对着浏览器进行各种操作,但是不代表着系统对于用户在浏览器上的操作不进行处理,所以说,设计一个系统需要考虑到方方面面。

4.1 功能结构设计

图4.1即为设计的管理员功能结构,管理员权限操作的功能包括对用户信息,对文章,微博正文,公告等信息的管理。

图4.1 管理员功能结构

图4.2即为设计的用户功能结构,用户权限操作的功能包括查看文章,收藏文章,对文章留言,发布微博正文,评论微博正文,查看公告等。

图4.2 用户功能结构

4.2 数据库设计

微博网站运行中产生的数据需要按照提前设置的存储规则进行保存,而这个存储规则则是在数据库的设计中进行设置的。通常情况下,为了更好的配合系统运行,也要给用户带来良好的使用体验,设计一个很好的数据库是必须的,因为它能减少用户的等待时间,还可以对系统的请求在最短时间内进行响应。所以,对数据库设计时,需要花费一定的时间来分析系统对于数据存储的要求以及存储的具体数据,然后设计具体的存储规则,保证数据库能够对系统的各种数据请求进行及时回应,缩短数据处理时间,并在一定程度上降低数据冗余,节省存储空间。

4.2.1 数据库概念设计

(1)图4.4即为文章留言这个实体所拥有的属性值。

图4.4 文章留言实体属性图

(2)图4.5即为管理员这个实体所拥有的属性值。

图4.5 管理员实体属性图

(3)图4.6即为文章这个实体所拥有的属性值。

图4.6 文章实体属性图

  1. 图4.7即为用户这个实体所拥有的属性值。

图4.7 用户实体属性图

  1. 图4.8即为上面介绍的实体中存在的联系。

图4.8 实体间关系E-R图

4.2.2 数据库物理设计

本小节主要任务即是根据上述内容进行数据存储结构的设计,也就是在数据库中设计存放本系统的数据的数据表,设计数据表时,需要对各个字段进行确定,通常来说,一个实体与一张数据表相对应,实体的属性就用来表示字段名称,不同的字段表示的数据类型以及取值都不相同,这里需要根据系统实际数据的情况进行设置,同时也需要在具体表中确定该表的主键,以及该表各个字段是否能够保持空等进行说明,设计完成一张数据表的结构之后,在保存时同样要命名,尽量选择英文名称进行命名并保存,方便今后系统对数据表进行数据存储访问时,在提高数据存储效率的同时,还不容易导致系统出错。接下来就对设计的数据表进行展示。

表4.1 论坛表

字段注释类型
id (主键)主键int(11)
forum_name微博正文标题varchar(200)
yonghu_id用户int(11)
laoshi_id老师int(11)
users_id管理员int(11)
forum_content发布内容text
super_ids父idint(11)
forum_types微博正文类型int(11)
forum_state_types微博正文状态int(11)
insert_time发帖时间timestamp
update_time修改时间timestamp
create_time创建时间timestamp

表4.2 公告信息表

字段注释类型
id (主键)主键int(11)
gonggao_name公告名称varchar(200)
gonggao_photo公告图片varchar(200)
gonggao_types公告类型int(11)
insert_time公告发布时间timestamp
gonggao_content公告详情text
create_time创建时间timestamp

表4.3 管理员表

字段注释类型
id (主键)主键bigint(20)
username用户名varchar(100)
password密码varchar(100)
role角色varchar(100)
addtime新增时间timestamp

表4.4 文章表

字段注释类型
id (主键)主键int(11)
shangjia_id商家int(11)
wenzhang_name文章名称varchar(200)
wenzhang_photo文章照片varchar(200)
wenzhang_types文章类型int(11)
zan_numberint(11)
cai_numberint(11)
shangxia_types是否上架int(11)
wenzhang_delete逻辑删除int(11)
wenzhang_content文章简介text
create_time创建时间timestamp

表4.5 文章收藏表

字段注释类型
id (主键)主键int(11)
wenzhang_id文章int(11)
yonghu_id用户int(11)
wenzhang_collection_types类型int(11)
insert_time收藏时间timestamp
create_time创建时间timestamp

表4.6 文章留言表

字段注释类型
id (主键)主键int(11)
wenzhang_id文章int(11)
yonghu_id用户int(11)
wenzhang_liuyan_text留言内容text
reply_text回复内容text
insert_time留言时间timestamp
update_time回复时间timestamp
create_time创建时间timestamp

表4.7 用户表

字段注释类型
id (主键)主键int(11)
username账户varchar(200)
password密码varchar(200)
yonghu_name用户姓名varchar(200)
yonghu_phone用户手机号varchar(200)
yonghu_id_number用户身份证号varchar(200)
yonghu_photo用户头像varchar(200)
sex_types性别int(11)
wenzhang_types感兴趣类型int(11)
yonghu_email电子邮箱varchar(200)
create_time创建时间timestamp

第5章 系统实现

在本节中,展示部分操作权限的功能与界面。

5.1 管理员功能实现

5.1.1 论坛管理

图5.1 即为编码实现的论坛管理界面,管理员在该界面中对论坛的微博正文内容还有微博正文的回复内容进行查看,可以更改微博正文的内容信息,可以删除微博正文。

图5.1 论坛管理界面

5.1.2 文章管理

图5.2 即为编码实现的文章管理界面,管理员在该界面中下架文章,查看文章的点赞数,可以更改文章信息,能够新增和删除文章。

图5.2 文章管理界面

5.1.3 用户管理

图5.3 即为编码实现的用户管理界面,管理员在该界面具备添加用户,为用户重置密码,删除用户等操作权限。

图5.3 用户管理界面

5.2 用户功能实现

5.2.1 文章信息

图5.4 即为编码实现的文章信息界面,用户在该界面中对文章点赞,对文章内容查看之后发布留言,同时可以收藏文章。

图5.4 文章信息界面

5.2.2 在线论坛

图5.5 即为编码实现的在线论坛界面,用户在该界面中选择微博正文进行其内容的查看,用户查看微博正文的内容时,可以发布微博正文的评论,用户在本界面可以发布微博正文。

图5.5 在线论坛界面

5.2.3 公告信息

图5.6 即为编码实现的公告信息界面,用户在该界面中查询公告,查看公告的图片,标题以及公告的内容信息。

图5.6 公告信息界面

5.2.4 更改个人信息

图5.7 即为编码实现的更改个人信息界面,用户在该界面中可以设置感兴趣的文章类型,便于系统推荐给用户感兴趣的文章,用户可以在本界面更改基础信息,包括照片,邮箱等。

图5.7 更改个人信息界面

MPUtil.java
package com.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.mapper.Wrapper;

/**
 * Mybatis-Plus工具类
 */
public class MPUtil {
	public static final char UNDERLINE = '_';

	
	//mybatis plus allEQ 表达式转换
		public static Map allEQMapPre(Object bean,String pre) {
		   Map<String, Object> map =BeanUtil.beanToMap(bean);
		  return camelToUnderlineMap(map,pre);
	   }

		//mybatis plus allEQ 表达式转换
		public static Map allEQMap(Object bean) {
		   Map<String, Object> map =BeanUtil.beanToMap(bean);
		   return camelToUnderlineMap(map,"");
	   }

		public static Wrapper allLikePre(Wrapper wrapper,Object bean,String pre) {
			   Map<String, Object> map =BeanUtil.beanToMap(bean);
			   Map result = camelToUnderlineMap(map,pre);
			 
			return genLike(wrapper,result);
		}
	
		public static Wrapper allLike(Wrapper wrapper,Object bean) {
			  Map result = BeanUtil.beanToMap(bean, true, true);			 
			return genLike(wrapper,result);
		}
	
	
		public static Wrapper genLike( Wrapper wrapper,Map param) {
			Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
			int i=0;
			while (it.hasNext()) {
				if(i>0) wrapper.and();
				Map.Entry<String, Object> entry = it.next();
				String key = entry.getKey();
				String value = (String) entry.getValue();
				wrapper.like(key, value);
				i++;
			}
			return wrapper;
		}
		
		public static Wrapper likeOrEq(Wrapper wrapper,Object bean) {
			  Map result = BeanUtil.beanToMap(bean, true, true);			 
			return genLikeOrEq(wrapper,result);
		}
		
		public static Wrapper genLikeOrEq( Wrapper wrapper,Map param) {
			Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
			int i=0;
			while (it.hasNext()) {
				if(i>0) wrapper.and();
				Map.Entry<String, Object> entry = it.next();
				String key = entry.getKey();
				if(entry.getValue().toString().contains("%")) {
					wrapper.like(key, entry.getValue().toString().replace("%", ""));
				} else {
					wrapper.eq(key, entry.getValue());
				}
				i++;
			}
			return wrapper;
		}
		
		public static Wrapper allEq(Wrapper wrapper,Object bean) {
			  Map result = BeanUtil.beanToMap(bean, true, true);			 
			return genEq(wrapper,result);
		}
	
	
		public static Wrapper genEq( Wrapper wrapper,Map param) {
			Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
			int i=0;
			while (it.hasNext()) {
				if(i>0) wrapper.and();
				Map.Entry<String, Object> entry = it.next();
				String key = entry.getKey();
				wrapper.eq(key, entry.getValue());
				i++;
			}
			return wrapper;
		}
	
	
		public static Wrapper between(Wrapper wrapper,Map<String, Object> params) {
			for(String key : params.keySet()) {
				String columnName = "";
				if(key.endsWith("_start")) {
					columnName = key.substring(0, key.indexOf("_start"));
					if(StringUtils.isNotBlank(params.get(key).toString())) {
						wrapper.ge(columnName, params.get(key));
					}
				}
				if(key.endsWith("_end")) {
					columnName = key.substring(0, key.indexOf("_end"));
					if(StringUtils.isNotBlank(params.get(key).toString())) {
						wrapper.le(columnName, params.get(key));
					}
				}
			}
			return wrapper;
		}
	
		public static Wrapper sort(Wrapper wrapper,Map<String, Object> params) {
			String order = "";
			if(params.get("order") != null && StringUtils.isNotBlank(params.get("order").toString())) {
				order = params.get("order").toString();
			}
			if(params.get("sort") != null && StringUtils.isNotBlank(params.get("sort").toString())) {
				if(order.equalsIgnoreCase("desc")) {
					wrapper.orderDesc(Arrays.asList(params.get("sort")));
				} else {
					wrapper.orderAsc(Arrays.asList(params.get("sort")));
				}
			}
			return wrapper;
		}
	
	
	/**
	 * 驼峰格式字符串转换为下划线格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static void main(String[] ages) {
		System.out.println(camelToUnderline("ABCddfANM"));
	}
	
	public static Map camelToUnderlineMap(Map param, String pre) {

		Map<String, Object> newMap = new HashMap<String, Object>();
		Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = it.next();
			String key = entry.getKey();
			String newKey = camelToUnderline(key);
			if (pre.endsWith(".")) {
				newMap.put(pre + newKey, entry.getValue());
			} else if (StringUtils.isEmpty(pre)) {
				newMap.put(newKey, entry.getValue());
			} else {

				newMap.put(pre + "." + newKey, entry.getValue());
			}
		}
		return newMap;
	}
}

UsersServiceImpl.java

package com.service.impl;


import java.util.List;
import java.util.Map;

import com.service.UsersService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dao.UsersDao;
import com.entity.UsersEntity;
import com.utils.PageUtils;
import com.utils.Query;


/**
 * 系统用户
 * @author
 */
@Service("userService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, UsersEntity> implements UsersService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<UsersEntity> page = this.selectPage(
                new Query<UsersEntity>(params).getPage(),
                new EntityWrapper<UsersEntity>()
        );
        return new PageUtils(page);
	}

	@Override
	public List<UsersEntity> selectListView(Wrapper<UsersEntity> wrapper) {
		return baseMapper.selectListView(wrapper);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params,
			Wrapper<UsersEntity> wrapper) {
		 Page<UsersEntity> page =new Query<UsersEntity>(params).getPage();
	        page.setRecords(baseMapper.selectListView(page,wrapper));
	    	PageUtils pageUtil = new PageUtils(page);
	    	return pageUtil;
	}
}

YonghuController.java

package com.controller;

import java.io.File;
import java.math.BigDecimal;
import java.net.URL;
import java.text.SimpleDateFormat;
import com.alibaba.fastjson.JSONObject;
import java.util.*;
import org.springframework.beans.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.ContextLoader;
import javax.servlet.ServletContext;
import com.service.TokenService;
import com.utils.*;
import java.lang.reflect.InvocationTargetException;

import com.service.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import com.annotation.IgnoreAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.entity.*;
import com.entity.view.*;
import com.service.*;
import com.utils.PageUtils;
import com.utils.R;
import com.alibaba.fastjson.*;

/**
 * 用户
 * 后端接口
 * @author
 * @email
*/
@RestController
@Controller
@RequestMapping("/yonghu")
public class YonghuController {
    private static final Logger logger = LoggerFactory.getLogger(YonghuController.class);

    @Autowired
    private YonghuService yonghuService;


    @Autowired
    private TokenService tokenService;
    @Autowired
    private DictionaryService dictionaryService;

    //级联表service



    /**
    * 后端列表
    */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, HttpServletRequest request){
        logger.debug("page方法:,,Controller:{},,params:{}",this.getClass().getName(),JSONObject.toJSONString(params));
        String role = String.valueOf(request.getSession().getAttribute("role"));
        if(StringUtil.isEmpty(role))
            return R.error(511,"权限为空");
        else if("用户".equals(role))
            params.put("yonghuId",request.getSession().getAttribute("userId"));
        if(params.get("orderBy")==null || params.get("orderBy")==""){
            params.put("orderBy","id");
        }
        PageUtils page = yonghuService.queryPage(params);

        //字典表数据转换
        List<YonghuView> list =(List<YonghuView>)page.getList();
        for(YonghuView c:list){
            //修改对应字典表字段
            dictionaryService.dictionaryConvert(c, request);
        }
        return R.ok().put("data", page);
    }

    /**
    * 后端详情
    */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id, HttpServletRequest request){
        logger.debug("info方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
        YonghuEntity yonghu = yonghuService.selectById(id);
        if(yonghu !=null){
            //entity转view
            YonghuView view = new YonghuView();
            BeanUtils.copyProperties( yonghu , view );//把实体数据重构到view中

            //修改对应字典表字段
            dictionaryService.dictionaryConvert(view, request);
            return R.ok().put("data", view);
        }else {
            return R.error(511,"查不到数据");
        }

    }

    /**
    * 后端保存
    */
    @RequestMapping("/save")
    public R save(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
        logger.debug("save方法:,,Controller:{},,yonghu:{}",this.getClass().getName(),yonghu.toString());

        String role = String.valueOf(request.getSession().getAttribute("role"));
        if(StringUtil.isEmpty(role))
            return R.error(511,"权限为空");

        Wrapper<YonghuEntity> queryWrapper = new EntityWrapper<YonghuEntity>()
            .eq("username", yonghu.getUsername())
            .or()
            .eq("yonghu_phone", yonghu.getYonghuPhone())
            .or()
            .eq("yonghu_id_number", yonghu.getYonghuIdNumber())
            ;

        logger.info("sql语句:"+queryWrapper.getSqlSegment());
        YonghuEntity yonghuEntity = yonghuService.selectOne(queryWrapper);
        if(yonghuEntity==null){
            yonghu.setCreateTime(new Date());
            yonghu.setPassword("123456");
            yonghuService.insert(yonghu);
            return R.ok();
        }else {
            return R.error(511,"账户或者用户手机号或者用户身份证号已经被使用");
        }
    }

    /**
    * 后端修改
    */
    @RequestMapping("/update")
    public R update(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
        logger.debug("update方法:,,Controller:{},,yonghu:{}",this.getClass().getName(),yonghu.toString());

        String role = String.valueOf(request.getSession().getAttribute("role"));
//        if(StringUtil.isEmpty(role))
//            return R.error(511,"权限为空");
        //根据字段查询是否有相同数据
        Wrapper<YonghuEntity> queryWrapper = new EntityWrapper<YonghuEntity>()
            .notIn("id",yonghu.getId())
            .andNew()
            .eq("username", yonghu.getUsername())
            .or()
            .eq("yonghu_phone", yonghu.getYonghuPhone())
            .or()
            .eq("yonghu_id_number", yonghu.getYonghuIdNumber())
            ;

        logger.info("sql语句:"+queryWrapper.getSqlSegment());
        YonghuEntity yonghuEntity = yonghuService.selectOne(queryWrapper);
        if("".equals(yonghu.getYonghuPhoto()) || "null".equals(yonghu.getYonghuPhoto())){
                yonghu.setYonghuPhoto(null);
        }
        if(yonghuEntity==null){
            //  String role = String.valueOf(request.getSession().getAttribute("role"));
            //  if("".equals(role)){
            //      yonghu.set
            //  }
            yonghuService.updateById(yonghu);//根据id更新
            return R.ok();
        }else {
            return R.error(511,"账户或者用户手机号或者用户身份证号已经被使用");
        }
    }

    /**
    * 删除
    */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        logger.debug("delete:,,Controller:{},,ids:{}",this.getClass().getName(),ids.toString());
        yonghuService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 批量上传
     */
    @RequestMapping("/batchInsert")
    public R save( String fileName){
        logger.debug("batchInsert方法:,,Controller:{},,fileName:{}",this.getClass().getName(),fileName);
        try {
            List<YonghuEntity> yonghuList = new ArrayList<>();//上传的东西
            Map<String, List<String>> seachFields= new HashMap<>();//要查询的字段
            Date date = new Date();
            int lastIndexOf = fileName.lastIndexOf(".");
            if(lastIndexOf == -1){
                return R.error(511,"该文件没有后缀");
            }else{
                String suffix = fileName.substring(lastIndexOf);
                if(!".xls".equals(suffix)){
                    return R.error(511,"只支持后缀为xls的excel文件");
                }else{
                    URL resource = this.getClass().getClassLoader().getResource("static/upload/" + fileName);//获取文件路径
                    File file = new File(resource.getFile());
                    if(!file.exists()){
                        return R.error(511,"找不到上传文件,请联系管理员");
                    }else{
                        List<List<String>> dataList = PoiUtil.poiImport(file.getPath());//读取xls文件
                        dataList.remove(0);//删除第一行,因为第一行是提示
                        for(List<String> data:dataList){
                            //循环
                            YonghuEntity yonghuEntity = new YonghuEntity();
//                            yonghuEntity.setUsername(data.get(0));                    //账户 要改的
//                            //yonghuEntity.setPassword("123456");//密码
//                            yonghuEntity.setYonghuName(data.get(0));                    //用户姓名 要改的
//                            yonghuEntity.setYonghuPhone(data.get(0));                    //用户手机号 要改的
//                            yonghuEntity.setYonghuIdNumber(data.get(0));                    //用户身份证号 要改的
//                            yonghuEntity.setYonghuPhoto("");//照片
//                            yonghuEntity.setSexTypes(Integer.valueOf(data.get(0)));   //性别 要改的
//                            yonghuEntity.setWenzhangTypes(Integer.valueOf(data.get(0)));   //感兴趣类型 要改的
//                            yonghuEntity.setYonghuEmail(data.get(0));                    //电子邮箱 要改的
//                            yonghuEntity.setCreateTime(date);//时间
                            yonghuList.add(yonghuEntity);


                            //把要查询是否重复的字段放入map中
                                //账户
                                if(seachFields.containsKey("username")){
                                    List<String> username = seachFields.get("username");
                                    username.add(data.get(0));//要改的
                                }else{
                                    List<String> username = new ArrayList<>();
                                    username.add(data.get(0));//要改的
                                    seachFields.put("username",username);
                                }
                                //用户手机号
                                if(seachFields.containsKey("yonghuPhone")){
                                    List<String> yonghuPhone = seachFields.get("yonghuPhone");
                                    yonghuPhone.add(data.get(0));//要改的
                                }else{
                                    List<String> yonghuPhone = new ArrayList<>();
                                    yonghuPhone.add(data.get(0));//要改的
                                    seachFields.put("yonghuPhone",yonghuPhone);
                                }
                                //用户身份证号
                                if(seachFields.containsKey("yonghuIdNumber")){
                                    List<String> yonghuIdNumber = seachFields.get("yonghuIdNumber");
                                    yonghuIdNumber.add(data.get(0));//要改的
                                }else{
                                    List<String> yonghuIdNumber = new ArrayList<>();
                                    yonghuIdNumber.add(data.get(0));//要改的
                                    seachFields.put("yonghuIdNumber",yonghuIdNumber);
                                }
                        }

                        //查询是否重复
                         //账户
                        List<YonghuEntity> yonghuEntities_username = yonghuService.selectList(new EntityWrapper<YonghuEntity>().in("username", seachFields.get("username")));
                        if(yonghuEntities_username.size() >0 ){
                            ArrayList<String> repeatFields = new ArrayList<>();
                            for(YonghuEntity s:yonghuEntities_username){
                                repeatFields.add(s.getUsername());
                            }
                            return R.error(511,"数据库的该表中的 [账户] 字段已经存在 存在数据为:"+repeatFields.toString());
                        }
                         //用户手机号
                        List<YonghuEntity> yonghuEntities_yonghuPhone = yonghuService.selectList(new EntityWrapper<YonghuEntity>().in("yonghu_phone", seachFields.get("yonghuPhone")));
                        if(yonghuEntities_yonghuPhone.size() >0 ){
                            ArrayList<String> repeatFields = new ArrayList<>();
                            for(YonghuEntity s:yonghuEntities_yonghuPhone){
                                repeatFields.add(s.getYonghuPhone());
                            }
                            return R.error(511,"数据库的该表中的 [用户手机号] 字段已经存在 存在数据为:"+repeatFields.toString());
                        }
                         //用户身份证号
                        List<YonghuEntity> yonghuEntities_yonghuIdNumber = yonghuService.selectList(new EntityWrapper<YonghuEntity>().in("yonghu_id_number", seachFields.get("yonghuIdNumber")));
                        if(yonghuEntities_yonghuIdNumber.size() >0 ){
                            ArrayList<String> repeatFields = new ArrayList<>();
                            for(YonghuEntity s:yonghuEntities_yonghuIdNumber){
                                repeatFields.add(s.getYonghuIdNumber());
                            }
                            return R.error(511,"数据库的该表中的 [用户身份证号] 字段已经存在 存在数据为:"+repeatFields.toString());
                        }
                        yonghuService.insertBatch(yonghuList);
                        return R.ok();
                    }
                }
            }
        }catch (Exception e){
            return R.error(511,"批量插入数据异常,请联系管理员");
        }
    }


    /**
    * 登录
    */
    @IgnoreAuth
    @RequestMapping(value = "/login")
    public R login(String username, String password, String captcha, HttpServletRequest request) {
        YonghuEntity yonghu = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("username", username));
        if(yonghu==null || !yonghu.getPassword().equals(password))
            return R.error("账号或密码不正确");
        //  // 获取监听器中的字典表
        // ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
        // Map<String, Map<Integer, String>> dictionaryMap= (Map<String, Map<Integer, String>>) servletContext.getAttribute("dictionaryMap");
        // Map<Integer, String> role_types = dictionaryMap.get("role_types");
        // role_types.get(.getRoleTypes());
        String token = tokenService.generateToken(yonghu.getId(),username, "yonghu", "用户");
        R r = R.ok();
        r.put("token", token);
        r.put("role","用户");
        r.put("username",yonghu.getYonghuName());
        r.put("tableName","yonghu");
        r.put("userId",yonghu.getId());
        return r;
    }

    /**
    * 注册
    */
    @IgnoreAuth
    @PostMapping(value = "/register")
    public R register(@RequestBody YonghuEntity yonghu){
//    	ValidatorUtils.validateEntity(user);
        Wrapper<YonghuEntity> queryWrapper = new EntityWrapper<YonghuEntity>()
            .eq("username", yonghu.getUsername())
            .or()
            .eq("yonghu_phone", yonghu.getYonghuPhone())
            .or()
            .eq("yonghu_id_number", yonghu.getYonghuIdNumber())
            ;
        YonghuEntity yonghuEntity = yonghuService.selectOne(queryWrapper);
        if(yonghuEntity != null)
            return R.error("账户或者用户手机号或者用户身份证号已经被使用");
        yonghu.setCreateTime(new Date());
        yonghuService.insert(yonghu);
        return R.ok();
    }

    /**
     * 重置密码
     */
    @GetMapping(value = "/resetPassword")
    public R resetPassword(Integer  id){
        YonghuEntity yonghu = new YonghuEntity();
        yonghu.setPassword("123456");
        yonghu.setId(id);
        yonghuService.updateById(yonghu);
        return R.ok();
    }


    /**
     * 忘记密码
     */
    @IgnoreAuth
    @RequestMapping(value = "/resetPass")
    public R resetPass(String username, HttpServletRequest request) {
        YonghuEntity yonghu = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("username", username));
        if(yonghu!=null){
            yonghu.setPassword("123456");
            boolean b = yonghuService.updateById(yonghu);
            if(!b){
               return R.error();
            }
        }else{
           return R.error("账号不存在");
        }
        return R.ok();
    }


    /**
    * 获取用户的session用户信息
    */
    @RequestMapping("/session")
    public R getCurrYonghu(HttpServletRequest request){
        Integer id = (Integer)request.getSession().getAttribute("userId");
        YonghuEntity yonghu = yonghuService.selectById(id);
        if(yonghu !=null){
            //entity转view
            YonghuView view = new YonghuView();
            BeanUtils.copyProperties( yonghu , view );//把实体数据重构到view中

            //修改对应字典表字段
            dictionaryService.dictionaryConvert(view, request);
            return R.ok().put("data", view);
        }else {
            return R.error(511,"查不到数据");
        }
    }


    /**
    * 退出
    */
    @GetMapping(value = "logout")
    public R logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.ok("退出成功");
    }




    /**
    * 前端列表
    */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        logger.debug("list方法:,,Controller:{},,params:{}",this.getClass().getName(),JSONObject.toJSONString(params));

        // 没有指定排序字段就默认id倒序
        if(StringUtil.isEmpty(String.valueOf(params.get("orderBy")))){
            params.put("orderBy","id");
        }
        PageUtils page = yonghuService.queryPage(params);

        //字典表数据转换
        List<YonghuView> list =(List<YonghuView>)page.getList();
        for(YonghuView c:list)
            dictionaryService.dictionaryConvert(c, request); //修改对应字典表字段
        return R.ok().put("data", page);
    }

    /**
    * 前端详情
    */
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id, HttpServletRequest request){
        logger.debug("detail方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
        YonghuEntity yonghu = yonghuService.selectById(id);
            if(yonghu !=null){


                //entity转view
                YonghuView view = new YonghuView();
                BeanUtils.copyProperties( yonghu , view );//把实体数据重构到view中

                //修改对应字典表字段
                dictionaryService.dictionaryConvert(view, request);
                return R.ok().put("data", view);
            }else {
                return R.error(511,"查不到数据");
            }
    }


    /**
    * 前端保存
    */
    @RequestMapping("/add")
    public R add(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
        logger.debug("add方法:,,Controller:{},,yonghu:{}",this.getClass().getName(),yonghu.toString());
        Wrapper<YonghuEntity> queryWrapper = new EntityWrapper<YonghuEntity>()
            .eq("username", yonghu.getUsername())
            .or()
            .eq("yonghu_phone", yonghu.getYonghuPhone())
            .or()
            .eq("yonghu_id_number", yonghu.getYonghuIdNumber())
            ;
        logger.info("sql语句:"+queryWrapper.getSqlSegment());
        YonghuEntity yonghuEntity = yonghuService.selectOne(queryWrapper);
        if(yonghuEntity==null){
            yonghu.setCreateTime(new Date());
        yonghu.setPassword("123456");
        //  String role = String.valueOf(request.getSession().getAttribute("role"));
        //  if("".equals(role)){
        //      yonghu.set
        //  }
        yonghuService.insert(yonghu);
            return R.ok();
        }else {
            return R.error(511,"账户或者用户手机号或者用户身份证号已经被使用");
        }
    }


}

声明

本博客适用于广泛的学术和教育用途,包括但不限于个人学习、开发设计,产品设计。仅供学习参考,旨在为读者提供深入理解和学术研究的材料。

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值