数据库

Jboot 数据库功能基于 JFinal 的 ActiveRecordPlugin 插件和 Apache sharding-sphere 框架,提供了方便快捷的数据库操作能力。

目录

  • 描述
  • 基本增删改查
  • 关联查询
  • 分页查询
  • 批量插入
  • 事务操作
  • 读写分离
  • 分库分表
  • 分布式事务

描述

Jboot 的数据库是依赖 JFinal 的 ORM 做基本的数据库操作,同时依赖 apahce-sphere 来做分库、分表和依赖 Seata 来做分布式事务。因此,在使用 Jboot 操作数据库的时候,建议对 JFinal 的 ORM 功能和 Apache Sharding-Sphere 有所了解。

基本增删改查

JFinal 操作数据库,提供了两种方式对数据库进行操作,他们分别是:

  • Db + Record 方式
  • Model 映射方式

Db + Record 方式

Db 可以理解为一个工具类,而 Record 是一个类似 Map 的数据结构(其实内部就是通过 Map 来实现的),Db 查询的返回的数据是一个 Record 或者是 List<Record> , Db 提供了如下操作数据库的系列方法:

方法 描述
query(String, Object…)
query(String)
queryFirst(String, Object…)
queryFirst(String)
queryColumn(String, Object…)
queryColumn(String)
queryStr(String, Object…)
queryStr(String)
queryInt(String, Object…)
queryInt(String)
queryLong(String, Object…)
queryLong(String)
queryDouble(String, Object…)
queryDouble(String)
queryFloat(String, Object…)
queryFloat(String)
queryBigDecimal(String, Object…)
queryBigDecimal(String)
queryBytes(String, Object…)
queryBytes(String)
queryDate(String, Object…)
queryDate(String)
queryTime(String, Object…)
queryTime(String)
queryTimestamp(String, Object…)
queryTimestamp(String)
queryBoolean(String, Object…)
queryBoolean(String)
queryShort(String, Object…)
queryShort(String)
queryByte(String, Object…)
queryByte(String)
queryNumber(String, Object…)
queryNumber(String)
update(String, Object…)
update(String)
find(String, Object…)
find(String)
findFirst(String, Object…)
findFirst(String)
findById(String, Object)
findById(String, String, Object…)
deleteById(String, Object)
deleteById(String, String, Object…)
delete(String, String, Record)
delete(String, Record)
delete(String, Object…)
delete(String)
paginate(int, int, String, String, Object…)
paginate(int, int, boolean, String, String, Object…)
paginate(int, int, String, String)
paginateByFullSql(int, int, String, String, Object…)
paginateByFullSql(int, int, boolean, String, String, Object…)
save(Config, java.sql.Connection, String, String, Record)
save(String, String, Record)
save(String, Record)
update(Config, java.sql.Connection, String, String, Record)
update(String, String, Record)
update(String, Record)
execute(ICallback)
execute(Config, ICallback)
tx(Config, int, IAtom)
tx(int, IAtom)
tx(IAtom)
findByCache(String, Object, String, Object…)
findByCache(String, Object, String)
findFirstByCache(String, Object, String, Object…)
findFirstByCache(String, Object, String)
paginateByCache(String, Object, int, int, String, String, Object…)
paginateByCache(String, Object, int, int, boolean, String, String, Object…)
paginateByCache(String, Object, int, int, String, String)
batch(String, Object[][], int)
batch(String, String, List, int)
batch(List, int)
batchSave(List<? extends Model>, int)
batchSave(String, List, int)
batchUpdate(List<? extends Model>, int)
batchUpdate(String, String, List, int)
batchUpdate(String, List, int)
getSql
getSqlPara(String, Record)
getSqlPara(String, Model)
getSqlPara(String, Map)
getSqlPara(String, Object…)
find(SqlPara)
findFirst(SqlPara)
update(SqlPara)
paginate(int, int, SqlPara)

