mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
a3d5385efc
commit
4613d47c84
@ -6,8 +6,8 @@ import cn.hutool.db.dialect.Dialect;
|
||||
import cn.hutool.db.handler.BeanListHandler;
|
||||
import cn.hutool.db.handler.EntityHandler;
|
||||
import cn.hutool.db.handler.EntityListHandler;
|
||||
import cn.hutool.db.handler.ResultSetUtil;
|
||||
import cn.hutool.db.handler.NumberHandler;
|
||||
import cn.hutool.db.handler.ResultSetUtil;
|
||||
import cn.hutool.db.handler.RsHandler;
|
||||
import cn.hutool.db.handler.StringHandler;
|
||||
import cn.hutool.db.sql.Condition;
|
||||
@ -673,10 +673,21 @@ public abstract class AbstractDb<R extends AbstractDb<R>> implements ConnectionH
|
||||
* @throws DbRuntimeException SQL执行异常
|
||||
*/
|
||||
public long count(final Entity where) throws DbRuntimeException {
|
||||
return count(where, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 结果的条目数
|
||||
*
|
||||
* @param where 查询条件
|
||||
* @return 复合条件的结果数
|
||||
* @throws DbRuntimeException SQL执行异常
|
||||
*/
|
||||
public long count(final Entity where, final Page page) throws DbRuntimeException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
return runner.count(conn, Query.of(where));
|
||||
return runner.count(conn, Query.of(where).setPage(page));
|
||||
} finally {
|
||||
this.closeConnection(conn);
|
||||
}
|
||||
|
@ -134,8 +134,7 @@ public interface Dialect extends Serializable {
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
default PreparedStatement psForCount(final Connection conn, final Query query) throws SQLException {
|
||||
query.setFields(ListUtil.toList("count(1)"));
|
||||
return psForFind(conn, query);
|
||||
return psForFind(conn, query.clone().setFields(ListUtil.toList("count(1)")));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,8 +2,8 @@ package cn.hutool.db.dialect.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.Page;
|
||||
|
@ -47,6 +47,6 @@ public class EntityHandler implements RsHandler<Entity>{
|
||||
final ResultSetMetaData meta = rs.getMetaData();
|
||||
final int columnCount = meta.getColumnCount();
|
||||
|
||||
return rs.next() ? ResultSetUtil.toBean(columnCount, meta, rs, this.caseInsensitive) : null;
|
||||
return rs.next() ? ResultSetUtil.toEntity(columnCount, meta, rs, this.caseInsensitive) : null;
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ public class ResultSetUtil {
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public static Entity toEntity(final int columnCount, final ResultSetMetaData meta, final ResultSet rs) throws SQLException {
|
||||
return toBean(columnCount, meta, rs, false);
|
||||
return toEntity(columnCount, meta, rs, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -130,8 +130,8 @@ public class ResultSetUtil {
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 4.5.16
|
||||
*/
|
||||
public static Entity toBean(final int columnCount, final ResultSetMetaData meta, final ResultSet rs, final boolean caseInsensitive) throws SQLException {
|
||||
return toBean(new Entity(null, caseInsensitive), columnCount, meta, rs, true);
|
||||
public static Entity toEntity(final int columnCount, final ResultSetMetaData meta, final ResultSet rs, final boolean caseInsensitive) throws SQLException {
|
||||
return toEntity(new Entity(null, caseInsensitive), columnCount, meta, rs, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -147,7 +147,7 @@ public class ResultSetUtil {
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 3.3.1
|
||||
*/
|
||||
public static <T extends Entity> T toBean(final T row, final int columnCount, final ResultSetMetaData meta, final ResultSet rs, final boolean withMetaInfo) throws SQLException {
|
||||
public static <T extends Entity> T toEntity(final T row, final int columnCount, final ResultSetMetaData meta, final ResultSet rs, final boolean withMetaInfo) throws SQLException {
|
||||
int type;
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
type = meta.getColumnType(i);
|
||||
@ -226,7 +226,7 @@ public class ResultSetUtil {
|
||||
final int columnCount = meta.getColumnCount();
|
||||
|
||||
while (rs.next()) {
|
||||
collection.add(ResultSetUtil.toBean(columnCount, meta, rs, caseInsensitive));
|
||||
collection.add(toEntity(columnCount, meta, rs, caseInsensitive));
|
||||
}
|
||||
|
||||
return collection;
|
||||
|
@ -16,7 +16,7 @@ import java.util.Collection;
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class Query {
|
||||
public class Query implements Cloneable {
|
||||
|
||||
/** 查询的字段名列表 */
|
||||
Collection<String> fields;
|
||||
@ -190,4 +190,13 @@ public class Query {
|
||||
}
|
||||
return this.tableNames[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query clone() {
|
||||
try {
|
||||
return (Query) super.clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,12 +36,22 @@ public class DbTest {
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(Entity.create("user"), Page.of(0, 3));
|
||||
Assert.assertEquals(3, page0.size());
|
||||
|
||||
final List<Entity> page1 = Db.of().page(Entity.create("user"), Page.of(1, 3));
|
||||
Assert.assertEquals(1, page1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageTest2() {
|
||||
final long count = Db.of().count(Entity.create("user"), Page.of(1, 3));
|
||||
Assert.assertEquals(1, count);
|
||||
|
||||
final List<Entity> page1 = Db.of().page(Entity.create("user"), Page.of(1, 3));
|
||||
Assert.assertEquals(1, page1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageBySqlTest() {
|
||||
final String sql = "select * from user order by name";
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(
|
||||
|
Loading…
x
Reference in New Issue
Block a user