一 日志功能,實(shí)現(xiàn)將用戶操作日志保存到數(shù)據(jù)庫
1.準(zhǔn)備工作
新建ssm項(xiàng)目,添加依賴,在spring-mvc.xml添加aop相關(guān)配置
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- 啟用 aspectj 方式 AOP-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
2.實(shí)現(xiàn)
2.1 自定義注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
/**
* 操作事件
*/
String value();
}
2.2 日志類
public class UserLog {
//日志id
private Integer id;
//用戶id
private Integer userId;
//用戶ip
private String ip;
//操作時(shí)間
private Date createdTime;
//請(qǐng)求方法
private String method;
//請(qǐng)求url
private String requestUrl;
//返回結(jié)果
private String result;
//get和set方法
...
}
數(shù)據(jù)庫字段和實(shí)體類對(duì)應(yīng)即可
2.3 數(shù)據(jù)庫mapper
public interface UserLogMapper {
int deleteByPrimaryKey(Integer logId);
int insert(UserLog userLog);
UserLog selectByPrimaryKey(Integer logId);
List<UserLog> selectAll();
int updateByPrimaryKey(UserLog record);
/**
* 查詢某一個(gè)時(shí)間段的數(shù)據(jù)
* @param start 開始的時(shí)間
* @param stop 結(jié)束的時(shí)間
* */
List<UserLog> selectUserByPeriod(@Param("start") String start, @Param("stop") String stop);
}
2.5 service
@Aspect
@Component
public class LogServiceImpl implements LogService {
/*
* 獲取入?yún)⒑统鰠? * @param joinPoint
* @param o
* @return
* */
@Override
public void getByJoinPoint(JoinPoint joinPoint, Object o) throws ClassNotFoundException {
StringBuffer operateEvent = new StringBuffer();
String targetName = joinPoint.getTarget().getClass().getName(); // 請(qǐng)求類名稱
String methodName = joinPoint.getSignature().getName(); // 請(qǐng)求方法
Object[] arguments = joinPoint.getArgs(); //獲取入?yún)? UserLog userLog = new log();
User loginUser = (User) session.getAttribute("loginUser");
log.setUserId(loginUser.getId());
//getHos()是獲取客戶端ip的方法
log.setLogIp(getHost(request));
log.setLogReqUrl(request.getRequestURI());
log.setLogMethod(joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName());
// 從o中讀取提取返回結(jié)果
//String result =
log.setLogResult(result);
//保存到數(shù)據(jù)庫
int insert = mapper.insert(log);
2.4 LogAspect.java:日志切面
@Aspect
@Component
@Order(3)
public class LogAspect {
// 注入Service用于把日志保存數(shù)據(jù)庫
@Autowired
private LogService logService;
// 切點(diǎn)
//@annotation用于匹配當(dāng)前執(zhí)行方法持有指定注解的方法;
@Pointcut("@annotation(com.cateringsystem.service.web.log.Log)")
public void logAspect() {
}
@Around("logAspect()")
public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = joinPoint.proceed();
logService.getByJoinPoint(joinPoint, result);
return result;
}
}
2.5 在需要生成日志的Controller上加上@Log注解,當(dāng)然在service層加注解也可以
@RestController
public class UserController {
@Log(value = " ")
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public String getUser(Model model,String parameter1,Integer parameter2) {
...
}
}
3.測試
運(yùn)行項(xiàng)目,請(qǐng)求getUser這個(gè)方法,數(shù)據(jù)庫就多了一條記錄.
參考https://my.oschina.net/u/3136014/blog/904643#comments
二 文件上傳下載
1 上傳文件
/**
* form 表單上傳圖片/文件的方法
*
* @param savePath 獲取圖片/文件保存路徑
* @param multipartFile 文件上傳對(duì)象
*/
public static String setMultipartFile(String savePath, MultipartFile multipartFile) throws IOException {
// 獲取文件對(duì)象
File file = new File(savePath);
// 獲取路徑
String basePath = file.getPath();
// 重命名后的文件
String relativePath = getMakeRelativePath(multipartFile.getOriginalFilename());
// 創(chuàng)建一個(gè)文件
File target = new File(basePath.concat(relativePath));
target.getParentFile();
// 轉(zhuǎn)移圖片數(shù)據(jù)
multipartFile.transferTo(target);
String fileName = target.getName();
return fileName;
}
- controller接口可參照下文的wangEditor實(shí)現(xiàn)富文本編輯的controller接口
2 下載文件
@RequestMapping(value = "/pic/{id}", method = RequestMethod.GET)
public Result getPic(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
//根據(jù)id獲取圖片名
Test test = deviceService.getById(id);
String fileName = test .getPic;
//從本地獲取圖片损肛,實(shí)際開發(fā)中需要從服務(wù)器獲取
File file = new File(“f:/test/”.concat(fileName));
long fileSize = file.length();
// 重置response
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes(), "GBK"));
response.addHeader("Content-Length", String.valueOf(fileSize));
// 讀取并以流的方式輸出
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = (bis.read(buffer))) != -1) {
bos.write(buffer, 0, length);
}
bos.flush();
bos.close();
bis.close();
return null;
}
三 wangEditor實(shí)現(xiàn)富文本編輯
官方文檔
這個(gè)富文本編輯器使用中最需要注意的是上傳圖片功能悠咱。服務(wù)器配置如下:
1 返回類
官方文檔給出了這樣的說明:https://www.kancloud.cn/wangfupeng/wangeditor3/335782
首先我們需要定義一個(gè)返回類,返回類必須是下面的格式贾虽。
@Data
public class ImgResult<T> {
/**
* 錯(cuò)誤碼
*/
private int errno;
/**
* data 是一個(gè)數(shù)組逃糟,返回若干圖片的線上地址
*/
private String[] data;
}
2 controller接口寫法
@RequestMapping(value = "/uploads", method = RequestMethod.POST)
public ImgResult upload(@RequestParam(value = "files", required = false) MultipartFile[] files) throws IOException {
ImgResult result = new ImgResult();
// form 表單上傳多張圖片/文件的方法
List<String> picPath = Upload.setMultipartFiles("F:/test/", files);
int index = 0;
String[] url = new String[picPath.size()];
for (String s : picPath) {
url[index++] = s;
}
result.setData(url);
return result;
}
四 quartz定時(shí)任務(wù)
quartz定時(shí)任務(wù)基礎(chǔ)知識(shí)可查看https://blog.csdn.net/noaman_wgs/article/details/80984873
定時(shí)任務(wù)相關(guān)內(nèi)容網(wǎng)上資料很多,不再多做說明蓬豁。