以下是 Db + Record 模式的一些示例:

  1. // 创建name属性为James,age属性为25的record对象并添加到数据库
  2. Record user = new Record()
  3. .set("name", "James")
  4. .set("age", 25);
  5. Db.save("user", user);
  6. // 删除id值为25的user表中的记录
  7. Db.deleteById("user", 25);
  8. // 查询id值为25的Record将其name属性改为James并更新到数据库
  9. user = Db.findById("user", 25).set("name", "James");
  10. Db.update("user", user);
  11. // 获取user的name属性
  12. String userName = user.getStr("name");
  13. // 获取user的age属性
  14. Integer userAge = user.getInt("age");
  15. // 查询所有年龄大于18岁的user
  16. List<Record> users = Db.find("select * from user where age > 18");
  17. // 分页查询年龄大于18的user,当前页号为1,每页10个user
  18. Page<Record> userPage = Db.paginate(1, 10, "select *", "from user where age > ?", 18);

在单数据库下,以下是 Db 工具进行的事务操作:

  1. boolean succeed = Db.tx(() -> {
  2. int count = Db.update("update account set cash = cash - ? where id = ?", 100, 123);
  3. int count2 = Db.update("update account set cash = cash + ? where id = ?", 100, 456);
  4. return count == 1 && count2 == 1;
  5. });

以上两次数据库更新操作在一个事务中执行,如果执行过程中发生异常或者return false,则自动回滚事务。

Model 映射方式

Model是 MVC 模式中的 M 部分。以下是 Model 定义示例代码:

  1. @Table(tableName = "user", primaryKey = "id")
  2. public class User extends BaseUser<User> {
  3. }

BaseUser:

  1. public abstract class BaseUser<M extends BaseUser<M>> extends JbootModel<M> implements IBean {
  2. public void setId(java.lang.Long id) {
  3. set("id", id);
  4. }
  5. public java.lang.Long getId() {
  6. return getLong("id");
  7. }
  8. // other getter setter ...
  9. }

需要注意的是:

  • 以上的 UserBaseUser 都是通过代码生成器自动生成的,无需手写。
  • 多次执行代码生成器,User 代码不会被覆盖,但是 BaseUser 会被重新覆盖,因此,请不要在 BaseUser 手写任何代码。

一般情况下,在正式的项目里,代码分层还需要 Service 层来对业务逻辑进行处理。

UserService 代码如下:

  1. public class UserService extends JbootServiceBase<User> {
  2. // 不需要做任何的实现
  3. }

以上的 UserService , 只需要继承 JbootServiceBase<User> ,我们不需要编写任何实现代码,就可以拥有基本的增删改查的功能。

以下是示例代码:

  1. //创建 UserService
  2. UserService userService = new UserService();
  3. // 创建name属性为James,age属性为25的User对象并添加到数据库
  4. User user = new User().set("name", "James").set("age", 25);
  5. userService.save(user);
  6. // 删除id值为25的User
  7. userService.deleteById(25);
  8. // 查询id值为25的User将其name属性改为James并更新到数据库
  9. User user = userService.findById(25);
  10. user.set("name", "James");
  11. userService.update(user);
  12. // 分页查询user,当前页号为1,每页10个user
  13. Page<User> userPage = userService.paginate(1, 10);

读写分离

在 Jboot 应用中,读写分离建议使用两个数据源,分别是读的数据源和写的数据源,写的数据源必须支持可读可写。

在应用中,在某些场景下我们需要从只读数据源读取数据的时候,通过 DAO.use('只读数据源的名称').find(...) 就可以。

分库分表

Jboot 的分库分表功能使用了 Sharding-jdbc 实现的,若在 Jboot 应用在需要用到分库分表功能,需要添加 jboot.datasource.shardingConfigYaml = xxx.yaml 的配置,其中 xxx.yaml 配置需要放在 classpath 目录下,配置内容参考:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-yaml/

