dynamic: primary: master #设置默认的数据源或者数据源组,默认值即为master strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源. datasource: master: url: jdbc:MysqL://ip:端口/库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai3.写好两个实体类,一个sqlserver的表,一个MysqL的表 4.写好转移的逻辑业务(HelloService中)
username: root password: root driver-class-name: com.MysqL.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置 slave_1: url: jdbc:sqlserver://ip:端口;DatabaseName=ykt username: sa password: 123 driver-class-name: com.microsoft.sqlserver.jdbc.sqlServerDriver
time: 2020-01-01 00:00:00
@Service @Slf4j public class HelloService { @Autowired private DataMapper dataMapper; @Autowired private ParkRecordMapper parkRecordMapper; @Value("${time}") @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date starTime; public void saveData() throws ParseException {
//以下为具体业务,根据自己需求修改 log.info("进入数据转存定时任务"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date endTime = new Date(); List<ComeCar> comeCars = dataMapper.selectByTime(starTime,endTime); if (comeCars!=null && comeCars.isEmpty()){ return; } for (int i = 0; i < comeCars.size(); i++) { ComeCar comeCar = comeCars.get(i); log.info("正在转存数据"); ParkRecord parkRecord = new ParkRecord(); parkRecord.setCreateDate(formatter.parse(comeCar.getComeDate())); parkRecord.setPhoto(comeCar.getComePic()); log.info("单条数据为"+parkRecord); parkRecordMapper.insert(parkRecord); } starTime = endTime; log.info("刷新数据成功"); } }
5.创建一个定时器(SchedulingTask)
@Component @Async @Slf4j public class SchedulingTask { private final long TEN_MINUTES = 600000; @Autowired HelloService helloService; /** * 每10分钟刷新一下数据 */ @Scheduled(fixedrate = TEN_MINUTES) public void refreshData() { try { helloService.saveData(); log.info("定时任务开启,每10分钟刷新一下数据"); } catch (ParseException e) { log.error("刷新数据失败,sql Server数据库有可能挂了"); e.printstacktrace(); } } }
6.HelloController
@RestController public class HelloController { @Autowired HelloService helloService; @GetMapping("hello") public String hello() throws ParseException { helloService.saveData(); return "hello"; } }
7.运行测试没问题,修改模拟数据库地址,maven导出jar包,再到云服务器上运行就可以了!
@Mapper public interface DataMapper {
@DS("slave_1") List<ComeCar> selectByTime(@Param("star_time") Date starTime,@Param("end_time")Date endTime); }