8 changed files with 226 additions and 20 deletions
-
32src/main/java/com/example/demo/Mysql/MysqlController.java
-
9src/main/java/com/example/demo/Mysql/MysqlService.java
-
64src/main/java/com/example/demo/Mysql/MysqlServiceImpl.java
-
13src/main/java/com/example/demo/config/Mysql1DataSourceConfig.java
-
63src/main/java/com/example/demo/config/SqlServer1DataSourceConfig.java
-
2src/main/java/com/example/demo/domain/DTO/GoldDetailDTO.java
-
52src/main/java/com/example/demo/serviceImpl/ExportExcelServiceImpl.java
-
11src/main/resources/application.yml
@ -0,0 +1,32 @@ |
|||
//package com.example.demo.Mysql; |
|||
// |
|||
//import com.example.demo.domain.vo.Result; |
|||
//import lombok.RequiredArgsConstructor; |
|||
//import lombok.extern.slf4j.Slf4j; |
|||
//import org.springframework.beans.factory.annotation.Autowired; |
|||
//import org.springframework.web.bind.annotation.CrossOrigin; |
|||
//import org.springframework.web.bind.annotation.RequestMapping; |
|||
//import org.springframework.web.bind.annotation.RestController; |
|||
// |
|||
///** |
|||
// * @program: GOLD |
|||
// * @ClassName MysqlController |
|||
// * @description: |
|||
// * @author: huangqizhen |
|||
// * @create: 2025−07-08 15:50 |
|||
// * @Version 1.0 |
|||
// **/ |
|||
//@RestController |
|||
//@RequestMapping("/Mysql") |
|||
//@RequiredArgsConstructor |
|||
//@Slf4j |
|||
//@CrossOrigin |
|||
//public class MysqlController { |
|||
// @Autowired |
|||
// MysqlService mysqlService; |
|||
// @RequestMapping |
|||
// public Result Mysql () throws Exception { |
|||
// mysqlService.getSqlserverData(); |
|||
// return Result.success(); |
|||
// } |
|||
//} |
@ -0,0 +1,9 @@ |
|||
package com.example.demo.Mysql;/** |
|||
@program: GOLD |
|||
@ClassName MysqlService |
|||
@description: |
|||
@author: huangqizhen |
|||
@create: 2025−07-08 15:52 |
|||
@Version 1.0 |
|||
**/public interface MysqlService { |
|||
} |
@ -0,0 +1,64 @@ |
|||
//package com.example.demo.Mysql; |
|||
// |
|||
//import com.example.demo.Mysql.MysqlService; |
|||
//import org.slf4j.Logger; |
|||
//import org.slf4j.LoggerFactory; |
|||
//import org.springframework.beans.factory.annotation.Qualifier; |
|||
//import org.springframework.scheduling.annotation.Scheduled; |
|||
//import org.springframework.stereotype.Service; |
|||
// |
|||
//import javax.sql.DataSource; |
|||
//import java.sql.*; |
|||
//import java.time.LocalDateTime; |
|||
// |
|||
//@Service |
|||
//public class MysqlServiceImpl implements MysqlService { |
|||
// |
|||
// private static final Logger logger = LoggerFactory.getLogger(MysqlServiceImpl.class); |
|||
// |
|||
// private final DataSource sqlServerDataSource; |
|||
// private final DataSource mysqlDataSource; |
|||
// |
|||
// public MysqlServiceImpl(@Qualifier("sqlServerDataSource") DataSource sqlServerDataSource, |
|||
// @Qualifier("mysqlDataSource") DataSource mysqlDataSource) { |
|||
// this.sqlServerDataSource = sqlServerDataSource; |
|||
// this.mysqlDataSource = mysqlDataSource; |
|||
// } |
|||
// |
|||
// @Override |
|||
// @Scheduled(cron = "0 0 * * * ?") // 每小时执行一次 |
|||
// public void getSqlserverData() { |
|||
// logger.info("开始从 SQL Server 同步数据到 MySQL..."); |
|||
// try (Connection sqlServerConn = sqlServerDataSource.getConnection(); |
|||
// Connection mysqlConn = mysqlDataSource.getConnection()) { |
|||
// |
|||
// // 从 SQL Server 查询数据 |
|||
// String querySql = "SELECT gtype,jwcode, created_at FROM user_gold_records WHERE created_at > ?"; |
|||
// try (PreparedStatement sqlServerStmt = sqlServerConn.prepareStatement(querySql)) { |
|||
// sqlServerStmt.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now().minusHours(1))); // 获取最近一小时的数据 |
|||
// ResultSet resultSet = sqlServerStmt.executeQuery(); |
|||
// |
|||
// // 插入到 MySQL |
|||
// String insertSql = "INSERT INTO target_table (id, name, created_at) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE name = ?"; |
|||
// try (PreparedStatement mysqlStmt = mysqlConn.prepareStatement(insertSql)) { |
|||
// while (resultSet.next()) { |
|||
// int id = resultSet.getInt("id"); |
|||
// String name = resultSet.getString("name"); |
|||
// Timestamp createdAt = resultSet.getTimestamp("created_at"); |
|||
// |
|||
// mysqlStmt.setInt(1, id); |
|||
// mysqlStmt.setString(2, name); |
|||
// mysqlStmt.setTimestamp(3, createdAt); |
|||
// mysqlStmt.setString(4, name); // 更新时的值 |
|||
// mysqlStmt.addBatch(); |
|||
// } |
|||
// mysqlStmt.executeBatch(); // 批量插入 |
|||
// } |
|||
// } |
|||
// logger.info("数据同步完成"); |
|||
// } catch (SQLException e) { |
|||
// logger.error("数据同步失败", e); |
|||
// throw new RuntimeException("数据同步失败", e); |
|||
// } |
|||
// } |
|||
//} |
@ -0,0 +1,63 @@ |
|||
//package com.example.demo.config; |
|||
// |
|||
//import com.zaxxer.hikari.HikariDataSource; |
|||
//import lombok.extern.slf4j.Slf4j; |
|||
//import org.apache.ibatis.session.SqlSessionFactory; |
|||
//import org.mybatis.spring.SqlSessionFactoryBean; |
|||
//import org.mybatis.spring.SqlSessionTemplate; |
|||
//import org.springframework.beans.factory.annotation.Qualifier; |
|||
//import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
//import org.springframework.boot.jdbc.DataSourceBuilder; |
|||
//import org.springframework.context.annotation.Bean; |
|||
//import org.springframework.context.annotation.Configuration; |
|||
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
|||
// |
|||
//import javax.sql.DataSource; |
|||
// |
|||
//@Slf4j |
|||
//@Configuration |
|||
//public class SqlServer1DataSourceConfig { |
|||
// |
|||
// /** |
|||
// * 定义 SQL Server 数据源 |
|||
// */ |
|||
// @Bean(name = "sqlserver1DataSource") |
|||
// @ConfigurationProperties(prefix = "spring.datasource.sqlserver1") |
|||
// public DataSource sqlserver1DataSource() { |
|||
// log.info("Initializing SQL Server data source..."); |
|||
// return DataSourceBuilder.create().type(HikariDataSource.class).build(); |
|||
// } |
|||
// |
|||
// /** |
|||
// * 定义 SQL Server 的 SqlSessionFactory |
|||
// */ |
|||
// @Bean(name = "sqlserver1SqlSessionFactory") |
|||
// public SqlSessionFactory sqlserver1SqlSessionFactory(@Qualifier("sqlserver1DataSource") DataSource dataSource, |
|||
// @Qualifier("globalConfiguration1") org.apache.ibatis.session.Configuration globalConfiguration) throws Exception { |
|||
// log.info("Initializing SQL Server SqlSessionFactory..."); |
|||
// SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); |
|||
// sessionFactory.setDataSource(dataSource); |
|||
// sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sqlserver/*.xml")); |
|||
// sessionFactory.setConfiguration(globalConfiguration); |
|||
// return sessionFactory.getObject(); |
|||
// } |
|||
// |
|||
// /** |
|||
// * 定义 SQL Server 的 SqlSessionTemplate |
|||
// */ |
|||
// @Bean(name = "sqlserver1SqlSessionTemplate") |
|||
// public SqlSessionTemplate sqlserver1SqlSessionTemplate(@Qualifier("sqlserver1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { |
|||
// log.info("Initializing SQL Server SqlSessionTemplate..."); |
|||
// return new SqlSessionTemplate(sqlSessionFactory); |
|||
// } |
|||
// |
|||
// /** |
|||
// * 定义全局 MyBatis 配置 |
|||
// */ |
|||
// @Bean |
|||
// @ConfigurationProperties(prefix = "mybatis.configuration.sqlserver1") |
|||
// public org.apache.ibatis.session.Configuration globalConfiguration() { |
|||
// log.info("Initializing SQL Server MyBatis global configuration..."); |
|||
// return new org.apache.ibatis.session.Configuration(); |
|||
// } |
|||
//} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue