MongoDB在银行海量历史订单交易数据查询中的应用(Spring boot + Bee)
近年来,随着各种便捷支付方式的普及,银行账户交易数据呈现爆炸式增长,同时数据模型也在不断变化,传统关系型数据库已难以满足这种海量的、模式灵活、高可用、高性能的数据存储和查询需求。通过对银行历史交易数据查询业务特点的分析,本文提出一种基于MongoDB数据库的银行海量历史订单交易数据查询解决方案,方案包括系统架构、数据模型、索引设计和分片键策略。实验表明,MongoDB能够实现海量的交易数据的存储和快速查询能力。
代码架构图:
MongoDB历史订单表的数据:
根据表,生成对应的Javabean:
new GenBean().genSomeBeanFile("orderhistory");
new GenBean().genAllBeanFile(); 为所有表生成Javabean. 一行搞定
Javabean :
Spring boot Application
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Spring boot Rest 代码:
@RestController
@RequestMapping("/orderhistory")
public class OrderhistoryRest {
// Mongodb使用
@Resource(name = "mongodbObjSQLRichService")
ObjSQLRichService objSQLRichService;
@RequestMapping("/list")
public Result list(Orderhistory orderhistory,
@RequestParam(value = "page", defaultValue = "1", required = false) int page,
@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {
Result result = new Result();
try {
objSQLRichService.setDataSourceName("ds0");
int total = objSQLRichService.count(orderhistory);
List<Orderhistory> list = objSQLRichService.select(orderhistory, (page - 1) * rows, rows);
result.setRows(list);
result.setTotal(total);
} catch (BeeSQLException e) {
Logger.error(e.getMessage(),e);
result.setErrorMsg(e.getMessage());
}catch (Exception e) {
Logger.error(e.getMessage(),e);
result.setErrorMsg(e.getMessage());
}
return result;
}
}
为什么要有这句话, 因为ObjSQLRichService默认是使用JDBC的,访问传统的关系型数据库, 使用Mongodb时,需要指定.
@Resource(name = "mongodbObjSQLRichService")
bee.properties配置信息:
bee.db.dbName=MongoDB
bee.db.url = mongodb://localhost:27017/bee/
bee.db.username =
bee.db.password =
bee.osql.showSQL=true
bee.osql.showSql.showType=true
bee.osql.showSql.showExecutableSql=true
将配置信息,放application.properties也是可以的.
Maven pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bee-starter-demo</artifactId>
<parent>
<groupId>org.teasoft</groupId>
<artifactId>bee-spring-boot-starter</artifactId>
<version>2.1</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>2.5.6</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.automvc</groupId>
<artifactId>comm</artifactId>
<version>1.0.2-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/automvc-comm-basic2.jar</systemPath>
</dependency>
<!-- druid -->
<!-- <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency> -->
<!-- dbcp2 -->
<!-- <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency> -->
<!-- c3p0 -->
<!-- <dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.15</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- if in spring-boot use datasoure,need this dependency -->
<!-- 直接使用DriverManager.getConnection获取连接,不用设置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- for TranHandler use aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动Application
访问地址,即可返回Json数据.
前端界面效果:
至此, 使用Mongodb开发银行订单历史系统的例子就介绍完成了.
Bee整合Spring Boot,让你瞬间拥有两样快速开发利器!
Java ORM Bee 同时支持JDBC(比如JavaWeb),Android和Harmony;支持Sharding分片;支持多种关系型数据库(MySQL,MariaDB,Oracle,H2,SQLite,PostgreSQL,SQL Server,Access等),还支持NoSQL的Cassandra,Mongodb等
源码下载:
https://github.com/automvc/bee
https://gitee.com/automvc/bee
https://gitee.com/automvc/bee-springboot
MongoDB还可应用于外卖场景。使用MongoDB存储用户信息及用户发表的朋友圈信息,通过地理位置索引将快递骑手和快递商家的信息(包含位置信息)存储在MongoDB,然后通过地理位置查询,使得快递骑手能就近接单。
下次再介绍了.