注意: 当在 jboot.properties 文件配置 jboot.datasource.shardingConfigYaml = xxx.yaml之后,不再需要在 jboot.properties 配置 jboot.datasource.urljboot.datasource.userjboot.datasource.password 等,这些配置都转义到 xxx.yaml 里进行配置了。

分布式事务

Jboot 的分布式事务依赖 Seata 来进行实现,在开始分布式事务之前,请先做好 Seata 的相关配置。

  • 创建 Seata 数据库
  • 启动 Seata

参考:https://github.com/seata/seata/wiki/Quick-Start

正常启动 Seata 之后,需要在 jboot.properties 配置文件添加如下配置

  1. jboot.rpc.filter = seata
  2. jboot.rpc.type = dubbo
  3. jboot.seata.enable = true
  4. jboot.seata.failureHandler = com.alibaba.io.seata.tm.api.DefaultFailureHandlerImpl
  5. jboot.seata.applicationId = Dubbo_Seata_Account_Service
  6. jboot.seata.txServiceGroup = dubbo_seata_tx_group

同时,在 resource 目录下添加 registry.conf 文件,用于对 seata 进行 registry 配置,内容如下:

  1. registry {
  2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  3. type = "file"
  4. nacos {
  5. serverAddr = "localhost"
  6. namespace = ""
  7. cluster = "default"
  8. }
  9. eureka {
  10. serviceUrl = "http://localhost:8761/eureka"
  11. application = "default"
  12. weight = "1"
  13. }
  14. redis {
  15. serverAddr = "localhost:6379"
  16. db = "0"
  17. }
  18. zk {
  19. cluster = "default"
  20. serverAddr = "127.0.0.1:2181"
  21. session.timeout = 6000
  22. connect.timeout = 2000
  23. }
  24. consul {
  25. cluster = "default"
  26. serverAddr = "127.0.0.1:8500"
  27. }
  28. etcd3 {
  29. cluster = "default"
  30. serverAddr = "http://localhost:2379"
  31. }
  32. sofa {
  33. serverAddr = "127.0.0.1:9603"
  34. application = "default"
  35. region = "DEFAULT_ZONE"
  36. datacenter = "DefaultDataCenter"
  37. cluster = "default"
  38. group = "SEATA_GROUP"
  39. addressWaitTime = "3000"
  40. }
  41. file {
  42. name = "file.conf"
  43. }
  44. }
  45. config {
  46. # file、nacos 、apollo、zk、consul、etcd3
  47. type = "file"
  48. nacos {
  49. serverAddr = "localhost"
  50. namespace = ""
  51. }
  52. consul {
  53. serverAddr = "127.0.0.1:8500"
  54. }
  55. apollo {
  56. app.id = "seata-server"
  57. apollo.meta = "http://192.168.1.204:8801"
  58. }
  59. zk {
  60. serverAddr = "127.0.0.1:2181"
  61. session.timeout = 6000
  62. connect.timeout = 2000
  63. }
  64. etcd3 {
  65. serverAddr = "http://localhost:2379"
  66. }
  67. file {
  68. name = "file.conf"
  69. }
  70. }

PS:如果不想叫 registry.conf ,请在环境变量里配置 seata.config.name = yourname ,那么可以使用 yourname.conf 代替 registry.conf。

