mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
b5d9fe13f8
@ -28,14 +28,36 @@ List<Row> rows = Db.selectListBySql(sql,18);
|
||||
|
||||
|
||||
//分页查询:每页 10 条数据,查询第 3 页的年龄大于 18 的用户
|
||||
QueryWrapper query = QueryWrapper.create()
|
||||
.where(ACCOUNT.AGE.ge(18));
|
||||
Page<Row> rowPage = Db.paginate("tb_account",3,10,query);
|
||||
QueryWrapper query=QueryWrapper.create()
|
||||
.where(ACCOUNT.AGE.ge(18));
|
||||
Page<Row> rowPage=Db.paginate("tb_account",3,10,query);
|
||||
```
|
||||
|
||||
> Db 工具类还提供了更多 增、删、改、查和分页查询等方法。
|
||||
>
|
||||
> 具体参考: [Db.java](https://gitee.com/mybatis-flex/mybatis-flex/blob/main/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java) 。
|
||||
>
|
||||
具体参考: [Db.java](https://gitee.com/mybatis-flex/mybatis-flex/blob/main/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java) 。
|
||||
|
||||
## DbChain 链式 Db 调用
|
||||
|
||||
使用 `DbChain` 之后无需将 `QueryWrapper` 与 `Row` 的构建分离,直接即可进行操作。
|
||||
|
||||
```java
|
||||
// 新增 Row 构建
|
||||
DbChain.table("tb_account")
|
||||
.set(RowKey.AUTO)
|
||||
.set("user_name","王帅")
|
||||
.set("age",18)
|
||||
.set("birthday",new Date())
|
||||
.save();
|
||||
|
||||
// 查询 QueryWrapper 构建
|
||||
DbChain.table("tb_account")
|
||||
.select("id","user_name","age","birthday")
|
||||
.where("age > ?",18)
|
||||
.list()
|
||||
.forEach(System.out::println);
|
||||
```
|
||||
|
||||
## Row.toEntity()
|
||||
|
||||
@ -45,7 +67,7 @@ Page<Row> rowPage = Db.paginate("tb_account",3,10,query);
|
||||
代码示例:
|
||||
|
||||
```java
|
||||
Row row = Db.selectOneBySql("select * from ....");
|
||||
Row row=Db.selectOneBySql("select * from ....");
|
||||
Account entity = row.toEntity(Account.class);
|
||||
```
|
||||
|
||||
@ -125,7 +147,7 @@ System.out.println(articles);
|
||||
public class Other {
|
||||
private String id;
|
||||
private String userName;
|
||||
|
||||
|
||||
//getter setter
|
||||
}
|
||||
```
|
||||
|
||||
@ -57,11 +57,22 @@ public class DbChain extends QueryWrapperAdapter<DbChain> {
|
||||
|
||||
/**
|
||||
* 覆盖 {@link QueryWrapper} 的静态方法,仅用于查询,必须使用 {@code from(...)} 方法指定表。
|
||||
*
|
||||
* @deprecated 使用 {@code table(...)} 方法创建
|
||||
*/
|
||||
@Deprecated
|
||||
public static DbChain create() {
|
||||
return new DbChain();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 覆盖 {@link QueryWrapper} 的静态方法
|
||||
*/
|
||||
@Deprecated
|
||||
public static DbChain create(Object entity) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static DbChain table(String tableName) {
|
||||
return new DbChain(tableName);
|
||||
}
|
||||
@ -86,12 +97,12 @@ public class DbChain extends QueryWrapperAdapter<DbChain> {
|
||||
return rowData;
|
||||
}
|
||||
|
||||
public DbChain set(RowKey rowKey) {
|
||||
public DbChain setId(RowKey rowKey) {
|
||||
getRow().getPrimaryKeys().add(rowKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DbChain set(RowKey rowKey, Object value) {
|
||||
public DbChain setId(RowKey rowKey, Object value) {
|
||||
getRow().getPrimaryKeys().add(rowKey);
|
||||
getRow().put(rowKey.keyColumn, value);
|
||||
return this;
|
||||
@ -136,7 +147,7 @@ public class DbChain extends QueryWrapperAdapter<DbChain> {
|
||||
return SqlUtil.toBool(Db.updateById(schema, tableName, getRow()));
|
||||
}
|
||||
|
||||
private Row toRow(Object entity) {
|
||||
private static Row toRow(Object entity) {
|
||||
Class<?> entityClass = entity.getClass();
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
|
||||
Row row = new Row();
|
||||
|
||||
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import com.mybatisflex.core.audit.AuditManager;
|
||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.row.DbChain;
|
||||
import com.mybatisflex.core.row.Row;
|
||||
import com.mybatisflex.core.row.RowKey;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author 王帅
|
||||
* @since 2023-07-23
|
||||
*/
|
||||
public class DbChainTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
DataSource dataSource = new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.addScript("schema.sql")
|
||||
.addScript("data.sql")
|
||||
.build();
|
||||
|
||||
MybatisFlexBootstrap.getInstance()
|
||||
.setDataSource(dataSource)
|
||||
.start();
|
||||
|
||||
AuditManager.setAuditEnable(true);
|
||||
AuditManager.setMessageCollector(new ConsoleMessageCollector());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSave() {
|
||||
boolean saved = DbChain.table("tb_account")
|
||||
.setId(RowKey.AUTO)
|
||||
.set("user_name", "王帅")
|
||||
.set("age", 18)
|
||||
.set("birthday", new Date())
|
||||
.save();
|
||||
|
||||
Row row = DbChain.table("tb_account")
|
||||
.where("user_name = ?", "王帅")
|
||||
.one();
|
||||
|
||||
System.out.println(row);
|
||||
|
||||
assertTrue(saved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
boolean updated = DbChain.table("tb_account")
|
||||
.setId(RowKey.AUTO, 1)
|
||||
.set("age", 1000)
|
||||
.updateById();
|
||||
|
||||
Row row = DbChain.table("tb_account")
|
||||
.where("id = ?", 1)
|
||||
.one();
|
||||
|
||||
System.out.println(row);
|
||||
|
||||
assertTrue(updated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
DbChain.table("tb_account")
|
||||
.where("id = ?", 1)
|
||||
.remove();
|
||||
|
||||
long count = DbChain.table("tb_account").count();
|
||||
|
||||
assertEquals(1, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testList() {
|
||||
DbChain.table("tb_account")
|
||||
.list()
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user