项目地址
https://gitee.com/liuge1988/kitty
一些收获
1、baseEntity
其他实体继承
2、同样的crud service 也可以写个base 然后继承
public interface CurdService<T> { int save(T record); int delete(T record); int delete(List<T> records); T findById(Long id); PageResult findPage(PageRequest pageRequest); }
这里用的是接口,实际上,现在主张去掉inteface ,service直接class
3、Mysql 备份还原
MySQL备份还原工具类
import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.louis.kitty.backup.constants.BackupConstants; import com.louis.kitty.backup.datasource.BackupDataSourceProperties; import com.louis.kitty.backup.service.MysqlBackupService; import com.louis.kitty.backup.util.FileUtils; import com.louis.kitty.backup.util.HttpResult; /** * 系统数据备份还原 * 采用MYSQL备份还原命令 * @author Louis * @date Sep 20, 2018 */ @RestController @RequestMapping("/backup") public class MySqlBackupController { @Autowired MysqlBackupService mysqlBackupService; @Autowired BackupDataSourceProperties properties; @GetMapping("/backup") public HttpResult backup() { String host = properties.getHost(); String userName = properties.getUserName(); String password = properties.getPassword(); String database = properties.getDatabase(); String backupFodlerName = BackupConstants.DEFAULT_BACKUP_NAME+ "_" + (new SimpleDateFormat(BackupConstants.DATE_FORMAT)).format(new Date()); String backupFolderPath = BackupConstants.BACKUP_FOLDER + backupFodlerName + File.separator; String fileName = BackupConstants.BACKUP_FILE_NAME; try { mysqlBackupService.backup(host, userName, password, backupFolderPath, fileName, database); } catch (Exception e) { return HttpResult.error(500, e.getMessage()); } return HttpResult.ok(); } @GetMapping("/restore") public HttpResult restore(@RequestParam String name) { String host = properties.getHost(); String userName = properties.getUserName(); String password = properties.getPassword(); String database = properties.getDatabase(); String restoreFilePath = BackupConstants.RESTORE_FOLDER + name; try { mysqlBackupService.restore(restoreFilePath, host, userName, password, database); } catch (Exception e) { return HttpResult.error(500, e.getMessage()); } return HttpResult.ok(); } @GetMapping("/findRecords") public HttpResult findBackupRecords() { List<Map<String, Object>> backupRecords = new ArrayList<>(); File restoreFolderFile = new File(BackupConstants.RESTORE_FOLDER); if(restoreFolderFile.exists()) { for(File file:restoreFolderFile.listFiles()) { Map<String, Object> bean = new HashMap<>(); bean.put("name", file.getName()); bean.put("title", file.getName()); if(BackupConstants.DEFAULT_BACKUP_NAME.equals(file.getName())) { bean.put("title", "系统默认备份"); } backupRecords.add(bean); } } return HttpResult.ok(backupRecords); } @GetMapping("/delete") public HttpResult deleteBackupRecord(@RequestParam String name) { if(BackupConstants.DEFAULT_BACKUP_NAME.equals(name)) { return HttpResult.error("系统默认备份无法删除!"); } String restoreFilePath = BackupConstants.RESTORE_FOLDER + name; try { FileUtils.deleteFile(new File(restoreFilePath)); } catch (Exception e) { return HttpResult.error(500, e.getMessage()); } return HttpResult.ok(); } }
4、springboot 配置druid mybaits 以及sql监控
5、跨域的配置
/** * 跨域配置 * @author Louis * @date Oct 29, 2018 */ @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 允许跨域访问的路径 .allowedOrigins("*") // 允许跨域访问的源 .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") // 允许请求方法 .maxAge(168000) // 预检间隔时间 .allowedHeaders("*") // 允许头部设置 .allowCredentials(true); // 是否发送cookie } }