项目作者: banchunchun

项目描述 :
基于spring boot开发的数据组件集成库,目前支持:mybatis、JPA、redis、memcached、elasticsearch、mongodb和基于redis或memcached的cache
高级语言: Java
项目地址: git://github.com/banchunchun/spring-data-framework.git
创建时间: 2017-05-26T06:37:17Z
项目社区:https://github.com/banchunchun/spring-data-framework

开源协议:

下载


spring data数据中间件

  1. 项目基于spring boot,用于简化SQL/Nosql相关配置。
  2. 由于spring boot 默认情况下配置数据库等都是简单配置,如果需要多数据源、读写
  3. 分离等设置,就需要手动配置。
  4. 此项目所做的就是将这些较复杂配置整理成一个个的jar,使用者直接引入依赖,并配置
  5. 项目的具体数据源,即可使用,避免每个项目都要写一套。

项目地址

  1. https://github.com/banchunchun/spring-data-framework/

项目模块

  • bc-data-framework-common
  • bc-data-framework-datasource
  • bc-data-framework-mybatis
  • bc-data-framework-jpa
  • bc-data-framework-mongo
  • bc-data-framework-redis
  • bc-data-framework-memcached
  • bc-data-framework-cache-elasticsearch

bc-data-framework-common[公共模块]

  1. 此模块包含一些公共的类,暂时没有具体的逻辑。

bc-data-framework-datasource[数据源模块]

  1. 此模块实现了多数据源配置,支持读写分离、动态数据源切换功能。

bc-data-framework-mybatis[mybatis支持模块]

  1. 如果使用mybatis作为ORM框架,引入此模块即可直接使用Mapper的方式操作数据库。

bc-data-framework-jpa[JPA支持模块]

  1. 使用JPA操作数据库,可以和mybatis支持模块同时使用。

bc-data-framework-mongo[MongoDB支持模块]

  1. 配置并实现了MongoService,同时支持获取MongoTemplateMongoClientDBCollection以及读写分离操作。
  2. 另外也可以直接使用spring-datarepository方式操作mongo

bc-data-framework-redis[Redis支持模块]

  1. 配置并提供了RedisService,同时支持获取RedisTemplate

bc-data-framework-memcached[memcached支持模块]

  1. 配置并实现了MemcachedService,支持常用的memcache操作。

bc-data-framework-cache-elasticsearch[ES搜索支持模块]

  1. 配置并实现了ElasticsearchService,同时支持获取ElasticsearchTemplateClient操作。
  2. 目前Client只实现了TransportClient的连接方式,NodeClient连接方式暂未实现
  3. 另外也可以直接使用spring-datarepository方式操作elasticsearch

如何使用

加入依赖

使用mybatis,配置扫描的包路径需要加上org.spring.data.framework

  1. <dependency>
  2. <groupId>org.spring.data.framework</groupId>
  3. <artifactId>bc-spring-data-framework-mybatis</artifactId>
  4. <version>${bc-data-framework-mybatis.version}</version>
  5. </dependency>

使用JPA

  1. <dependency>
  2. <groupId>org.spring.data.framework</groupId>
  3. <artifactId>bc-spring-data-framework-jpa</artifactId>
  4. <version>${bc-data-framework-jpa.version}</version>
  5. </dependency>

使用其他模块类似。

配置文件

  1. 推荐使用yml文件配置

