Skip to content

Commit 5330880

Browse files
author
杨钊
committed
spring boot 重构项目
0 parents  commit 5330880

20 files changed

+1297
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# data_migration 数据迁移工具
2+
3+
支持mysql、sqlite、postgresql、sqlserver四种数据库之间数据相互迁移。
4+
5+
### 两种方式实现数据迁移:
6+
7+
![image.png](https://upload-images.jianshu.io/upload_images/3057341-a1b8410eb5bb5d04.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
8+
9+
#### ①页面交互
10+
11+
数据库连接成功后,通过点击按钮查询所有表显示当前数据库所有表名然后点击表名显示该表的所有字段,只需要勾选需要操作的字段。
12+
13+
**这种方式只支持源数据库select,目标数据库insert操作**
14+
15+
#### ②SQL输入
16+
这种方式比较简单,在页面中输入将在两边数据库执行的sql语句然后点击执行按钮即可
17+
18+
### 安装、部署
19+
20+
**JDK环境:>=1.8**
21+
22+
#### 源码部署:
23+
24+
项目源码:https://github.com/yz-java/data_migration
25+
maven打包:mvn clean package -Dmaven.test.skip=true
26+
27+
#### realase下载:
28+
29+
[Download](https://github.com/yz-java/data_migration/releases)
30+
31+
#### 运行
32+
java -jar 安装包
33+
34+

pom.xml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.yangzhao</groupId>
5+
<artifactId>data_migration</artifactId>
6+
<packaging>jar</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>data_migration Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
<properties>
11+
<org.springframework.version>4.2.3.RELEASE</org.springframework.version>
12+
<java.version>1.8</java.version>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<spring.boot.version>1.5.9.RELEASE</spring.boot.version>
15+
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
16+
</properties>
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>3.8.1</version>
23+
<scope>test</scope>
24+
</dependency>
25+
26+
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
27+
<dependency>
28+
<groupId>org.xerial</groupId>
29+
<artifactId>sqlite-jdbc</artifactId>
30+
<version>3.23.1</version>
31+
</dependency>
32+
33+
34+
<dependency>
35+
<groupId>mysql</groupId>
36+
<artifactId>mysql-connector-java</artifactId>
37+
<version>5.1.38</version>
38+
</dependency>
39+
40+
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
41+
<dependency>
42+
<groupId>org.postgresql</groupId>
43+
<artifactId>postgresql</artifactId>
44+
<version>9.4.1212</version>
45+
</dependency>
46+
47+
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
48+
<dependency>
49+
<groupId>com.microsoft.sqlserver</groupId>
50+
<artifactId>mssql-jdbc</artifactId>
51+
<version>6.1.0.jre7</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>com.alibaba</groupId>
56+
<artifactId>druid</artifactId>
57+
<version>1.0.18</version>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>com.fasterxml.jackson.core</groupId>
62+
<artifactId>jackson-databind</artifactId>
63+
<version>2.6.3</version>
64+
</dependency>
65+
66+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter</artifactId>
70+
<version>${spring.boot.version}</version>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
76+
<version>${spring.boot.version}</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.springframework</groupId>
81+
<artifactId>spring-jdbc</artifactId>
82+
<version>4.3.13.RELEASE</version>
83+
</dependency>
84+
85+
</dependencies>
86+
87+
<build>
88+
<finalName>data_migration</finalName>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-compiler-plugin</artifactId>
93+
<configuration>
94+
<source>${java.version}</source>
95+
<target>${java.version}</target>
96+
<!-- 文件编码格式 -->
97+
<encoding>${project.build.sourceEncoding}</encoding>
98+
<compilerArguments>
99+
<verbose/>
100+
<bootclasspath>${java.home}/lib/rt.jar:${java.home}/lib/jce.jar</bootclasspath>
101+
</compilerArguments>
102+
</configuration>
103+
</plugin>
104+
<plugin>
105+
<groupId>org.springframework.boot</groupId>
106+
<artifactId>spring-boot-maven-plugin</artifactId>
107+
<version>${spring.boot.version}</version>
108+
<configuration>
109+
<mainClass>com.yz.Application</mainClass>
110+
<fork>true</fork>
111+
</configuration>
112+
<executions>
113+
<execution>
114+
<goals>
115+
<goal>repackage</goal>
116+
</goals>
117+
</execution>
118+
</executions>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
123+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.yz;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author yz
8+
* @Description
9+
* @Date create by 13:24 18/8/1
10+
*/
11+
@SpringBootApplication
12+
public class Application {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(Application.class,args);
16+
}
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.yz.bean;
2+
3+
/**
4+
* Created by yz on 17/2/8.
5+
*/
6+
public class Property {
7+
8+
private String driver;
9+
private String userName;
10+
private String passWord;
11+
private String url;
12+
13+
public String getDriver() {
14+
return driver;
15+
}
16+
17+
public void setDriver(String driver) {
18+
this.driver = driver;
19+
}
20+
21+
public String getUserName() {
22+
return userName;
23+
}
24+
25+
public void setUserName(String userName) {
26+
this.userName = userName;
27+
}
28+
29+
public String getPassWord() {
30+
return passWord;
31+
}
32+
33+
public void setPassWord(String passWord) {
34+
this.passWord = passWord;
35+
}
36+
37+
public String getUrl() {
38+
return url;
39+
}
40+
41+
public void setUrl(String url) {
42+
this.url = url;
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.yz.configuration;
2+
3+
import org.springframework.boot.SpringBootConfiguration;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
6+
7+
import java.util.concurrent.ThreadPoolExecutor;
8+
9+
/**
10+
* @author yz
11+
* @Description
12+
* @Date create by 13:31 18/8/1
13+
*/
14+
@SpringBootConfiguration
15+
public class SysConfig {
16+
17+
@Bean
18+
public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
19+
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
20+
//核心线程数,默认为1
21+
threadPoolTaskExecutor.setCorePoolSize(100);
22+
//最大线程数,默认为Integer.MAX_VALUE
23+
threadPoolTaskExecutor.setMaxPoolSize(500);
24+
//队列最大长度,一般需要设置值>=notifyScheduledMainExecutor.maxNum;默认为Integer.MAX_VALUE
25+
threadPoolTaskExecutor.setQueueCapacity(800);
26+
//线程池维护线程所允许的空闲时间,默认为60s
27+
threadPoolTaskExecutor.setKeepAliveSeconds(300);
28+
/**
29+
* 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy
30+
* AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常
31+
* CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度
32+
* DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行
33+
* DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行
34+
*/
35+
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
36+
return threadPoolTaskExecutor;
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.yz.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
/**
7+
* @author yz
8+
* @Description
9+
* @Date create by 13:51 18/8/1
10+
*/
11+
@Controller
12+
public class IndexConroller {
13+
14+
@RequestMapping("/")
15+
public String index(){
16+
return "index";
17+
}
18+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.yz.controller;
2+
3+
import com.yz.bean.Property;
4+
import com.yz.database.Operation;
5+
import com.yz.database.OperationFactory;
6+
import com.yz.datasources.DataSourceContextHolder;
7+
import com.yz.datasources.DataSourceManager;
8+
import com.yz.service.MigrationService;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import javax.annotation.Resource;
13+
import javax.servlet.http.HttpSession;
14+
import javax.sql.DataSource;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.stream.Collectors;
19+
20+
/**
21+
* Created by yz on 17/2/7.
22+
*/
23+
@RestController
24+
public class MigrationController {
25+
26+
@Resource(name = "operationFactory")
27+
private OperationFactory operationFactory;
28+
29+
@Resource(name = "migrationServiceImpl")
30+
private MigrationService migrationServiceImpl;
31+
32+
@RequestMapping("/create_datasource")
33+
public Object createDataSource(HttpSession session, Property property, int flag, int databaseType){
34+
switch (databaseType){
35+
case 1:
36+
property.setDriver("com.mysql.jdbc.Driver");
37+
break;
38+
case 2:
39+
property.setDriver("org.sqlite.JDBC");
40+
break;
41+
case 3:
42+
property.setDriver("org.postgresql.Driver");
43+
break;
44+
case 4:
45+
property.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
46+
break;
47+
}
48+
boolean result = migrationServiceImpl.createDataSource(session.getId(), property, flag);
49+
return result;
50+
}
51+
52+
@RequestMapping("/tables")
53+
public Object getTables(HttpSession session,int databaseType,int flag){
54+
Operation operation = operationFactory.getOperation(databaseType);
55+
DataSource dataSource = DataSourceManager.getInstance().getUserDataSource(session.getId(), flag);
56+
DataSourceContextHolder.setDataSource(dataSource);
57+
List<String> tables = operation.queryTables();
58+
Collections.sort(tables);
59+
return tables;
60+
}
61+
62+
@RequestMapping("/table/fields")
63+
public Object getTableSchema(HttpSession session,String tableName,int databaseType,int flag){
64+
DataSource dataSource = DataSourceManager.getInstance().getUserDataSource(session.getId(), flag);
65+
DataSourceContextHolder.setDataSource(dataSource);
66+
Operation operation = operationFactory.getOperation(databaseType);
67+
List<Map<String, Object>> mapList = operation.queryTableSchema(tableName);
68+
List<String> fields = mapList.stream().map(map -> map.get("Field").toString()).collect(Collectors.toList());
69+
Collections.sort(fields);
70+
return fields;
71+
}
72+
73+
@RequestMapping("/data/migration")
74+
public Object migration(HttpSession session,String[]fields,String[]targetFields,String tableName,String targetTableName){
75+
boolean b = migrationServiceImpl.dataMigration(session.getId(), fields, targetFields, tableName, targetTableName);
76+
return b;
77+
}
78+
79+
@RequestMapping("/sql/execute")
80+
public Object execute(HttpSession session,String sourceSql,String tegetSql){
81+
long execute = migrationServiceImpl.execute(session.getId(), sourceSql, tegetSql);
82+
return execute;
83+
}
84+
}

0 commit comments

Comments
 (0)