This commit is contained in:
redkale
2023-12-29 23:19:10 +08:00
parent 263aa86211
commit fe6a5dd91c
3 changed files with 105 additions and 4 deletions

View File

@@ -1,2 +1,100 @@
# DB数据源
文档完善中……
## 配置数据源
```properties
redkale.datasource.platf.url = jdbc:mysql://127.0.0.1:3306/platf?serverTimezone=UTC&characterEncoding=utf8
redkale.datasource.platf.user = root
redkale.datasource.platf.password = pwd123
```
## 增删改
```java
@Data
public class Account {
//男
public static final short GENDER_MALE = 1;
//女
public static final short GENDER_FEMALE = 2;
@Id
@Column(name = "account_id")
private String accountid;
@Column(name = "account_name")
private String accountName;
private int age;
private short gender;
private String remark;
@Column(name = "create_time", updatable = false)
private long createTime;
}
```
  新增实体对象:
```java
//新增单个
Account account = new Account();
account.setAccountid("account1");
account.setAccountName("Hello");
account.setCreateTime(System.currentTimeMillis());
source.insert(account);
//异步新增多个
Account a1 = new Account();
a1.setAccountid("account1");
a1.setAccountName("Hello1");
a1.setCreateTime(System.currentTimeMillis());
Account a2 = new Account();
a2.setAccountid("account2");
a2.setAccountName("Hello2");
a2.setCreateTime(System.currentTimeMillis());
source.insertAsync(a1, a2);
```
  修改实体对象:
```java
//更新单个字段
source.updateColumn(Account.class, "account1", Account::getRemark, "新备注");
//更新多个字段
source.updateColumn(Account.class, "account1",
ColumnValue.set(Account::getAccountName, "新名称"),
ColumnValue.set(Account::getRemark, "新备注"),
ColumnValue.inc(Account::getAge, 2)); //年龄+2
//更新多个字段
Account account = new Account();
account.setAccountid("account1");
account.setAccountName("新名称");
account.setRemark("新备注");
source.updateColumn(account, "accountName", "remark");
//或者
source.updateColumn(account, Account::getAccountName, Account::getRemark);
//更新整个对象
Account one = new Account();
one.setAccountid("account1");
one.setAccountName("Hello1");
one.setCreateTime(System.currentTimeMillis());
source.update(one); //createTime不会被更新因字段设置了@Column(updatable=false)
//过滤更新
source.updateColumn(Account.class, FilterNodes.lt(Account::getAge, 16),
ColumnValue.set(Account::getRemark, "不满16岁是青少年"));
```
  删除实体:
```java
//根据主键值删除
Account account = new Account();
account.setAccountid("account1");
source.delete(account);
//过滤删除, 删除16以下男生
source.delete(Account.class, FilterNodes.lt(Account::getAge, 16).and("gender", GENDER_MALE));
```

View File

@@ -22,12 +22,14 @@ public class UserService implements Service {
@Resource(name = "platf")
private DataSource source;
@RestMapping(auth = true, comment = "更改密码")
//请求url: /user/updatePwd?bean={}
@RestMapping(auth = true, methods = "POST", comment = "更改密码(只能POST请求)")
public RetResult<String> updatePwd(@RestUserid long userid, UserPwdBean bean) {
//逻辑处理
return RetResult.success();
}
//请求url: /user/updateIntro?intro=xxx
@RestMapping(auth = true, comment = "更新用户介绍")
public RetResult<String> updateIntro(@RestUserid long userid, String intro) {
intro = Utility.orElse(intro, ""); //为null则用""
@@ -36,6 +38,7 @@ public class UserService implements Service {
return RetResult.success();
}
//请求url: /user/updateGender?gender=1
@RestMapping(auth = true, comment = "修改用户性别(异步方法)")
public CompletableFuture<RetResult<String>> updateGender(@RestUserid long userid, short gender) {
if (gender != GENDER_MALE && gender != GENDER_FEMALE) {