毕设基于SSM+Vue3实现设备维修管理系统四:后台框架及基础增删改查功能实现

本章介绍后端基础框架及基础的增删改查功能实现,创建基础的dao、service即controller层相关的基类,并实现基础的增删改查相关功能。

源码下载: 点击下载
讲解视频:

SMM+VUE3实现设备维修管理系统毕设:后端框架搭建及表外键添加

一、基础框架搭建

1.1 DAO层

使用mybatisplus提供的BaseMapper,实现基础的数据库增删改查功能。

java 复制代码
package com.junjunjun.device.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.junjunjun.device.entity.BaseEntity;

/**
 * 数据层处理的基础类
 */
public interface BaseDao<T extends BaseEntity> extends BaseMapper<T> {

}

1.2 SERVICE层

service层实现业务逻辑,包括数据填充、数据缓存、事务、关联数据处理等基础逻辑。

java 复制代码
package com.junjunjun.device.service;

import com.junjunjun.device.entity.BaseEntity;

/**
 * 业务逻辑基础
 * @param <T>
 */
public interface IBaseService<T extends BaseEntity> {

}

实现数据删除基础服务,为需要删除数据的实体提供对应的数据删除业务逻辑。

java 复制代码
package com.junjunjun.device.service;

import com.junjunjun.device.entity.BaseDataEntity;
/**
 * 删除数据
 * @param <T>
 */
public interface IBaseDeleteService<T extends BaseDataEntity> extends IBaseService<T>{
	/**
	 * 删除数据
	 * @param id
	 */
	void delete(Long id);
}

数据保存基础处理逻辑。

java 复制代码
package com.junjunjun.device.service;

import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.vo.BaseDataVo;

/**
 * 数据保存
 * 
 * @param <T>
 */
public interface IBaseSaveService<V extends BaseDataVo,T extends BaseDataEntity> extends IBaseService<T> {
	/**
	 * 保存数据
	 * 
	 * @param data
	 */
	V save(V data);
}

数据更新基础处理逻辑。

java 复制代码
package com.junjunjun.device.service;

import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.vo.BaseDataVo;

/**
 * 更新数据
 * 
 * @param <T>
 */
public interface IBaseUpdateService<V extends BaseDataVo,T extends BaseDataEntity> extends IBaseService<T> {
	/**
	 * 更新数据
	 * 
	 * @param data
	 */
	V update(V data);
}

数据查询相关基础处理逻辑,包括详情、分页等查询功能。

java 复制代码
package com.junjunjun.device.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.vo.BaseDataVo;

/**
 * 查詢數據
 * 
 * @param <T>
 */
public interface IBaseViewService<V extends BaseDataVo, T extends BaseDataEntity> extends IBaseService<T> {
	/**
	 * 获取详情
	 * 
	 * @param id
	 * @return
	 */
	V get(Long id);

	/**
	 * 分页
	 * 
	 * @param size
	 * @param pageNo
	 * @param dict
	 * @return
	 */
	Page<V> page(Page<V> page, V data);
}

service服务实现基类。

java 复制代码
package com.junjunjun.device.service.impl;

import org.springframework.security.core.GrantedAuthority;

import com.junjunjun.device.entity.BaseEntity;
import com.junjunjun.device.entity.system.User;
import com.junjunjun.device.service.IBaseService;

import lombok.extern.slf4j.Slf4j;

/**
 * 业务逻辑基础类
 * 
 * @param <T>
 */
