Merge pull request '杜韦注释' (#5) from 杜韦注释 into main
commit
bfe69c539c
@ -0,0 +1,24 @@
|
||||
// 定义当前接口所在的包路径
|
||||
package com.service;
|
||||
|
||||
// 导入Java集合框架中的Map接口
|
||||
import java.util.Map;
|
||||
|
||||
// 导入MyBatis-Plus服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入配置实体类
|
||||
import com.entity.ConfigEntity;
|
||||
// 导入分页工具类
|
||||
import com.utils.PageUtils;
|
||||
|
||||
// 系统配置服务接口
|
||||
// 继承MyBatis-Plus通用服务接口
|
||||
// 泛型参数为ConfigEntity配置实体类
|
||||
public interface ConfigService extends IService<ConfigEntity> {
|
||||
|
||||
// 分页查询配置数据方法
|
||||
// params - 包含查询条件的参数Map
|
||||
// 返回分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
// 定义当前接口所在的包路径
|
||||
package com.service;
|
||||
|
||||
// 导入MyBatis-Plus服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入分页工具类
|
||||
import com.utils.PageUtils;
|
||||
// 导入字典实体类
|
||||
import com.entity.DictionaryEntity;
|
||||
// 导入Java集合框架中的Map接口
|
||||
import java.util.Map;
|
||||
// 导入HTTP请求对象
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入Spring空值注解
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入Java集合框架中的List接口
|
||||
import java.util.List;
|
||||
|
||||
// 字典服务接口
|
||||
// 继承MyBatis-Plus通用服务接口
|
||||
// 泛型参数为DictionaryEntity字典实体类
|
||||
public interface DictionaryService extends IService<DictionaryEntity> {
|
||||
|
||||
// 分页查询字典数据方法
|
||||
// params - 包含查询条件的参数Map
|
||||
// 返回分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
// 字典表数据转换方法
|
||||
// obj - 需要进行字典转换的对象
|
||||
// request - HTTP请求对象
|
||||
void dictionaryConvert(Object obj, HttpServletRequest request);
|
||||
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
package com.service.impl; // 定义服务实现类包路径
|
||||
|
||||
import com.utils.StringUtil; // 导入字符串工具类
|
||||
import com.service.DictionaryService; // 导入字典服务接口
|
||||
import com.utils.ClazzDiff; // 导入类差异工具类
|
||||
import org.springframework.beans.BeanUtils; // 导入Spring Bean工具类
|
||||
import org.springframework.beans.factory.annotation.Autowired; // 导入自动装配注解
|
||||
import org.springframework.stereotype.Service; // 导入服务注解
|
||||
import java.lang.reflect.Field; // 导入反射Field类
|
||||
import java.util.*; // 导入常用集合类
|
||||
import com.baomidou.mybatisplus.plugins.Page; // 导入MyBatis-Plus分页组件
|
||||
import com.baomidou.mybatisplus.service.impl.ServiceImpl; // 导入MyBatis-Plus服务实现基类
|
||||
import org.springframework.transaction.annotation.Transactional; // 导入事务注解
|
||||
import com.utils.PageUtils; // 导入分页工具类
|
||||
import com.utils.Query; // 导入查询工具类
|
||||
import org.springframework.web.context.ContextLoader; // 导入上下文加载器
|
||||
import javax.servlet.ServletContext; // 导入Servlet上下文接口
|
||||
import javax.servlet.http.HttpServletRequest; // 导入HTTP请求接口
|
||||
import org.springframework.lang.Nullable; // 导入可空注解
|
||||
import org.springframework.util.Assert; // 导入断言工具
|
||||
import com.dao.DictionaryDao; // 导入字典数据访问接口
|
||||
import com.entity.DictionaryEntity; // 导入字典实体类
|
||||
import com.service.DictionaryService; // 导入字典服务接口
|
||||
import com.entity.view.DictionaryView; // 导入字典视图类
|
||||
|
||||
@Service("dictionaryService") // 声明为Spring服务组件
|
||||
@Transactional // 启用事务管理
|
||||
public class DictionaryServiceImpl extends ServiceImpl<DictionaryDao, DictionaryEntity> implements DictionaryService {
|
||||
|
||||
@Override // 重写分页查询方法
|
||||
public PageUtils queryPage(Map<String,Object> params) {
|
||||
Page<DictionaryView> page =new Query<DictionaryView>(params).getPage(); // 创建分页对象
|
||||
page.setRecords(baseMapper.selectListView(page,params)); // 设置分页记录
|
||||
return new PageUtils(page); // 返回分页结果
|
||||
}
|
||||
|
||||
public void dictionaryConvert(Object obj, HttpServletRequest request) {
|
||||
try {
|
||||
if (obj == null) return; // 空对象直接返回
|
||||
|
||||
List<String> fieldNameList = new ArrayList<>(); // 创建字段名列表
|
||||
Class tempClass = obj.getClass(); // 获取对象类
|
||||
while (tempClass !=null) {
|
||||
Field[] declaredFields = tempClass.getDeclaredFields(); // 获取所有声明字段
|
||||
for (Field f : declaredFields) {
|
||||
f.setAccessible(true); // 设置字段可访问
|
||||
if (f.getType().getName().equals("java.lang.Integer") && f.getName().contains("Types")) {
|
||||
fieldNameList.add(f.getName()); // 添加符合条件的字段名
|
||||
}
|
||||
}
|
||||
tempClass = tempClass.getSuperclass(); // 获取父类继续查找
|
||||
}
|
||||
|
||||
ServletContext servletContext = request.getServletContext(); // 获取Servlet上下文
|
||||
Map<String, Map<Integer, String>> dictionaryMap= (Map<String, Map<Integer, String>>) servletContext.getAttribute("dictionaryMap"); // 获取字典映射
|
||||
|
||||
for (String s : fieldNameList) {
|
||||
Field types = null; // 声明Types字段
|
||||
if(hasField(obj.getClass(),s)){
|
||||
types= obj.getClass().getDeclaredField(s); // 获取当前类字段
|
||||
}else{
|
||||
types=obj.getClass().getSuperclass().getDeclaredField(s); // 获取父类字段
|
||||
}
|
||||
Field value = obj.getClass().getDeclaredField(s.replace("Types", "Value")); // 获取Value字段
|
||||
types.setAccessible(true); // 设置可访问
|
||||
value.setAccessible(true); // 设置可访问
|
||||
|
||||
if (StringUtil.isNotEmpty(String.valueOf(types.get(obj)))) { // 值不为空
|
||||
int i = Integer.parseInt(String.valueOf(types.get(obj))); // 获取整数值
|
||||
char[] chars = s.toCharArray(); // 转换为字符数组
|
||||
StringBuffer sbf = new StringBuffer(); // 创建字符串缓冲区
|
||||
for(int b=0; b< chars.length; b++){
|
||||
char ch = chars[b]; // 获取当前字符
|
||||
if(ch <= 90 && ch >= 65){ // 大写字母处理
|
||||
sbf.append("_"); // 添加下划线
|
||||
ch += 32; // 转小写
|
||||
}
|
||||
sbf.append(ch); // 添加字符
|
||||
}
|
||||
String s2 = dictionaryMap.get(sbf.toString()).get(i); // 获取字典值
|
||||
value.set(obj, s2); // 设置字段值
|
||||
} else {
|
||||
new Exception("字典表赋值出现问题::::"+value.getName()); // 抛出异常
|
||||
value.set(obj, ""); // 设置空值
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace(); // 打印异常
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace(); // 打印异常
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(); // 打印异常
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasField(Class c, String fieldName){
|
||||
Field[] fields = c.getDeclaredFields(); // 获取所有声明字段
|
||||
|
||||
for (Field f : fields) {
|
||||
if (fieldName.equals(f.getName())) { // 匹配字段名
|
||||
return true; // 返回存在
|
||||
}
|
||||
}
|
||||
|
||||
return false; // 返回不存在
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
// 定义当前类所在的包路径
|
||||
package com.ServletContextListener;
|
||||
|
||||
// 导入MyBatis-Plus实体包装类
|
||||
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
||||
// 导入字典实体类
|
||||
import com.entity.DictionaryEntity;
|
||||
// 导入字典服务接口
|
||||
import com.service.DictionaryService;
|
||||
// 导入自定义线程方法类
|
||||
import com.thread.MyThreadMethod;
|
||||
// 导入日志记录器接口
|
||||
import org.slf4j.Logger;
|
||||
// 导入日志工厂类
|
||||
import org.slf4j.LoggerFactory;
|
||||
// 导入Spring应用上下文接口
|
||||
import org.springframework.context.ApplicationContext;
|
||||
// 导入Web应用上下文工具类
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
// 导入Servlet监听器接口
|
||||
import javax.servlet.ServletContextListener;
|
||||
// 导入Servlet上下文事件类
|
||||
import javax.servlet.ServletContextEvent;
|
||||
// 导入Web监听器注解
|
||||
import javax.servlet.annotation.WebListener;
|
||||
// 导入HashMap实现类
|
||||
import java.util.HashMap;
|
||||
// 导入List集合接口
|
||||
import java.util.List;
|
||||
// 导入Map集合接口
|
||||
import java.util.Map;
|
||||
|
||||
// 字典初始化监听器类
|
||||
// 使用@WebListener注解声明为Servlet监听器
|
||||
// 实现ServletContextListener接口
|
||||
@WebListener
|
||||
public class DictionaryServletContextListener implements ServletContextListener {
|
||||
|
||||
// 声明日志记录器
|
||||
private static final Logger logger = LoggerFactory.getLogger(DictionaryServletContextListener.class);
|
||||
// 声明自定义线程方法对象
|
||||
private MyThreadMethod myThreadMethod;
|
||||
|
||||
// 上下文销毁方法实现
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
// 记录服务器停止日志
|
||||
logger.info("----------服务器停止----------");
|
||||
}
|
||||
|
||||
// 上下文初始化方法实现
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
// 获取Spring应用上下文
|
||||
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
|
||||
|
||||
// 记录字典表初始化开始日志
|
||||
logger.info("----------字典表初始化开始----------");
|
||||
// 从Spring容器获取字典服务实例
|
||||
DictionaryService dictionaryService = (DictionaryService)appContext.getBean("dictionaryService");
|
||||
// 查询所有字典数据
|
||||
List<DictionaryEntity> dictionaryEntities = dictionaryService.selectList(new EntityWrapper<DictionaryEntity>());
|
||||
// 创建字典映射表
|
||||
Map<String, Map<Integer,String>> map = new HashMap<>();
|
||||
// 遍历字典数据并构建映射关系
|
||||
for(DictionaryEntity d :dictionaryEntities){
|
||||
// 获取当前字典代码对应的映射表
|
||||
Map<Integer, String> m = map.get(d.getDicCode());
|
||||
// 如果映射表为空则新建
|
||||
if(m ==null || m.isEmpty()){
|
||||
m = new HashMap<>();
|
||||
}
|
||||
// 添加字典项到映射表
|
||||
m.put(d.getCodeIndex(),d.getIndexName());
|
||||
// 更新字典映射表
|
||||
map.put(d.getDicCode(),m);
|
||||
}
|
||||
// 将字典映射表存入Servlet上下文
|
||||
sce.getServletContext().setAttribute("dictionaryMap", map);
|
||||
// 记录字典表初始化完成日志
|
||||
logger.info("----------字典表初始化完成----------");
|
||||
|
||||
// 记录线程执行开始日志
|
||||
logger.info("----------线程执行开始----------");
|
||||
// 初始化并启动自定义线程
|
||||
if (myThreadMethod == null) {
|
||||
myThreadMethod = new MyThreadMethod();
|
||||
myThreadMethod.start(); // 在Servlet上下文初始化时启动线程
|
||||
}
|
||||
// 记录线程执行结束日志
|
||||
logger.info("----------线程执行结束----------");
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.utils; // 声明包路径
|
||||
|
||||
import java.io.ByteArrayOutputStream; // 导入字节数组输出流类
|
||||
import java.io.File; // 导入文件操作类
|
||||
import java.io.FileInputStream; // 导入文件输入流类
|
||||
import java.io.IOException; // 导入IO异常类
|
||||
import java.io.InputStream; // 导入输入流基类
|
||||
|
||||
public class FileUtil { // 文件工具类定义
|
||||
|
||||
public static byte[] FileToByte(File file) throws IOException { // 文件转字节数组方法
|
||||
@SuppressWarnings("resource") // 忽略资源未关闭警告
|
||||
InputStream content = new FileInputStream(file); // 创建文件输入流
|
||||
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); // 创建字节数组输出流
|
||||
byte[] buff = new byte[100]; // 创建100字节缓冲区
|
||||
int rc = 0; // 读取字节数变量
|
||||
while ((rc = content.read(buff, 0, 100)) > 0) { // 循环读取文件内容
|
||||
swapStream.write(buff, 0, rc); // 将读取内容写入输出流
|
||||
}
|
||||
return swapStream.toByteArray(); // 返回字节数组
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// 定义当前接口所在的包路径
|
||||
package com.service;
|
||||
|
||||
// 导入MyBatis-Plus服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入分页工具类
|
||||
import com.utils.PageUtils;
|
||||
// 导入健身论坛实体类
|
||||
import com.entity.ForumEntity;
|
||||
// 导入Java集合框架中的Map接口
|
||||
import java.util.Map;
|
||||
// 导入HTTP请求对象
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入Spring空值注解
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入Java集合框架中的List接口
|
||||
import java.util.List;
|
||||
|
||||
// 健身论坛服务接口
|
||||
// 继承MyBatis-Plus通用服务接口
|
||||
// 泛型参数为ForumEntity论坛实体类
|
||||
public interface ForumService extends IService<ForumEntity> {
|
||||
|
||||
// 分页查询论坛数据方法
|
||||
// params - 包含查询条件的参数Map
|
||||
// 返回分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
// 定义包名为 com.service.impl
|
||||
package com.service.impl;
|
||||
|
||||
// 导入 com.utils 包下的 StringUtil 类
|
||||
import com.utils.StringUtil;
|
||||
// 导入 com.service 包下的 DictionaryService 接口
|
||||
import com.service.DictionaryService;
|
||||
// 导入 com.utils 包下的 ClazzDiff 类
|
||||
import com.utils.ClazzDiff;
|
||||
// 导入 Spring 框架的 BeanUtils 类
|
||||
import org.springframework.beans.BeanUtils;
|
||||
// 导入 Spring 框架的 Autowired 注解
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
// 导入 Spring 框架的 Service 注解
|
||||
import org.springframework.stereotype.Service;
|
||||
// 导入 Java 反射机制中的 Field 类
|
||||
import java.lang.reflect.Field;
|
||||
// 导入 Java 集合框架中的相关类
|
||||
import java.util.*;
|
||||
// 导入 MyBatis-Plus 的 Page 类
|
||||
import com.baomidou.mybatisplus.plugins.Page;
|
||||
// 导入 MyBatis-Plus 的 ServiceImpl 类
|
||||
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
||||
// 导入 Spring 框架的 Transactional 注解
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
// 导入自定义的 PageUtils 类
|
||||
import com.utils.PageUtils;
|
||||
// 导入自定义的 Query 类
|
||||
import com.utils.Query;
|
||||
// 导入 Spring 框架的 ContextLoader 类
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
// 导入 Servlet 上下文类
|
||||
import javax.servlet.ServletContext;
|
||||
// 导入 HTTP 请求类
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入 Spring 框架的 Nullable 注解
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入 Spring 框架的 Assert 类
|
||||
import org.springframework.util.Assert;
|
||||
// 导入数据访问对象 ForumDao 接口
|
||||
import com.dao.ForumDao;
|
||||
// 导入实体类 ForumEntity
|
||||
import com.entity.ForumEntity;
|
||||
// 导入服务接口 ForumService
|
||||
import com.service.ForumService;
|
||||
// 导入视图类 ForumView
|
||||
import com.entity.view.ForumView;
|
||||
|
||||
// 使用 Spring 的 Service 注解,将该类标记为服务组件,名称为 forumService
|
||||
@Service("forumService")
|
||||
// 使用 Spring 的 Transactional 注解,开启事务管理
|
||||
@Transactional
|
||||
// 定义 ForumServiceImpl 类,继承自 MyBatis-Plus 的 ServiceImpl 类,并实现 ForumService 接口
|
||||
public class ForumServiceImpl extends ServiceImpl<ForumDao, ForumEntity> implements ForumService {
|
||||
|
||||
// 重写 ForumService 接口中的 queryPage 方法
|
||||
@Override
|
||||
// 定义 queryPage 方法,用于查询分页数据,接收一个包含查询参数的 Map 对象
|
||||
public PageUtils queryPage(Map<String,Object> params) {
|
||||
// 根据传入的参数创建一个 ForumView 类型的分页对象
|
||||
Page<ForumView> page =new Query<ForumView>(params).getPage();
|
||||
// 调用 baseMapper 的 selectListView 方法查询数据列表,并设置到分页对象中
|
||||
page.setRecords(baseMapper.selectListView(page,params));
|
||||
// 根据分页对象创建 PageUtils 对象并返回
|
||||
return new PageUtils(page);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.utils; // 声明工具类所在包
|
||||
|
||||
import java.io.BufferedReader; // 导入缓冲字符输入流
|
||||
import java.io.InputStreamReader; // 导入字节流转字符流
|
||||
import java.net.HttpURLConnection; // 导入HTTP连接类
|
||||
import java.net.URL; // 导入URL处理类
|
||||
|
||||
public class HttpClientUtils { // HTTP客户端工具类
|
||||
|
||||
public static String doGet(String uri) { // GET请求方法
|
||||
StringBuilder result = new StringBuilder(); // 创建结果字符串构建器
|
||||
try {
|
||||
String res = ""; // 初始化响应字符串
|
||||
URL url = new URL(uri); // 创建URL对象
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 打开HTTP连接
|
||||
BufferedReader in = new BufferedReader( // 创建缓冲读取器
|
||||
new InputStreamReader(conn.getInputStream(), "UTF-8")); // 使用UTF-8编码读取输入流
|
||||
String line; // 声明行字符串变量
|
||||
while ((line = in.readLine()) != null) { // 逐行读取响应内容
|
||||
res += line+"\n"; // 拼接每行内容并换行
|
||||
}
|
||||
in.close(); // 关闭输入流
|
||||
return res; // 返回响应结果
|
||||
} catch (Exception e) { // 异常处理
|
||||
e.printStackTrace(); // 打印异常堆栈
|
||||
return null; // 返回空值
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// 声明该接口所在的包为 com.service
|
||||
package com.service;
|
||||
|
||||
// 导入 MyBatis-Plus 的 IService 接口,这是 MyBatis-Plus 提供的通用服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入自定义的分页工具类,用于处理分页结果
|
||||
import com.utils.PageUtils;
|
||||
// 导入课程收藏实体类,对应数据库中的课程收藏表
|
||||
import com.entity.JianshenkechengCollectionEntity;
|
||||
// 导入 Java 的 Map 接口,用于存储键值对,通常用于传递查询参数
|
||||
import java.util.Map;
|
||||
// 导入 Servlet 的 HttpServletRequest 类,用于处理 HTTP 请求
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入 Spring 框架的可空注解,表明参数或返回值可以为 null
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入 Java 的 List 接口,用于存储元素集合
|
||||
import java.util.List;
|
||||
|
||||
// 定义课程收藏服务接口,继承自 MyBatis-Plus 的 IService 接口
|
||||
public interface JianshenkechengCollectionService extends IService<JianshenkechengCollectionEntity> {
|
||||
|
||||
// 定义查询分页数据的方法,接收一个包含查询参数的 Map 对象
|
||||
// 返回值为 PageUtils 对象,包含分页信息和查询结果
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
// 声明该类所在的包为 com.service.impl
|
||||
package com.service.impl;
|
||||
|
||||
// 导入 com.utils 包下的 StringUtil 工具类
|
||||
import com.utils.StringUtil;
|
||||
// 导入 com.service 包下的 DictionaryService 接口
|
||||
import com.service.DictionaryService;
|
||||
// 导入 com.utils 包下的 ClazzDiff 工具类
|
||||
import com.utils.ClazzDiff;
|
||||
// 导入 Spring 框架的 BeanUtils 类,用于 Bean 属性的复制等操作
|
||||
import org.springframework.beans.BeanUtils;
|
||||
// 导入 Spring 框架的 Autowired 注解,用于自动装配依赖
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
// 导入 Spring 框架的 Service 注解,将该类标记为服务层组件
|
||||
import org.springframework.stereotype.Service;
|
||||
// 导入 Java 反射机制中的 Field 类,用于操作类的字段
|
||||
import java.lang.reflect.Field;
|
||||
// 导入 Java 集合框架的相关类,用于处理集合数据
|
||||
import java.util.*;
|
||||
// 导入 MyBatis-Plus 的 Page 类,用于分页操作
|
||||
import com.baomidou.mybatisplus.plugins.Page;
|
||||
// 导入 MyBatis-Plus 的 ServiceImpl 类,作为服务实现类的基类
|
||||
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
||||
// 导入 Spring 框架的 Transactional 注解,用于开启事务管理
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
// 导入自定义的 PageUtils 类,用于处理分页结果
|
||||
import com.utils.PageUtils;
|
||||
// 导入自定义的 Query 类,用于构建查询条件和分页信息
|
||||
import com.utils.Query;
|
||||
// 导入 Spring 框架的 ContextLoader 类,用于获取 Web 应用上下文
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
// 导入 Servlet 上下文类
|
||||
import javax.servlet.ServletContext;
|
||||
// 导入 HTTP 请求类
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入 Spring 框架的 Nullable 注解,标记参数或返回值可以为 null
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入 Spring 框架的 Assert 类,用于进行断言检查
|
||||
import org.springframework.util.Assert;
|
||||
// 导入课程收藏的数据访问对象接口
|
||||
import com.dao.JianshenkechengCollectionDao;
|
||||
// 导入课程收藏的实体类
|
||||
import com.entity.JianshenkechengCollectionEntity;
|
||||
// 导入课程收藏的服务接口
|
||||
import com.service.JianshenkechengCollectionService;
|
||||
// 导入课程收藏的视图类
|
||||
import com.entity.view.JianshenkechengCollectionView;
|
||||
|
||||
// 使用 Service 注解将该类注册为 Spring 服务组件,名称为 jianshenkechengCollectionService
|
||||
@Service("jianshenkechengCollectionService")
|
||||
// 使用 Transactional 注解开启事务管理
|
||||
@Transactional
|
||||
// 定义课程收藏服务实现类,继承自 MyBatis-Plus 的 ServiceImpl 类,并实现 JianshenkechengCollectionService 接口
|
||||
public class JianshenkechengCollectionServiceImpl extends ServiceImpl<JianshenkechengCollectionDao, JianshenkechengCollectionEntity> implements JianshenkechengCollectionService {
|
||||
|
||||
// 重写 JianshenkechengCollectionService 接口中的 queryPage 方法
|
||||
@Override
|
||||
// 实现分页查询课程收藏数据的方法,接收一个包含查询参数的 Map 对象
|
||||
public PageUtils queryPage(Map<String,Object> params) {
|
||||
// 根据传入的查询参数创建一个 JianshenkechengCollectionView 类型的分页对象
|
||||
Page<JianshenkechengCollectionView> page = new Query<JianshenkechengCollectionView>(params).getPage();
|
||||
// 调用数据访问对象的方法查询分页列表数据,并设置到分页对象中
|
||||
page.setRecords(baseMapper.selectListView(page, params));
|
||||
// 根据分页对象创建 PageUtils 对象并返回
|
||||
return new PageUtils(page);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// 定义当前接口所在的包路径
|
||||
package com.service;
|
||||
|
||||
// 导入MyBatis-Plus服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入分页工具类
|
||||
import com.utils.PageUtils;
|
||||
// 导入课程留言实体类
|
||||
import com.entity.JianshenkechengLiuyanEntity;
|
||||
// 导入Java集合框架中的Map接口
|
||||
import java.util.Map;
|
||||
// 导入HTTP请求对象
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入Spring空值注解
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入Java集合框架中的List接口
|
||||
import java.util.List;
|
||||
|
||||
// 课程留言服务接口
|
||||
// 继承MyBatis-Plus通用服务接口
|
||||
// 泛型参数为JianshenkechengLiuyanEntity课程留言实体类
|
||||
public interface JianshenkechengLiuyanService extends IService<JianshenkechengLiuyanEntity> {
|
||||
|
||||
// 分页查询课程留言数据方法
|
||||
// params - 包含查询条件的参数Map
|
||||
// 返回封装好的分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// 定义当前接口所在的包路径
|
||||
package com.service;
|
||||
|
||||
// 导入MyBatis-Plus服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入分页工具类
|
||||
import com.utils.PageUtils;
|
||||
// 导入教练实体类
|
||||
import com.entity.JiaolianEntity;
|
||||
// 导入Java集合框架中的Map接口
|
||||
import java.util.Map;
|
||||
// 导入HTTP请求对象
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入Spring空值注解
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入Java集合框架中的List接口
|
||||
import java.util.List;
|
||||
|
||||
// 教练服务接口定义
|
||||
// 继承MyBatis-Plus通用服务接口
|
||||
// 泛型参数为JiaolianEntity教练实体类
|
||||
public interface JiaolianService extends IService<JiaolianEntity> {
|
||||
|
||||
// 分页查询教练数据方法
|
||||
// params - 包含查询条件的参数Map
|
||||
// 返回封装好的分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// 指定该代码文件所属的包为 com.service
|
||||
package com.service;
|
||||
|
||||
// 导入 MyBatis-Plus 框架提供的 IService 接口,用于实现通用的单表业务逻辑
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入自定义的 PageUtils 类,用于处理分页查询结果
|
||||
import com.utils.PageUtils;
|
||||
// 导入教练预约申请实体类,代表数据库中教练预约申请的数据结构
|
||||
import com.entity.JiaolianYuyueEntity;
|
||||
// 导入 Map 类,用于存储键值对,可作为查询参数
|
||||
import java.util.Map;
|
||||
// 导入 HttpServletRequest 类,用于处理 HTTP 请求
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入 Spring 框架中的 Nullable 注解,表明参数或返回值可以为 null
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入 List 类,用于存储一组对象
|
||||
import java.util.List;
|
||||
|
||||
// 定义一个名为 JiaolianYuyueService 的接口,继承自 IService<JiaolianYuyueEntity>,表示这是教练预约申请的服务接口
|
||||
public interface JiaolianYuyueService extends IService<JiaolianYuyueEntity> {
|
||||
// 定义一个方法用于分页查询教练预约申请数据
|
||||
// params 参数为一个 Map 对象,存储了查询所需的条件
|
||||
// 返回值为 PageUtils 对象,包含了分页查询后的结果
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
}
|
@ -0,0 +1,179 @@
|
||||
package com.utils; // 声明工具类所在包
|
||||
|
||||
import java.util.Arrays; // 导入数组工具类
|
||||
import java.util.HashMap; // 导入HashMap类
|
||||
import java.util.Iterator; // 导入迭代器接口
|
||||
import java.util.Map; // 导入Map接口
|
||||
|
||||
import org.apache.commons.lang3.StringUtils; // 导入字符串工具类
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil; // 导入Bean工具类
|
||||
|
||||
import com.baomidou.mybatisplus.mapper.Wrapper; // 导入MyBatis-Plus条件构造器
|
||||
|
||||
public class MPUtil { // MyBatis-Plus工具类
|
||||
public static final char UNDERLINE = '_'; // 下划线常量
|
||||
|
||||
// 带前缀的AllEQ条件转换
|
||||
public static Map allEQMapPre(Object bean,String pre) {
|
||||
Map<String, Object> map =BeanUtil.beanToMap(bean); // 将Bean转为Map
|
||||
return camelToUnderlineMap(map,pre); // 转换键名并返回
|
||||
}
|
||||
|
||||
// AllEQ条件转换
|
||||
public static Map allEQMap(Object bean) {
|
||||
Map<String, Object> map =BeanUtil.beanToMap(bean); // 将Bean转为Map
|
||||
return camelToUnderlineMap(map,""); // 转换键名并返回
|
||||
}
|
||||
|
||||
// 带前缀的LIKE条件构造
|
||||
public static Wrapper allLikePre(Wrapper wrapper,Object bean,String pre) {
|
||||
Map<String, Object> map =BeanUtil.beanToMap(bean); // 将Bean转为Map
|
||||
Map result = camelToUnderlineMap(map,pre); // 转换键名
|
||||
return genLike(wrapper,result); // 生成LIKE条件
|
||||
}
|
||||
|
||||
// LIKE条件构造
|
||||
public static Wrapper allLike(Wrapper wrapper,Object bean) {
|
||||
Map result = BeanUtil.beanToMap(bean, true, true); // 将Bean转为Map(忽略空值)
|
||||
return genLike(wrapper,result); // 生成LIKE条件
|
||||
}
|
||||
|
||||
// 生成LIKE条件
|
||||
public static Wrapper genLike(Wrapper wrapper,Map param) {
|
||||
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator(); // 获取迭代器
|
||||
int i=0; // 计数器
|
||||
while (it.hasNext()) { // 遍历Map
|
||||
if(i>0) wrapper.and(); // 多个条件用AND连接
|
||||
Map.Entry<String, Object> entry = it.next(); // 获取键值对
|
||||
String key = entry.getKey(); // 获取字段名
|
||||
String value = (String) entry.getValue(); // 获取字段值
|
||||
wrapper.like(key, value); // 添加LIKE条件
|
||||
i++; // 计数器递增
|
||||
}
|
||||
return wrapper; // 返回条件构造器
|
||||
}
|
||||
|
||||
// LIKE或EQ条件构造
|
||||
public static Wrapper likeOrEq(Wrapper wrapper,Object bean) {
|
||||
Map result = BeanUtil.beanToMap(bean, true, true); // 将Bean转为Map(忽略空值)
|
||||
return genLikeOrEq(wrapper,result); // 生成LIKE或EQ条件
|
||||
}
|
||||
|
||||
// 生成LIKE或EQ条件
|
||||
public static Wrapper genLikeOrEq(Wrapper wrapper,Map param) {
|
||||
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator(); // 获取迭代器
|
||||
int i=0; // 计数器
|
||||
while (it.hasNext()) { // 遍历Map
|
||||
if(i>0) wrapper.and(); // 多个条件用AND连接
|
||||
Map.Entry<String, Object> entry = it.next(); // 获取键值对
|
||||
String key = entry.getKey(); // 获取字段名
|
||||
if(entry.getValue().toString().contains("%")) { // 判断是否包含%通配符
|
||||
wrapper.like(key, entry.getValue().toString().replace("%", "")); // 添加LIKE条件
|
||||
} else {
|
||||
wrapper.eq(key, entry.getValue()); // 添加EQ条件
|
||||
}
|
||||
i++; // 计数器递增
|
||||
}
|
||||
return wrapper; // 返回条件构造器
|
||||
}
|
||||
|
||||
// 全等条件构造
|
||||
public static Wrapper allEq(Wrapper wrapper,Object bean) {
|
||||
Map result = BeanUtil.beanToMap(bean, true, true); // 将Bean转为Map(忽略空值)
|
||||
return genEq(wrapper,result); // 生成EQ条件
|
||||
}
|
||||
|
||||
// 生成EQ条件
|
||||
public static Wrapper genEq(Wrapper wrapper,Map param) {
|
||||
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator(); // 获取迭代器
|
||||
int i=0; // 计数器
|
||||
while (it.hasNext()) { // 遍历Map
|
||||
if(i>0) wrapper.and(); // 多个条件用AND连接
|
||||
Map.Entry<String, Object> entry = it.next(); // 获取键值对
|
||||
String key = entry.getKey(); // 获取字段名
|
||||
wrapper.eq(key, entry.getValue()); // 添加EQ条件
|
||||
i++; // 计数器递增
|
||||
}
|
||||
return wrapper; // 返回条件构造器
|
||||
}
|
||||
|
||||
// BETWEEN条件构造
|
||||
public static Wrapper between(Wrapper wrapper,Map<String, Object> params) {
|
||||
for(String key : params.keySet()) { // 遍历参数Map
|
||||
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; // 返回条件构造器
|
||||
}
|
||||
|
||||
// 驼峰转下划线方法
|
||||
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")); // 测试驼峰转下划线
|
||||
}
|
||||
|
||||
// Map键名驼峰转下划线
|
||||
public static Map camelToUnderlineMap(Map param, String pre) {
|
||||
Map<String, Object> newMap = new HashMap<String, Object>(); // 创建新Map
|
||||
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator(); // 获取迭代器
|
||||
while (it.hasNext()) { // 遍历原Map
|
||||
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; // 返回新Map
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
// 声明该类所在的包为 com.thread
|
||||
package com.thread;
|
||||
|
||||
// 定义一个名为 MyThreadMethod 的类,继承自 Thread 类,用于实现线程操作
|
||||
// 此类的作用是执行一些项目启动后需要一直运行的操作,如根据时间自动更改订单状态等
|
||||
public class MyThreadMethod extends Thread {
|
||||
// 重写 Thread 类的 run 方法,该方法是线程启动后执行的主体逻辑
|
||||
public void run() {
|
||||
// 当线程未被中断时,持续执行循环
|
||||
while (!this.isInterrupted()) {
|
||||
try {
|
||||
// 使当前线程休眠 5000 毫秒(即 5 秒),也就是每隔 5 秒执行一次后续操作
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
// 若线程在休眠过程中被中断,打印异常堆栈信息
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 这里可以添加需要定时执行的具体业务逻辑,例如根据时间自动更改订单状态等操作
|
||||
// 以下是一个示例打印语句,可用于调试,显示当前线程正在执行以及当前的时间戳
|
||||
// System.out.println("线程执行中:" + System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// 定义当前接口所在的包路径
|
||||
package com.service;
|
||||
|
||||
// 导入MyBatis-Plus服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入分页工具类
|
||||
import com.utils.PageUtils;
|
||||
// 导入健身资讯实体类
|
||||
import com.entity.NewsEntity;
|
||||
// 导入Java集合框架中的Map接口
|
||||
import java.util.Map;
|
||||
// 导入HTTP请求对象
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入Spring空值注解
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入Java集合框架中的List接口
|
||||
import java.util.List;
|
||||
|
||||
// 健身资讯服务接口定义
|
||||
// 继承MyBatis-Plus通用服务接口
|
||||
// 泛型参数为NewsEntity健身资讯实体类
|
||||
public interface NewsService extends IService<NewsEntity> {
|
||||
|
||||
// 分页查询健身资讯数据方法
|
||||
// params - 包含查询条件的参数Map
|
||||
// 返回封装好的分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.utils; // 声明工具类所在包
|
||||
|
||||
import org.apache.commons.io.FileUtils; // 导入文件工具类
|
||||
import org.apache.poi.hssf.usermodel.HSSFCell; // 导入HSSF单元格类
|
||||
import org.apache.poi.hssf.usermodel.HSSFRow; // 导入HSSF行类
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet; // 导入HSSF工作表类
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; // 导入HSSF工作簿类
|
||||
import org.apache.poi.ss.usermodel.Cell; // 导入单元格接口
|
||||
|
||||
import java.io.File; // 导入文件类
|
||||
import java.io.FileOutputStream; // 导入文件输出流类
|
||||
import java.util.ArrayList; // 导入ArrayList类
|
||||
import java.util.List; // 导入List接口
|
||||
|
||||
public class PoiUtil { // POI工具类
|
||||
|
||||
// Excel导入方法
|
||||
public static List<List<String>> poiImport(String url) throws Exception {
|
||||
List<List<String>> list = new ArrayList<>(); // 创建结果列表
|
||||
HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(new File(url))); // 创建工作簿对象
|
||||
HSSFSheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
|
||||
for (int i = 0; i < sheet.getLastRowNum()+1; i++) { // 遍历所有行
|
||||
HSSFRow row = sheet.getRow(i); // 获取当前行
|
||||
List<String> rowlist = new ArrayList<>(); // 创建行数据列表
|
||||
for (int j = 0; j < row.getLastCellNum(); j++) { // 遍历行中所有单元格
|
||||
HSSFCell cell = row.getCell(j); // 获取当前单元格
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING); // 设置单元格类型为字符串
|
||||
String value = cell.getStringCellValue(); // 获取单元格值
|
||||
rowlist.add(value); // 将值添加到行数据列表
|
||||
}
|
||||
list.add(rowlist); // 将行数据添加到结果列表
|
||||
}
|
||||
return list; // 返回结果列表
|
||||
}
|
||||
|
||||
// Excel导出方法
|
||||
public static void poiExport(List<List<String>> list, String url) throws Exception {
|
||||
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
|
||||
HSSFSheet sheet = workbook.createSheet(); // 创建工作表
|
||||
for (int i = 0; i < list.size(); i++) { // 遍历所有行数据
|
||||
HSSFRow row = sheet.createRow(i); // 创建行
|
||||
List<String> dataList = list.get(i); // 获取当前行数据
|
||||
for (int j = 0; j < dataList.size(); j++) { // 遍历行中所有数据
|
||||
HSSFCell cell = row.createCell(j); // 创建单元格
|
||||
cell.setCellValue(dataList.get(j)); // 设置单元格值
|
||||
}
|
||||
}
|
||||
FileOutputStream stream = FileUtils.openOutputStream(new File(url)); // 创建文件输出流
|
||||
workbook.write(stream); // 写入工作簿数据
|
||||
stream.close(); // 关闭输出流
|
||||
}
|
||||
|
||||
// 主方法(测试用)
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
List<List<String>> lists = PoiUtil.poiImport("C:/Users/Administrator/Desktop/工作1.xls"); // 测试导入
|
||||
PoiUtil.poiExport(lists, "C:/Users/Administrator/Desktop/工作1.xls"); // 测试导出
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(); // 打印异常信息
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// 声明该类所在的包为 com.service
|
||||
package com.service;
|
||||
|
||||
// 导入 MyBatis-Plus 提供的 IService 接口,此接口可用于实现通用的 CRUD 操作
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入自定义的 PageUtils 类,用于处理分页查询相关的逻辑
|
||||
import com.utils.PageUtils;
|
||||
// 导入单页数据实体类,该类用于表示单页数据的结构
|
||||
import com.entity.SingleSeachEntity;
|
||||
// 导入 Map 类,可用于存储键值对,通常用于传递查询参数
|
||||
import java.util.Map;
|
||||
// 导入 HttpServletRequest 类,用于处理 HTTP 请求相关操作
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入 Spring 框架中的 Nullable 注解,用于标记参数或返回值可以为 null
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入 List 类,用于存储一组对象
|
||||
import java.util.List;
|
||||
|
||||
// 定义单页数据服务接口,继承自 IService<SingleSeachEntity>
|
||||
public interface SingleSeachService extends IService<SingleSeachEntity> {
|
||||
// 定义一个方法用于根据传入的查询参数进行分页查询
|
||||
// params 是一个 Map 类型的参数,用于存储查询条件
|
||||
// 返回值是 PageUtils 类型,包含了分页查询后的结果
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
// 声明类所在的包为 com.utils
|
||||
package com.utils;
|
||||
|
||||
// 导入 Spring 框架中处理 Bean 相关异常的类
|
||||
import org.springframework.beans.BeansException;
|
||||
// 导入 Spring 框架的应用上下文接口,代表 Spring 应用的上下文环境
|
||||
import org.springframework.context.ApplicationContext;
|
||||
// 导入 Spring 框架的应用上下文感知接口,实现该接口可获取应用上下文
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
// 导入 Spring 框架的组件注解,用于将类标记为 Spring 组件
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
// 使用 @Component 注解将该类标记为 Spring 组件,使其能被 Spring 自动扫描和管理
|
||||
@Component
|
||||
// 定义 SpringContextUtils 类,实现 ApplicationContextAware 接口以获取 Spring 应用上下文
|
||||
public class SpringContextUtils implements ApplicationContextAware {
|
||||
// 定义一个静态的 ApplicationContext 类型变量,用于存储 Spring 应用上下文
|
||||
public static ApplicationContext applicationContext;
|
||||
|
||||
// 重写 ApplicationContextAware 接口的方法,当 Spring 容器初始化时会调用此方法
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
// 将传入的应用上下文赋值给类的静态变量,方便后续静态方法使用
|
||||
SpringContextUtils.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
// 定义静态方法,根据 Bean 的名称从应用上下文中获取对应的 Bean 实例
|
||||
public static Object getBean(String name) {
|
||||
// 调用应用上下文的 getBean 方法,通过名称获取 Bean 实例
|
||||
return applicationContext.getBean(name);
|
||||
}
|
||||
|
||||
// 定义泛型静态方法,根据 Bean 的名称和所需类型从应用上下文中获取指定类型的 Bean 实例
|
||||
public static <T> T getBean(String name, Class<T> requiredType) {
|
||||
// 调用应用上下文的 getBean 方法,通过名称和类型获取 Bean 实例
|
||||
return applicationContext.getBean(name, requiredType);
|
||||
}
|
||||
|
||||
// 定义静态方法,用于判断应用上下文中是否包含指定名称的 Bean
|
||||
public static boolean containsBean(String name) {
|
||||
// 调用应用上下文的 containsBean 方法进行判断
|
||||
return applicationContext.containsBean(name);
|
||||
}
|
||||
|
||||
// 定义静态方法,用于判断指定名称的 Bean 是否为单例模式
|
||||
public static boolean isSingleton(String name) {
|
||||
// 调用应用上下文的 isSingleton 方法进行判断
|
||||
return applicationContext.isSingleton(name);
|
||||
}
|
||||
|
||||
// 定义静态方法,用于获取指定名称的 Bean 的类型
|
||||
public static Class<? extends Object> getType(String name) {
|
||||
// 调用应用上下文的 getType 方法获取 Bean 的类型
|
||||
return applicationContext.getType(name);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
// 声明该接口所在的包为 com.service
|
||||
package com.service;
|
||||
|
||||
// 导入 List 类,用于存储一组对象
|
||||
import java.util.List;
|
||||
// 导入 Map 类,用于存储键值对,通常用于传递查询参数
|
||||
import java.util.Map;
|
||||
|
||||
// 导入 MyBatis-Plus 的 Wrapper 类,用于构建查询条件
|
||||
import com.baomidou.mybatisplus.mapper.Wrapper;
|
||||
// 导入 MyBatis-Plus 的 IService 接口,提供通用的单表业务方法
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入 Token 实体类,代表数据库中的 Token 数据
|
||||
import com.entity.TokenEntity;
|
||||
// 导入自定义的 PageUtils 类,用于处理分页查询结果
|
||||
import com.utils.PageUtils;
|
||||
|
||||
// 定义 Token 服务接口,继承自 IService<TokenEntity>
|
||||
public interface TokenService extends IService<TokenEntity> {
|
||||
// 定义分页查询方法,根据传入的查询参数进行分页查询
|
||||
// params 参数为一个 Map 对象,存储了查询所需的条件
|
||||
// 返回值为 PageUtils 对象,包含了分页查询后的结果
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
// 定义查询列表视图的方法,根据传入的查询条件包装器查询 Token 实体列表
|
||||
// wrapper 参数为 Wrapper<TokenEntity> 类型,用于构建查询条件
|
||||
// 返回值为 List<TokenEntity> 类型,包含符合条件的 Token 实体列表
|
||||
List<TokenEntity> selectListView(Wrapper<TokenEntity> wrapper);
|
||||
|
||||
// 定义带查询条件包装器的分页查询方法,根据传入的查询参数和查询条件包装器进行分页查询
|
||||
// params 参数为一个 Map 对象,存储了查询所需的条件
|
||||
// wrapper 参数为 Wrapper<TokenEntity> 类型,用于构建查询条件
|
||||
// 返回值为 PageUtils 对象,包含了分页查询后的结果
|
||||
PageUtils queryPage(Map<String, Object> params, Wrapper<TokenEntity> wrapper);
|
||||
|
||||
// 定义生成 Token 的方法,根据传入的用户 ID、用户名、表名和角色生成一个 Token 字符串
|
||||
// userid 参数为 Integer 类型,表示用户的 ID
|
||||
// username 参数为 String 类型,表示用户的名称
|
||||
// tableName 参数为 String 类型,表示相关表的名称
|
||||
// role 参数为 String 类型,表示用户的角色
|
||||
// 返回值为 String 类型,即生成的 Token 字符串
|
||||
String generateToken(Integer userid, String username, String tableName, String role);
|
||||
|
||||
// 定义根据 Token 字符串获取 Token 实体的方法
|
||||
// token 参数为 String 类型,表示要查询的 Token 字符串
|
||||
// 返回值为 TokenEntity 类型,即符合条件的 Token 实体
|
||||
TokenEntity getTokenEntity(String token);
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
// 声明该类所在的包为 com.service.impl
|
||||
package com.service.impl;
|
||||
|
||||
// 导入 Java 中的 Calendar 类,用于处理日期和时间
|
||||
import java.util.Calendar;
|
||||
// 导入 Java 中的 Date 类,用于表示日期和时间
|
||||
import java.util.Date;
|
||||
// 导入 Java 中的 List 接口,用于存储一组对象
|
||||
import java.util.List;
|
||||
// 导入 Java 中的 Map 接口,用于存储键值对
|
||||
import java.util.Map;
|
||||
|
||||
// 导入 Spring 框架的 Service 注解,用于将该类标记为服务层组件
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
// 导入 MyBatis-Plus 的 EntityWrapper 类,用于构建 SQL 查询条件
|
||||
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
||||
// 导入 MyBatis-Plus 的 Wrapper 接口,是 EntityWrapper 的父接口
|
||||
import com.baomidou.mybatisplus.mapper.Wrapper;
|
||||
// 导入 MyBatis-Plus 的 Page 类,用于分页查询
|
||||
import com.baomidou.mybatisplus.plugins.Page;
|
||||
// 导入 MyBatis-Plus 的 ServiceImpl 类,作为服务实现类的基类
|
||||
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
||||
// 导入 Token 数据访问对象接口
|
||||
import com.dao.TokenDao;
|
||||
// 导入 Token 实体类
|
||||
import com.entity.TokenEntity;
|
||||
// 重复导入 Token 实体类,此处可删除该重复导入
|
||||
import com.entity.TokenEntity;
|
||||
// 导入 Token 服务接口
|
||||
import com.service.TokenService;
|
||||
// 导入自定义的通用工具类
|
||||
import com.utils.CommonUtil;
|
||||
// 导入自定义的分页工具类
|
||||
import com.utils.PageUtils;
|
||||
// 导入自定义的查询工具类
|
||||
import com.utils.Query;
|
||||
|
||||
// 使用 Service 注解将该类标记为 Spring 服务组件,名称为 tokenService
|
||||
@Service("tokenService")
|
||||
// 定义 Token 服务实现类,继承自 MyBatis-Plus 的 ServiceImpl 类,并实现 TokenService 接口
|
||||
public class TokenServiceImpl extends ServiceImpl<TokenDao, TokenEntity> implements TokenService {
|
||||
|
||||
// 重写 TokenService 接口中的 queryPage 方法,用于分页查询 Token 数据
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params) {
|
||||
// 根据传入的参数创建一个 TokenEntity 类型的分页对象
|
||||
Page<TokenEntity> page = this.selectPage(
|
||||
// 通过 Query 工具类根据参数获取分页信息
|
||||
new Query<TokenEntity>(params).getPage(),
|
||||
// 创建一个空的查询条件包装器
|
||||
new EntityWrapper<TokenEntity>()
|
||||
);
|
||||
// 根据分页对象创建 PageUtils 对象并返回
|
||||
return new PageUtils(page);
|
||||
}
|
||||
|
||||
// 重写 TokenService 接口中的 selectListView 方法,用于查询 Token 列表数据
|
||||
@Override
|
||||
public List<TokenEntity> selectListView(Wrapper<TokenEntity> wrapper) {
|
||||
// 调用数据访问对象的 selectListView 方法,根据查询条件包装器查询列表数据并返回
|
||||
return baseMapper.selectListView(wrapper);
|
||||
}
|
||||
|
||||
// 重写 TokenService 接口中的 queryPage 方法,支持带查询条件的分页查询 Token 数据
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params,
|
||||
Wrapper<TokenEntity> wrapper) {
|
||||
// 根据传入的参数创建一个 TokenEntity 类型的分页对象
|
||||
Page<TokenEntity> page = new Query<TokenEntity>(params).getPage();
|
||||
// 调用数据访问对象的 selectListView 方法,根据分页对象和查询条件包装器查询列表数据并设置到分页对象中
|
||||
page.setRecords(baseMapper.selectListView(page, wrapper));
|
||||
// 根据分页对象创建 PageUtils 对象
|
||||
PageUtils pageUtil = new PageUtils(page);
|
||||
// 返回 PageUtils 对象
|
||||
return pageUtil;
|
||||
}
|
||||
|
||||
// 重写 TokenService 接口中的 generateToken 方法,用于生成 Token
|
||||
@Override
|
||||
public String generateToken(Integer userid, String username, String tableName, String role) {
|
||||
// 根据用户 ID 和角色查询 Token 实体对象
|
||||
TokenEntity tokenEntity = this.selectOne(new EntityWrapper<TokenEntity>().eq("userid", userid).eq("role", role));
|
||||
// 调用通用工具类的方法生成一个 32 位的随机字符串作为 Token
|
||||
String token = CommonUtil.getRandomString(32);
|
||||
// 获取当前时间的 Calendar 实例
|
||||
Calendar cal = Calendar.getInstance();
|
||||
// 设置 Calendar 的时间为当前日期和时间
|
||||
cal.setTime(new Date());
|
||||
// 将 Calendar 的时间增加 1 小时,作为 Token 的过期时间
|
||||
cal.add(Calendar.HOUR_OF_DAY, 1);
|
||||
// 如果查询到了 Token 实体对象
|
||||
if (tokenEntity != null) {
|
||||
// 更新 Token 实体对象的 Token 字段
|
||||
tokenEntity.setToken(token);
|
||||
// 更新 Token 实体对象的过期时间字段
|
||||
tokenEntity.setExpiratedtime(cal.getTime());
|
||||
// 调用更新方法更新 Token 实体对象
|
||||
this.updateById(tokenEntity);
|
||||
} else {
|
||||
// 如果未查询到 Token 实体对象,创建一个新的 Token 实体对象并插入数据库
|
||||
this.insert(new TokenEntity(userid, username, tableName, role, token, cal.getTime()));
|
||||
}
|
||||
// 返回生成的 Token
|
||||
return token;
|
||||
}
|
||||
|
||||
// 重写 TokenService 接口中的 getTokenEntity 方法,用于根据 Token 获取 Token 实体对象
|
||||
@Override
|
||||
public TokenEntity getTokenEntity(String token) {
|
||||
// 根据 Token 查询 Token 实体对象
|
||||
TokenEntity tokenEntity = this.selectOne(new EntityWrapper<TokenEntity>().eq("token", token));
|
||||
// 如果未查询到 Token 实体对象或者 Token 已过期
|
||||
if (tokenEntity == null || tokenEntity.getExpiratedtime().getTime() < new Date().getTime()) {
|
||||
// 返回 null
|
||||
return null;
|
||||
}
|
||||
// 返回查询到的 Token 实体对象
|
||||
return tokenEntity;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
// 定义当前接口所在的包路径
|
||||
package com.service;
|
||||
|
||||
// 导入Java集合框架中的List接口
|
||||
import java.util.List;
|
||||
// 导入Java集合框架中的Map接口
|
||||
import java.util.Map;
|
||||
|
||||
// 导入MyBatis-Plus条件构造器
|
||||
import com.baomidou.mybatisplus.mapper.Wrapper;
|
||||
// 导入MyBatis-Plus服务接口
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入用户实体类
|
||||
import com.entity.UsersEntity;
|
||||
// 导入分页工具类
|
||||
import com.utils.PageUtils;
|
||||
|
||||
// 系统用户服务接口
|
||||
// 继承MyBatis-Plus通用服务接口
|
||||
// 泛型参数为UsersEntity用户实体类
|
||||
public interface UsersService extends IService<UsersEntity> {
|
||||
|
||||
// 分页查询用户数据方法(基础版)
|
||||
// params - 包含查询条件的参数Map
|
||||
// 返回封装好的分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
|
||||
// 根据条件查询用户列表方法
|
||||
// wrapper - 查询条件构造器
|
||||
// 返回用户实体列表
|
||||
List<UsersEntity> selectListView(Wrapper<UsersEntity> wrapper);
|
||||
|
||||
// 分页查询用户数据方法(增强版)
|
||||
// params - 包含查询条件的参数Map
|
||||
// wrapper - 查询条件构造器
|
||||
// 返回封装好的分页工具对象
|
||||
PageUtils queryPage(Map<String, Object> params, Wrapper<UsersEntity> wrapper);
|
||||
|
@ -0,0 +1,70 @@
|
||||
// 声明类所在的包为 com.service.impl
|
||||
package com.service.impl;
|
||||
|
||||
// 导入 Java 中的 List 接口,用于存储元素集合
|
||||
import java.util.List;
|
||||
// 导入 Java 中的 Map 接口,用于存储键值对
|
||||
import java.util.Map;
|
||||
|
||||
// 导入系统用户服务接口
|
||||
import com.service.UsersService;
|
||||
// 导入 Spring 框架的 Service 注解,用于将类标记为服务层组件
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
// 导入 MyBatis-Plus 的 EntityWrapper 类,用于构建查询条件
|
||||
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
||||
// 导入 MyBatis-Plus 的 Wrapper 接口,是 EntityWrapper 的父接口
|
||||
import com.baomidou.mybatisplus.mapper.Wrapper;
|
||||
// 导入 MyBatis-Plus 的 Page 类,用于分页查询
|
||||
import com.baomidou.mybatisplus.plugins.Page;
|
||||
// 导入 MyBatis-Plus 的 ServiceImpl 类,作为服务实现类的基类
|
||||
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
||||
// 导入系统用户数据访问对象接口
|
||||
import com.dao.UsersDao;
|
||||
// 导入系统用户实体类
|
||||
import com.entity.UsersEntity;
|
||||
// 导入自定义的分页工具类
|
||||
import com.utils.PageUtils;
|
||||
// 导入自定义的查询工具类
|
||||
import com.utils.Query;
|
||||
|
||||
// 使用 Service 注解将该类标记为 Spring 服务组件,名称为 userService
|
||||
@Service("userService")
|
||||
// 定义系统用户服务实现类,继承自 MyBatis-Plus 的 ServiceImpl 类,并实现 UsersService 接口
|
||||
public class UsersServiceImpl extends ServiceImpl<UsersDao, UsersEntity> implements UsersService {
|
||||
|
||||
// 重写 UsersService 接口中的 queryPage 方法,用于分页查询系统用户数据
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params) {
|
||||
// 根据传入的参数创建一个 UsersEntity 类型的分页对象
|
||||
Page<UsersEntity> page = this.selectPage(
|
||||
// 通过 Query 工具类根据参数获取分页信息
|
||||
new Query<UsersEntity>(params).getPage(),
|
||||
// 创建一个空的查询条件包装器
|
||||
new EntityWrapper<UsersEntity>()
|
||||
);
|
||||
// 根据分页对象创建 PageUtils 对象并返回
|
||||
return new PageUtils(page);
|
||||
}
|
||||
|
||||
// 重写 UsersService 接口中的 selectListView 方法,用于查询系统用户列表数据
|
||||
@Override
|
||||
public List<UsersEntity> selectListView(Wrapper<UsersEntity> wrapper) {
|
||||
// 调用数据访问对象的 selectListView 方法,根据查询条件包装器查询列表数据并返回
|
||||
return baseMapper.selectListView(wrapper);
|
||||
}
|
||||
|
||||
// 重写 UsersService 接口中的 queryPage 方法,支持带查询条件的分页查询系统用户数据
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params,
|
||||
Wrapper<UsersEntity> wrapper) {
|
||||
// 根据传入的参数创建一个 UsersEntity 类型的分页对象
|
||||
Page<UsersEntity> page = new Query<UsersEntity>(params).getPage();
|
||||
// 调用数据访问对象的 selectListView 方法,根据分页对象和查询条件包装器查询列表数据并设置到分页对象中
|
||||
page.setRecords(baseMapper.selectListView(page, wrapper));
|
||||
// 根据分页对象创建 PageUtils 对象
|
||||
PageUtils pageUtil = new PageUtils(page);
|
||||
// 返回 PageUtils 对象
|
||||
return pageUtil;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// 指定该代码文件所属的包名为 com.service
|
||||
package com.service;
|
||||
|
||||
// 导入 MyBatis-Plus 提供的 IService 接口,用于实现通用的单表业务逻辑操作
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
// 导入自定义的 PageUtils 类,用于处理分页查询的结果
|
||||
import com.utils.PageUtils;
|
||||
// 导入用户实体类,该类对应数据库中用户表的数据结构
|
||||
import com.entity.YonghuEntity;
|
||||
// 导入 Map 类,可用于存储键值对,通常用来作为查询参数的容器
|
||||
import java.util.Map;
|
||||
// 导入 HttpServletRequest 类,用于处理 HTTP 请求相关的操作
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
// 导入 Spring 框架的 Nullable 注解,表明参数或返回值可以为 null
|
||||
import org.springframework.lang.Nullable;
|
||||
// 导入 List 类,用于存储一组对象
|
||||
import java.util.List;
|
||||
|
||||
// 定义一个名为 YonghuService 的接口,继承自 IService<YonghuEntity>,表明这是一个用户服务接口
|
||||
public interface YonghuService extends IService<YonghuEntity> {
|
||||
// 定义一个方法用于根据传入的查询参数进行分页查询用户数据
|
||||
// params 是一个 Map 类型的参数,存储了查询所需的各种条件
|
||||
// 返回值是 PageUtils 类型,包含了分页查询后的结果数据
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
}
|
Loading…
Reference in new issue