This commit is contained in:
Looly 2022-04-30 22:56:23 +08:00
parent 40aa543edd
commit a3d5385efc
2 changed files with 16 additions and 31 deletions

View File

@ -867,21 +867,6 @@ public abstract class AbstractDb<R extends AbstractDb<R>> implements ConnectionH
} }
} }
/**
* 分页查询<br>
* 查询条件为多个key value对表示默认key = value如果使用其它条件可以使用where.put("key", " &gt; 1")value也可以传Condition对象key被忽略
*
* @param where 条件实体类包含表名
* @param page 页码
* @param numPerPage 每页条目数
* @return 分页结果集
* @throws DbRuntimeException SQL执行异常
* @since 3.2.2
*/
public PageResult<Entity> page(final Entity where, final int page, final int numPerPage) throws DbRuntimeException {
return this.page(where, new Page(page, numPerPage));
}
/** /**
* 分页查询<br> * 分页查询<br>
* 查询条件为多个key value对表示默认key = value如果使用其它条件可以使用where.put("key", " &gt; 1")value也可以传Condition对象key被忽略 * 查询条件为多个key value对表示默认key = value如果使用其它条件可以使用where.put("key", " &gt; 1")value也可以传Condition对象key被忽略

View File

@ -21,28 +21,28 @@ public class DbTest {
@Test @Test
public void queryTest() { public void queryTest() {
final List<Entity> find = Db.of().query("select * from ofr where age = ?", 18); final List<Entity> find = Db.of().query("select * from user where age = ?", 18);
Assert.assertEquals("王五", find.get(0).get("name")); Assert.assertEquals("王五", find.get(0).get("name"));
} }
@Test @Test
public void findTest() { public void findTest() {
final List<Entity> find = Db.of().find(Entity.create("ofr").set("age", 18)); final List<Entity> find = Db.of().find(Entity.create("user").set("age", 18));
Assert.assertEquals("王五", find.get(0).get("name")); Assert.assertEquals("王五", find.get(0).get("name"));
} }
@Test @Test
public void pageTest() { public void pageTest() {
// 测试数据库中一共4条数据第0页有3条第1页有1条 // 测试数据库中一共4条数据第0页有3条第1页有1条
final List<Entity> page0 = Db.of().page(Entity.create("ofr"), 0, 3); final List<Entity> page0 = Db.of().page(Entity.create("user"), Page.of(0, 3));
Assert.assertEquals(3, page0.size()); Assert.assertEquals(3, page0.size());
final List<Entity> page1 = Db.of().page(Entity.create("ofr"), 1, 3); final List<Entity> page1 = Db.of().page(Entity.create("user"), Page.of(1, 3));
Assert.assertEquals(1, page1.size()); Assert.assertEquals(1, page1.size());
} }
@Test @Test
public void pageTest2() { public void pageTest2() {
final String sql = "select * from ofr order by name"; final String sql = "select * from user order by name";
// 测试数据库中一共4条数据第0页有3条第1页有1条 // 测试数据库中一共4条数据第0页有3条第1页有1条
final List<Entity> page0 = Db.of().page( final List<Entity> page0 = Db.of().page(
sql, Page.of(0, 3)); sql, Page.of(0, 3));
@ -55,7 +55,7 @@ public class DbTest {
@Test @Test
public void pageWithParamsTest() { public void pageWithParamsTest() {
final String sql = "select * from ofr where name = ?"; final String sql = "select * from user where name = ?";
final PageResult<Entity> result = Db.of().page( final PageResult<Entity> result = Db.of().page(
sql, Page.of(0, 3), "张三"); sql, Page.of(0, 3), "张三");
@ -66,50 +66,50 @@ public class DbTest {
@Test @Test
public void countTest() { public void countTest() {
final long count = Db.of().count("select * from ofr"); final long count = Db.of().count("select * from user");
Assert.assertEquals(4, count); Assert.assertEquals(4, count);
} }
@Test @Test
public void countTest2() { public void countTest2() {
final long count = Db.of().count("select * from ofr order by name DESC"); final long count = Db.of().count("select * from user order by name DESC");
Assert.assertEquals(4, count); Assert.assertEquals(4, count);
} }
@Test @Test
public void findLikeTest() { public void findLikeTest() {
// 方式1 // 方式1
List<Entity> find = Db.of().find(Entity.create("ofr").set("name", "like 王%")); List<Entity> find = Db.of().find(Entity.create("user").set("name", "like 王%"));
Assert.assertEquals("王五", find.get(0).get("name")); Assert.assertEquals("王五", find.get(0).get("name"));
// 方式2 // 方式2
find = Db.of().findLike("ofr", "name", "", Condition.LikeType.StartWith); find = Db.of().findLike("user", "name", "", Condition.LikeType.StartWith);
Assert.assertEquals("王五", find.get(0).get("name")); Assert.assertEquals("王五", find.get(0).get("name"));
// 方式3 // 方式3
find = Db.of().query("select * from ofr where name like ?", "王%"); find = Db.of().query("select * from user where name like ?", "王%");
Assert.assertEquals("王五", find.get(0).get("name")); Assert.assertEquals("王五", find.get(0).get("name"));
} }
@Test @Test
public void findByTest() { public void findByTest() {
final List<Entity> find = Db.of().findBy("ofr", final List<Entity> find = Db.of().findBy("user",
Condition.parse("age", "> 18"), Condition.parse("age", "> 18"),
Condition.parse("age", "< 100") Condition.parse("age", "< 100")
); );
for (final Entity entity : find) { for (final Entity entity : find) {
StaticLog.debug("{}", entity); StaticLog.debug("{}", entity);
} }
Assert.assertEquals("unitTestofr", find.get(0).get("name")); Assert.assertEquals("unitTestUser", find.get(0).get("name"));
} }
@Test @Test
@Ignore @Ignore
public void txTest() throws SQLException { public void txTest() throws SQLException {
Db.of().tx(db -> { Db.of().tx(db -> {
db.insert(Entity.create("ofr").set("name", "unitTestofr2")); db.insert(Entity.create("user").set("name", "unitTestuser2"));
db.update(Entity.create().set("age", 79), Entity.create("ofr").set("name", "unitTestofr2")); db.update(Entity.create().set("age", 79), Entity.create("user").set("name", "unitTestuser2"));
db.del("ofr", "name", "unitTestofr2"); db.del("user", "name", "unitTestuser2");
}); });
} }