首先是Services
@Service
public class logServiceimpl implements logService {
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate;
@Override
public List getlog() {
String sql="show OPEN TABLES where In_use > 0;";
List
然后是Controller
@Component
public class runCron {
@Resource
private logService logservice;
@Value("${dir}")
private String dir;
@Scheduled(cron="0 0/1 * * * ?") //每一分钟执行一次
public void getlog() throws IOException {
List mapList = logservice.getlog();
File file = new File(dir);
if (!file.exists()) {
file.createNewFile();
}
//true 禁止覆盖之前的数据
FileWriter fileWriter=new FileWriter(file,true);
PrintWriter fout = new PrintWriter(fileWriter);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
fout.write(formatter.format(date));
String huanhang = System.getProperty("line.separator");
fout.write(huanhang);
for (int i = 0; i < mapList.size(); i++) {
lockLog sql = (lockLog) mapList.get(i);
String content = sql.getDatabase() + "t" + sql.getTable() + "t" + sql.getIn_use() + "t" + sql.getName_locked();
fout.write(content);
huanhang = System.getProperty("line.separator");
fout.write(huanhang);
}
fout.flush();
fout.close();
}
}
最后是配置文件application.properties
dir=D:\aaa.txt spring.datasource.primary.jdbc-url=jdbc:mysql://192.168.xx:xx/xx spring.datasource.primary.username=xx spring.datasource.primary.password=xx spring.datasource.primary.driver-class-name= com.mysql.cj.jdbc.Driver



