数据库分批读写交互模板
- 封装数据分批流式读写模板
- 封装mysql, es和mongo数据库的读写模板
db-exchange-core
:核心读写交互模板db-exchange-mysql
:封装mysql数据库读写db-exchange-es
:封装es数据库读写db-exchange-mongo
:封装mongo数据库读写db-exchange-spring-jdbc
:封装Spring JDBC读写
-
添加
db-exchange-mysql
Maven引用<dependency> <groupId>com.github.mengzuozhu</groupId> <artifactId>db-exchange-mysql</artifactId> <version>1.0.1</version> </dependency>
-
mysql读取示例
public class ExchangeDemo { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai"; DataSource dataSource = MysqlUtil.getDataSource(url, "root", "your mysql password"); MysqlQuery query = MysqlQuery.builder() .dataSource(dataSource) .sql("SELECT * FROM user") .batchSize(100) .build(); DataReader<Map<String, Object>> mysqlDataReader = new MysqlDataReader<>(query, new ResultSetToMap()); ListDataCollector<Map<String, Object>> listDataCollector = new ListDataCollector<>(); ExchangeTemplate.from(mysqlDataReader) .execute(listDataCollector) .await(); // do something List<Map<String, Object>> result = listDataCollector.getResult(); } }