@Slf4j
public abstract class BaseServiceImpl<T extends BaseEntity> implements IBaseService<T> {
	/**
	 * 判断当前登录用户是否是管理员
	 * 
	 * @param user
	 * @return
	 */
	protected boolean currentUserIsAdmin(User user) {
		if (user == null) {
			return false;
		}
		for (GrantedAuthority item : user.getAuthorities()) {
			if ("ROLE_ADMIN".equals(item.getAuthority())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断当前登录用户是否是部門管理员
	 * 
	 * @param user
	 * @return
	 */
	protected boolean currentUserIsDepartmentAdmin(User user) {
		if (user == null) {
			return false;
		}
		for (GrantedAuthority item : user.getAuthorities()) {
			if ("ROLE_DEPARTMENT_ADMIN".equals(item.getAuthority())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断当前登录用户是否具有审核权限
	 * 
	 * @param user
	 * @return
	 */
	protected boolean currentUserIsExamineAdmin(User user) {
		if (user == null) {
			return false;
		}
		for (GrantedAuthority item : user.getAuthorities()) {
			if ("ROLE_EXAMINE_ADMIN".equals(item.getAuthority())) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断当前登录用户是否具有审核权限
	 * 
	 * @param user
	 * @return
	 */
	protected boolean currentUserIsCheckAdmin(User user) {
		if (user == null) {
			return false;
		}
		for (GrantedAuthority item : user.getAuthorities()) {
			if ("ROLE_CHECK_ADMIN".equals(item.getAuthority())) {
				return true;
			}
		}
		return false;
	}
}

数据删除基础服务。

java 复制代码
package com.junjunjun.device.service.impl;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;

import com.junjunjun.device.CacheRemove;
import com.junjunjun.device.SaveLog;
import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.service.IBaseDeleteService;
import com.junjunjun.device.vo.BaseDataVo;

/**
 * 刪除功能
 * 
 * @param <T>
 */
public abstract class BaseDeleteServiceImpl<V extends BaseDataVo, T extends BaseDataEntity>
		extends BaseUpdateServiceImpl<V, T> implements IBaseDeleteService<T> {
	@Override
	@SaveLog(operDesc = "刪除数据信息", operType = "刪除")
	@CacheEvict(cacheNames = "systemcache", key = "#root.targetClass+'_get_'+#id")
	@CacheRemove(value =  {"#root.targetClass+'_page_'"},cache = "systemcache")
	@Transactional(rollbackFor = Exception.class)
	public void delete(Long id) {
		getDao().deleteById(id);
		afterDelete(id);
	}

	protected void afterDelete(Long id) {

	}
}

数据保存基础服务。

java 复制代码
package com.junjunjun.device.service.impl;

import org.springframework.cache.annotation.CachePut;
import org.springframework.transaction.annotation.Transactional;

import com.junjunjun.device.CacheRemove;
import com.junjunjun.device.SaveLog;
import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.service.IBaseSaveService;
import com.junjunjun.device.vo.BaseDataVo;

/**
 * 保存數據
 */
public abstract class BaseSaveServiceImpl<V extends BaseDataVo, T extends BaseDataEntity>
		extends BaseViewServiceImpl<V, T> implements IBaseSaveService<V, T> {

	@Override
	@SaveLog(operDesc = "添加数据信息", operType = "添加")
	@CachePut(cacheNames = "systemcache", key = "#root.targetClass+'_get_'+#result.id")
	@CacheRemove(value =  {"#root.targetClass+'_page_'"},cache = "systemcache")
	@Transactional(rollbackFor = Exception.class)
	public V save(V data) {
		/**
		 * 1.默认数据的填充
		 * 
		 * 2.记录操作日志
		 * 
		 * 3.缓存数据的更新
		 */
		T e = getDto().voToEntity(data);
		getDao().insert(e);
		V result = getDto().entityToVo(e);
		afterSave(result,data);
		return result;
	}

	protected void afterSave(V result,V data) {
	}
}

数据更新基础服务。

java 复制代码
package com.junjunjun.device.service.impl;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.junjunjun.device.CacheRemove;
import com.junjunjun.device.SaveLog;
import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.service.IBaseUpdateService;
import com.junjunjun.device.vo.BaseDataVo;

/**
 * 添加/更新/查看功能
 * 
 * @param <T>
 */
public abstract class BaseUpdateServiceImpl<V extends BaseDataVo, T extends BaseDataEntity>
		extends BaseSaveServiceImpl<V, T> implements IBaseUpdateService<V, T> {

	/**
	 * 填充要更新的字段
	 * 
	 * @param data
	 * @param updateWrapper
	 */
	protected abstract void initUpdateWrapper(V data, UpdateWrapper<T> updateWrapper);

	@Override
	@SaveLog(operDesc = "更新数据信息", operType = "更新")
	@CacheRemove(value =  {"#root.targetClass+'_page_'"},cache = "systemcache")
	@CachePut(cacheNames = "systemcache", key = "#root.targetClass+'_get_'+#result.id")
	@Transactional(rollbackFor = Exception.class)
	public V update(V data) {
		UpdateWrapper<T> updateWrapper = new UpdateWrapper<T>();
		initUpdateWrapper(data, updateWrapper);
		updateWrapper.eq("id", data.getId());
		getDao().update(updateWrapper);
		afterUpdate(data);

		return get(data.getId());
	}

	protected void afterUpdate(V data) {

	}
}

数据查询相关业务基础服务。

java 复制代码
package com.junjunjun.device.service.impl;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.junjunjun.device.dao.BaseDao;
import com.junjunjun.device.dto.BaseDto;
import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.service.IBaseViewService;
import com.junjunjun.device.vo.BaseDataVo;

/**
 * 查看功能接口
 * 
 * @param <T>
 */
public abstract class BaseViewServiceImpl<V extends BaseDataVo, T extends BaseDataEntity> extends BaseServiceImpl<T>
		implements IBaseViewService<V, T> {

	/**
	 * 获取具体的dao
	 * 
	 * @return
	 */
	protected abstract BaseDao<T> getDao();

	/**
	 * 获取具体的dao
	 * 
	 * @return
	 */
	protected abstract BaseDto<V, T> getDto();

	/**
	 * 填充搜索条件
	 * 
	 * @param dict
	 * @param queryWrapper
	 */
	protected abstract void initQueryWrapper(V data, QueryWrapper<T> queryWrapper);

	@Override
	@Cacheable(value = "systemcache", key="#root.targetClass+'_'+#root.method.name+'_'+#id")
	public V get(Long id) {
		T t = getDao().selectById(id);
		if(t==null) {
			return null;
		}
		V v = getDto().entityToVo(t);
		return afterGet(v);
	}

	/**
	 * 注入关联数据
	 * 
	 * @param v
	 * @return
	 */
	protected V afterGet(V v) {
		return v;
	};

	@Override
//	@Cacheable(value = "systemcache", key="#root.targetClass+'_'+#root.method.name+'_'+#page.orders+'_'+#page.current+'_'+#page.size+'_'+#data")
	@Cacheable(value = "systemcache", keyGenerator = "cachekKeyGenerator")
	public Page<V> page(Page<V> page, V data) {
		Page<T> tpage = new Page<>(page.getCurrent(), page.getSize());
		QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
		initQueryWrapper(data, queryWrapper);
		queryWrapper.orderByDesc("create_date");
		tpage = getDao().selectPage(tpage, queryWrapper);

		page.setCountId(tpage.countId());
		page.setCurrent(tpage.getCurrent()).setMaxLimit(tpage.maxLimit());
		page.setOptimizeCountSql(tpage.optimizeCountSql());
		page.setOptimizeJoinOfCountSql(tpage.optimizeJoinOfCountSql());
		page.setOrders(tpage.orders());
		page.setPages(tpage.getPages());
		page.setSize(tpage.getSize());
		List<V> vs = Lists.newArrayList();
		tpage.getRecords().forEach(item -> {
			V v = getDto().entityToVo(item);
			afterGet(v);
			vs.add(v);
		});
		page.setRecords(vs);
		page.setSize(tpage.getSize()).setTotal(tpage.getTotal());
		page.setSearchCount(tpage.searchCount());
		return page;
	}

}

1.3 CONTROLLER层

controller基础类,所有的controller都继承此类。

java 复制代码
package com.junjunjun.device.api;

import lombok.extern.slf4j.Slf4j;

/**
 * 接口的抽象类
 */
@Slf4j
public abstract class BaseController {

}

数据删除接口基础类。

java 复制代码
package com.junjunjun.device.api;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.junjunjun.device.service.IBaseDeleteService;
import com.junjunjun.device.vo.BaseDataVo;
import com.junjunjun.device.vo.ResultVo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BaseDeleteController<T extends BaseDataVo> extends BaseSaveController<T> {
	@PostMapping("/delete/{id}")
	@ResponseBody
	@PreAuthorize("hasAuthority(#this.this.class.name+':delete')")
	public ResultVo<T> delete(@Validated @NotNull(message = "ID不能为空") @PathVariable Long id, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO 参数及权限校验
		try {
			((IBaseDeleteService<?>) getService()).delete(id);
			return ResultVo.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("更新异常", e);
			return ResultVo.error(e.getMessage());
		}
	};
}

数据保存接口基础类。

java 复制代码
package com.junjunjun.device.api;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.junjunjun.device.Save;
import com.junjunjun.device.service.IBaseSaveService;
import com.junjunjun.device.vo.BaseDataVo;
import com.junjunjun.device.vo.ResultVo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BaseSaveController<T extends BaseDataVo> extends BaseUpdateController<T> {
	@PostMapping("/save")
	@ResponseBody
	@PreAuthorize("hasAuthority(#this.this.class.name+':save')")
	public ResultVo<T> save(@Validated(value = Save.class) @RequestBody T t, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO 参数及权限校验
		try {
			t = ((IBaseSaveService<T, ?>) getService()).save(t);
			return ResultVo.success(t);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("更新异常", e);
			return ResultVo.error(e.getMessage());
		}
	};
}

数据更新接口基础类。

java 复制代码
package com.junjunjun.device.api;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.junjunjun.device.Update;
import com.junjunjun.device.service.IBaseUpdateService;
import com.junjunjun.device.vo.BaseDataVo;
import com.junjunjun.device.vo.ResultVo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BaseUpdateController<T extends BaseDataVo> extends BaseViewController<T> {

	@PostMapping("/update")
	@ResponseBody
	@PreAuthorize("hasAuthority(#this.this.class.name+':update')")
	public ResultVo<T> update(@Validated(value = Update.class) @RequestBody T t, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO 参数及权限校验
		try {
			((IBaseUpdateService<T, ?>) getService()).update(t);
			return ResultVo.success(t);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("更新异常", e);
			return ResultVo.error(e.getMessage());
		}
	};
}

数据查询接口基础类。

java 复制代码
package com.junjunjun.device.api;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junjunjun.device.entity.BaseDataEntity;
import com.junjunjun.device.service.IBaseService;
import com.junjunjun.device.service.IBaseViewService;
import com.junjunjun.device.vo.BaseDataVo;
import com.junjunjun.device.vo.PageVo;
import com.junjunjun.device.vo.ResultVo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;

@RequestMapping(path = "/")
public abstract class BaseViewController<T extends BaseDataVo> extends BaseDataController<T> {

	/**
	 * 获取具体的service服务
	 * 
	 * @return
	 */
	protected abstract <E extends BaseDataEntity> IBaseService<E> getService();

	@GetMapping("/get/{id}")
	@ResponseBody
	@PreAuthorize("hasAuthority(#this.this.class.name+':get')")
	public ResultVo<T> get(@Validated  @NotNull(message = "ID不能为空") @PathVariable Long id, HttpServletRequest request,
			HttpServletResponse response) {
		if (id == null) {
			return ResultVo.error("参数为空");
		}
		// TODO 权限的检测
		T t = ((IBaseViewService<T, ?>) getService()).get(id);

		return ResultVo.success(t);
	};

	@PostMapping("/page")
	@ResponseBody
	@PreAuthorize("hasAuthority(#this.this.class.name+':page')")
	public ResultVo<T> page(@RequestBody PageVo<T> page, HttpServletRequest request, HttpServletResponse response) {
		if (page == null) {
			page = new PageVo<>();
		}
		if (page.getPage() == null) {
			page.setPage(new Page<>(1, 10));
		}
		Page<T> result = ((IBaseViewService<T, ?>) getService()).page(page.getPage(), page.getEntity());
		return ResultVo.success(result);
	};
}

二、增删改查实现

用户 controller service dao 数据库 调用接口,进行参数、权限检测。 调用对应service方法,进行相关业务逻辑处理。 调用dao数据操作。 进行数据库操作。 用户 controller service dao 数据库

2.1 数据添加

controller接口层通过save方法进行请求,请求后进行参数合法性及权限检测,调用service中的save方法进行数据保存,service层保存数据时自动填充默认数据及相关业务逻辑处理。service处理完毕后调用dao层进行数据库操作,数据库操作完毕后清除相关缓存。

2.2 数据删除

controller接口层通过delete方法进行请求,请求后进行参数合法性及权限检测,调用service中的delete方法进行数据删除,service层删除数据时自动处理相关业务逻辑。service处理完毕后调用dao层进行数据库操作,数据库操作完毕后清除相关缓存。

2.3 数据修改

controller接口层通过update方法进行请求,请求后进行参数合法性及权限检测,调用service中的update方法进行数据更新,service层更新数据时自动填充默认数据及相关业务逻辑处理。service处理完毕后调用dao层进行数据库操作。

2.4 分页查询

controller接口层通过page方法进行请求,请求后进行参数合法性及权限检测,调用service中的page方法进行数据查询,service层调用dao层进行数据库查询数据时自动填充关联数据并进行缓存处理。

2.5 详情查询

controller接口层通过get方法进行请求,请求后进行参数合法性及权限检测,调用service中的get方法进行数据查询,service层调用dao层进行数据库查询数据时自动填充关联数据并进行缓存处理。

某些实体只能进行查询,不能进行删除、更新等操作,可以通过只继承Update或VIew相关的基类实现对应功能的控制。

相关推荐
天天进步20151 分钟前
Vue+Springboot用Websocket实现协同编辑
vue.js·spring boot·websocket
草莓base12 分钟前
【手写一个spring】spring源码的简单实现--bean对象的创建
java·spring·rpc
Estar.Lee15 分钟前
时间操作[计算时间差]免费API接口教程
android·网络·后端·网络协议·tcp/ip
legend_jz24 分钟前
【Linux】线程控制
linux·服务器·开发语言·c++·笔记·学习·学习方法
drebander37 分钟前
使用 Java Stream 优雅实现List 转化为Map<key,Map<key,value>>
java·python·list
乌啼霜满天24940 分钟前
Spring 与 Spring MVC 与 Spring Boot三者之间的区别与联系
java·spring boot·spring·mvc
tangliang_cn1 小时前
java入门 自定义springboot starter
java·开发语言·spring boot
程序猿阿伟1 小时前
《智能指针频繁创建销毁:程序性能的“隐形杀手”》
java·开发语言·前端
Grey_fantasy1 小时前
高级编程之结构化代码
java·spring boot·spring cloud
新知图书1 小时前
Rust编程与项目实战-模块std::thread(之一)
开发语言·后端·rust