This commit is contained in:
xtaodada 2024-05-15 16:32:18 +08:00
parent f5a216cb27
commit 7c889fed2a
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
8 changed files with 235 additions and 1 deletions

18
pom.xml
View File

@ -96,6 +96,24 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>smartupload</groupId>
<artifactId>smartupload</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/jspsmartupload.jar</systemPath>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,110 @@
package cn.edu.cqwu.repair.controller.admin;
import cn.edu.cqwu.repair.db.ConnectionFactory;
import cn.edu.cqwu.repair.util.Message;
import com.jspsmart.upload.SmartUpload;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author xtaod
*/
@Controller
public class AdminDBController implements ServletConfigAware {
@RequestMapping("/admin/db/backup.do")
private void backUp(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletContext context = request.getServletContext();
String path = context.getRealPath("/admin/backup/");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
HttpSession session = request.getSession();
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String time = format.format(date);
Runtime runtime = Runtime.getRuntime();
String command = "cmd /c mysqldump -u" + ConnectionFactory.USER + " -p" + ConnectionFactory.PASSWORD + " -h" + ConnectionFactory.HOST + " --column-statistics=0 " + ConnectionFactory.DATABASE + " > " + file.getAbsolutePath() + "\\" + time + ".sql";
Process process = runtime.exec(command);
try {
int tag = process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (tag == 0) {
session.setAttribute("mess", new Message("backupMess", "备份成功"));
} else {
session.setAttribute("mess", new Message("backupMess", "备份失败"));
}
SmartUpload su = new SmartUpload();
su.initialize(servletConfig, request, response);
su.setContentDisposition(null);
su.downloadFile("/admin/backup/" + time + ".sql");
response.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("/admin/db/restore.do")
public String fileUpload(MultipartFile myfile, HttpServletRequest req, Model model) throws IllegalStateException, IOException {
String path = req.getServletContext().getRealPath("/admin/backup/");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String time = format.format(date);
String filePath = path + "\\" + time + ".sql";
myfile.transferTo(new File(filePath));
String command = "cmd /c mysql -u" + ConnectionFactory.USER + " -p" + ConnectionFactory.PASSWORD + " -h" + ConnectionFactory.HOST + " " + ConnectionFactory.DATABASE + " < \"" + filePath + "\"";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
try {
int tag = process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (tag == 0) {
model.addAttribute("mess", new Message("restoreMess", "还原成功"));
} else {
model.addAttribute("mess", new Message("restoreMess", "还原失败"));
}
} catch (Exception e) {
e.printStackTrace();
}
return "/admin/db";
}
private ServletConfig servletConfig;
@Override
public void setServletConfig(ServletConfig servletConfig) {
this.servletConfig = servletConfig;
}
}

View File

@ -12,9 +12,11 @@ import java.util.HashMap;
* @author xtaod
*/
public class ConnectionFactory {
public static final String URL = "jdbc:mysql://10.151.0.67:3306/repair?useSSL=false&serverTimezone=Asia/Shanghai";
public static final String USER = "root";
public static final String PASSWORD = "J3YNtU6v2dvio3gj6M7r";
public static final String HOST = "10.151.0.67";
public static final String DATABASE = "repair";
public static final String URL = "jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?useSSL=false&serverTimezone=Asia/Shanghai";
public static MybatisFlexBootstrap bootstrap;
public static HashMap<String, Object> mappers = new HashMap<>();

View File

@ -0,0 +1,30 @@
package cn.edu.cqwu.repair.util;
/**
* @author xtaod
*/
public class Message {
String name;
String content;
public Message(String stageAddMess, String s) {
this.name = stageAddMess;
this.content = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

View File

@ -16,6 +16,8 @@
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
</beans>

Binary file not shown.

View File

@ -0,0 +1,62 @@
<%--
Created by IntelliJ IDEA.
User: xtaod
Date: 2024/5/15
Time: 下午3:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page isELIgnored="false" %>
<c:set var="title" value="数据库管理"/>
<c:set var="webroot" value="${pageContext.request.contextPath}"/>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>${title}</title>
<link rel="stylesheet" href="${webroot}/styles/bootstrap.min.css" crossorigin="anonymous">
<script src="${webroot}/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="${webroot}/styles/sidebar.css" crossorigin="anonymous">
<link rel="stylesheet" href="${webroot}/styles/index.css" crossorigin="anonymous">
</head>
<body>
<main class="d-flex flex-nowrap">
<%@ include file="../includes/header.jsp" %>
<main class="form-signin w-100 m-auto">
<img class="mb-4" src="${webroot}/images/logo.jpg" alt="" width="72" height="72">
<h1 class="h3 mb-3 fw-normal">备份数据库</h1>
<c:if test="${'backupMess' eq requestScope.mess.name}">
<div class="alert alert-warning" role="alert">
${requestScope.mess.content}
</div>
</c:if>
<form action="${webroot}/admin/db/backup.do" method="post">
<input class="btn btn-primary w-100 py-2" type="submit" id="submit" value="备份数据库">
<br/><br/>
</form>
<h1 class="h3 mb-3 fw-normal">恢复数据库</h1>
<c:if test="${'restoreMess' eq sessionScope.mess.name}">
<div class="alert alert-warning" role="alert">
${sessionScope.mess.content}
</div>
</c:if>
<form action="${webroot}/admin/db/restore.do" method="post" enctype="multipart/form-data">
<div>
<label for="formFileLg" class="form-label">请您上传 sql 文件</label>
<input class="form-control form-control-lg" id="formFileLg" type="file" name="myfile">
</div>
<br/><br/>
<input class="btn btn-primary w-100 py-2" type="submit" id="submit1" value="恢复数据库">
<br/><br/>
</form>
</main>
</main>
</body>
</html>

View File

@ -56,6 +56,11 @@
>> 报修区域管理
</a>
</li>
<li class="nav-item">
<a href="${webroot}/admin/state.jsp" class="nav-link link-body-emphasis" aria-current="page">
>> 设备管理
</a>
</li>
<li class="nav-item">
<a href="${webroot}/admin/state.jsp" class="nav-link link-body-emphasis" aria-current="page">
>> 耗材管理
@ -66,6 +71,11 @@
>> 统计报表
</a>
</li>
<li class="nav-item">
<a href="${webroot}/admin/db.jsp" class="nav-link link-body-emphasis" aria-current="page">
>> 数据库管理
</a>
</li>
</c:when>
<c:when test="${not empty sessionScope.workerUser}">
<li class="nav-item">