同时,在 resource 目录下添加 file.conf 文件,内容如下:

  1. transport {
  2. # tcp udt unix-domain-socket
  3. type = "TCP"
  4. #NIO NATIVE
  5. server = "NIO"
  6. #enable heartbeat
  7. heartbeat = true
  8. #thread factory for netty
  9. thread-factory {
  10. boss-thread-prefix = "NettyBoss"
  11. worker-thread-prefix = "NettyServerNIOWorker"
  12. server-executor-thread-prefix = "NettyServerBizHandler"
  13. share-boss-worker = false
  14. client-selector-thread-prefix = "NettyClientSelector"
  15. client-selector-thread-size = 1
  16. client-worker-thread-prefix = "NettyClientWorkerThread"
  17. # netty boss thread size,will not be used for UDT
  18. boss-thread-size = 1
  19. #auto default pin or 8
  20. worker-thread-size = 8
  21. }
  22. shutdown {
  23. # when destroy server, wait seconds
  24. wait = 3
  25. }
  26. serialization = "seata"
  27. compressor = "none"
  28. }
  29. service {
  30. #vgroup->rgroup
  31. vgroup_mapping.dubbo_seata_tx_group = "default"
  32. #only support single node
  33. default.grouplist = "127.0.0.1:8091"
  34. #degrade current not support
  35. enableDegrade = false
  36. #disable
  37. disable = false
  38. #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  39. max.commit.retry.timeout = "-1"
  40. max.rollback.retry.timeout = "-1"
  41. }
  42. client {
  43. async.commit.buffer.limit = 10000
  44. lock {
  45. retry.internal = 10
  46. retry.times = 30
  47. }
  48. report.retry.count = 5
  49. tm.commit.retry.count = 1
  50. tm.rollback.retry.count = 1
  51. }
  52. ## transaction log store
  53. store {
  54. ## store mode: file、db
  55. mode = "file"
  56. ## file store
  57. file {
  58. dir = "sessionStore"
  59. # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
  60. max-branch-session-size = 16384
  61. # globe session size , if exceeded throws exceptions
  62. max-global-session-size = 512
  63. # file buffer size , if exceeded allocate new buffer
  64. file-write-buffer-cache-size = 16384
  65. # when recover batch read size
  66. session.reload.read_size = 100
  67. # async, sync
  68. flush-disk-mode = async
  69. }
  70. ## database store
  71. db {
  72. ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
  73. datasource = "dbcp"
  74. ## mysql/oracle/h2/oceanbase etc.
  75. db-type = "mysql"
  76. driver-class-name = "com.mysql.jdbc.Driver"
  77. url = "jdbc:mysql://127.0.0.1:3306/seata"
  78. user = "mysql"
  79. password = "mysql"
  80. min-conn = 1
  81. max-conn = 3
  82. global.table = "global_table"
  83. branch.table = "branch_table"
  84. lock-table = "lock_table"
  85. query-limit = 100
  86. }
  87. }
  88. lock {
  89. ## the lock store mode: local、remote
  90. mode = "remote"
  91. local {
  92. ## store locks in user's database
  93. }
  94. remote {
  95. ## store locks in the seata's server
  96. }
  97. }
  98. recovery {
  99. #schedule committing retry period in milliseconds
  100. committing-retry-period = 1000
  101. #schedule asyn committing retry period in milliseconds
  102. asyn-committing-retry-period = 1000
  103. #schedule rollbacking retry period in milliseconds
  104. rollbacking-retry-period = 1000
  105. #schedule timeout retry period in milliseconds
  106. timeout-retry-period = 1000
  107. }
  108. transaction {
  109. undo.data.validation = true
  110. undo.log.serialization = "jackson"
  111. undo.log.save.days = 7
  112. #schedule delete expired undo_log in milliseconds
  113. undo.log.delete.period = 86400000
  114. undo.log.table = "undo_log"
  115. }
  116. ## metrics settings
  117. metrics {
  118. enabled = false
  119. registry-type = "compact"
  120. # multi exporters use comma divided
  121. exporter-list = "prometheus"
  122. exporter-prometheus-port = 9898
  123. }
  124. support {
  125. ## spring
  126. spring {
  127. # auto proxy the DataSource bean
  128. datasource.autoproxy = false
  129. }
  130. }

注意:1、jboot.seata.txServiceGroup 配置的值要注意和 file.conf 里的 vgroup_mapping.xxx 保持一致2、jboot.rpc.filter=seata ##seata在Dubbo中的事务传播过滤器

以上配置完毕后如何使用呢?点击 这里 查看代码实例。