数据源配置

  1. bc:
  2. data:
  3. datasources:
  4. enabled: true ##是否开启数据源设置
  5. default-datasource-type: com.alibaba.druid.pool.DruidDataSource ##默认数据源类型
  6. default-datasource-name: default ##默认数据源名称
  7. groups: ##数据源组列表,包含自主从结构
  8. -
  9. group-name: default-group ##组名称
  10. items: ##组下面的数据源列表
  11. -
  12. name: default ##数据源名称
  13. master: true ##是否是主节点
  14. type: com.alibaba.druid.pool.DruidDataSource
  15. driver-class-name: com.mysql.jdbc.Driver
  16. url: jdbc:mysql://172.28.1.6:3306/db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&rewriteBatchedStatements=true
  17. username: root
  18. password: xxxx
  19. -
  20. name: ms0_ds1
  21. type: com.alibaba.druid.pool.DruidDataSource
  22. driver-class-name: com.mysql.jdbc.Driver
  23. url: jdbc:mysql://172.28.1.6:3306/db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&rewriteBatchedStatements=true
  24. username: root
  25. password: xxx
  26. -
  27. name: ms0_ds2
  28. type: com.alibaba.druid.pool.DruidDataSource
  29. driver-class-name: com.mysql.jdbc.Driver
  30. url: jdbc:mysql://172.28.1.6:3306/xxx?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&rewriteBatchedStatements=true
  31. username: root
  32. password: xxx
  33. -
  34. group-name: ms1
  35. items:
  36. -
  37. name: ms1_ds0
  38. master: true
  39. type: com.alibaba.druid.pool.DruidDataSource
  40. driver-class-name: com.mysql.jdbc.Driver
  41. url: jdbc:mysql://172.28.1.6:3306/xxx?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&rewriteBatchedStatements=true
  42. username: root
  43. password: xxx
  44. -
  45. name: ms1_ds1
  46. type: com.alibaba.druid.pool.DruidDataSource
  47. driver-class-name: com.mysql.jdbc.Driver
  48. url: jdbc:mysql://172.28.1.6:3306/xxx?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&rewriteBatchedStatements=true
  49. username: root
  50. password: xxx
  51. -
  52. group-name: ms2
  53. items:
  54. -
  55. name: ms2_ds0
  56. master: true
  57. type: com.alibaba.druid.pool.DruidDataSource
  58. driver-class-name: com.mysql.jdbc.Driver
  59. url: jdbc:mysql://172.28.1.6:3306/db_gd_dev?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&rewriteBatchedStatements=true
  60. username: root
  61. password: xxxx

mybatis配置

  1. bc:
  2. data:
  3. mybatis:
  4. enabled: true ##是否启用mybatis
  5. mapper-locations: classpath*:mapper/*Mapper.xml ##mapper文件地址
  6. type-aliases-package: com.mamahao.actsys.api.entity ##实体类所在包
  7. base-package: com.xxx.actsys.api.mapper ##Mapper所在包

JPA配置

  1. bc:
  2. data:
  3. jpa:
  4. enabled: true ##是否启用JPA
  5. show-sql: true ##是否打印SQL
  6. open-in-viewiew: false ##是否开启延迟关闭session
  7. hibernate: ##hibernte实现的JPA相关设置
  8. ddl-auto: update ##ddl策略
  9. dialect: org.hibernate.dialect.MySQL5Dialect ##数据库dialect
  10. entity-packages: ##实体类的包列表
  11. -
  12. com.xxx.actsys.api.entity

Mongo配置

  1. bc:
  2. data:
  3. mongo:
  4. enabled: true ##是否启用mongo
  5. defult-database: mydb ##默认数据库
  6. username: test
  7. password: test123
  8. connection-per-host: 100 ##每个host最大连接数
  9. connection-timeout-millis: 30000 ##客户端超时时间(毫秒)
  10. max-wait-time-millis: 30000 ##客户端最大等待时间(毫秒)
  11. addrs: ##mongo地址列表
  12. -
  13. host: 172.28.1.3
  14. port: 27017
  15. -
  16. host: 172.28.1.28
  17. port: 27017

Redis配置

  1. bc:
  2. data:
  3. redis:
  4. enabled: true ##是否启用redis
  5. default-database: 0 ##默认数据库下标
  6. password: ##密码
  7. host: 172.28.1.14 ##地址,单机时填写
  8. port: 6379 ##端口,单机时填写
  9. timeout: 100 ##请求超时时间
  10. default-expire-seconds: 1800 ##数据失效时间(秒)
  11. pool: ##连接池设置
  12. max-idle: 8 ##最大空闲连接数
  13. max-total: 20 ##最大连接数
  14. max-wait-millis: 1000 ##最大等待时间(毫秒)
  15. min-idle: 0 ##最大空闲连接数
  16. sentinel: ##哨兵配置
  17. master: redis01 ##主节点名称
  18. nodes: ##哨兵节点列表
  19. -
  20. host: 172.28.1.15
  21. port: 6379
  22. -
  23. host: 172.28.1.16
  24. port: 6379

Memcached配置

  1. bc:
  2. data:
  3. memcached:
  4. enabled: true ##是否启用memcached
  5. session-idle-timeout: 10000 ##session最大空闲时间
  6. default-cache-name: default_cache ##默认缓存组名称
  7. default-expire-seconds: 1800 ##默认缓存时间(秒)
  8. addrs: ##memcache地址列表
  9. -
  10. host: 172.28.1.129
  11. port: 11211

```