注:要将mysql的bin目录加入到环境变量Path中
1、将MySql中的数据库定时导出到文件中 备份
<span style="font-size:18px;">
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import mail.MailSenderInfo;
import mail.SimpleMailSender;
import org.jeecgframework.web.demo.service.test.TaskDemoServiceI;
import org.springframework.stereotype.Service;
@Service("taskDemoService")
public class TaskDemoServiceImpl implements TaskDemoServiceI {
public void works() {
// 数据库导出
String user = "root"; // 数据库帐号
String password = "root"; // 登陆密码
String database = "oapms"; // 需要备份的数据库名
Date currentDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String sdfDate = sdf.format(currentDate);
String filepath = "e:\\oapms_" + sdfDate + ".sql"; // 备份的路径地址
// 注意mysqldump是调用mysql数据库的一个组件,在未在系统变量中声明的话,要在这里写mysqldump的完整路径
String stmt1 = "mysqldump " + database + " -u " + user + " -p"
+ password + " --result-file=" + filepath;
try {
Runtime.getRuntime().exec(stmt1);
System.out.println("数据已导出到文件" + filepath + "中");
} catch (IOException e) {
e.printStackTrace();
}
}
}</span>
2、将数据从磁盘上的文本文件还原到MySql中的数据库
<span style="font-size:18px;">package util;
import java.io.IOException;
public class Beifen {
/**
* @param args
*/
public static void main(String[] args) {
// 还原MySql数据库
String filepath = "e:\\oapms_11-35-00.sql"; // 备份的路径地址
// 新建数据库test
String stmt1 = "mysqladmin -u root -proot create testbeifen";
String stmt2 = "mysql -u root -proot testbeifen < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("数据已从 " + filepath + " 导入到数据库中");
} catch (IOException e) {
e.printStackTrace();
}
}
}</span>
附: 定时任务的配置文件
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!-- 定时任务配置 scheduler 方式 注解 暂时不支持动态更新 -->
<context:component-scan base-package="org.jeecgframework.core.timer" />
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor"
scheduler="scheduler" />
<!-- 定时任务配置 quartz 可配置到管理界面 -->
<bean id="taskDemoServiceTaskJob2"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="taskDemoService" />
<property name="targetMethod" value="works" />
<property name="concurrent" value="true" />
</bean>
<bean id="taskDemoServiceTaskCronTrigger2" class="org.jeecgframework.core.timer.DataBaseCronTriggerBean">
<property name="jobDetail" ref="taskDemoServiceTaskJob2" />
<property name="cronExpression" value="0 * 15 * * ?" />
</bean>
<!-- 定时任务调度器 -->
<bean id="schedulerFactory" lazy-init="false" autowire="no"
class="org.jeecgframework.core.timer.DataBaseSchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="taskDemoServiceTaskCronTrigger2" />
</list>
</property>
</bean>
</beans></span>