Explorar el Código

!55 【feature】DB操作模块新增count接口
Merge pull request !55 from 一米阳光/master

小东 hace 2 años
padre
commit
d09753f7b1

+ 36 - 0
magic-api/src/main/java/org/ssssssss/magicapi/modules/db/SQLModule.java

@@ -589,6 +589,20 @@ public class SQLModule implements DynamicAttribute<SQLModule, SQLModule>, Dynami
 		return page(count, boundSql, page, null);
 	}
 
+	/**
+	 * 分页查询(手动传入count)
+	 */
+	@Comment("执行分页查询,并传入变量信息,分页`SQL`count")
+	public Object page(RuntimeContext runtimeContext,
+					   @Comment(name = "count", value = "总条数") int count,
+					   @Comment(name = "sqlOrXml", value = "查询语句") String sqlOrXml,
+					   @Comment(name = "limit", value = "限制条数") long limit,
+					   @Comment(name = "offset", value = "跳过条数") long offset,
+					   @Comment(name = "params", value = "变量信息") Map<String, Object> params) {
+		BoundSql boundSql = new BoundSql(runtimeContext, sqlOrXml, params, this);
+		return page(count, boundSql, new Page(limit, offset), null);
+	}
+
 	private Object page(int count, BoundSql boundSql, Page page, Dialect dialect) {
 		List<Map<String, Object>> list = null;
 		if (count > 0) {
@@ -611,6 +625,28 @@ public class SQLModule implements DynamicAttribute<SQLModule, SQLModule>, Dynami
 		return page(count, boundSql, page, dialect);
 	}
 
+	/**
+	 * 查询总条目数
+	 */
+	@Comment("查询总条目数")
+	public Integer count(RuntimeContext runtimeContext,
+							 @Comment(name = "sqlOrXml", value = "`SQL`语句或`xml`") String sqlOrXml) {
+		return count(runtimeContext, sqlOrXml, null);
+	}
+
+	/**
+	 * 查询总条目数
+	 */
+	@Comment("查询总条目数,并传入变量信息")
+	public Integer count(RuntimeContext runtimeContext,
+							 @Comment(name = "sqlOrXml", value = "`SQL`语句或`xml`") String sqlOrXml,
+							 @Comment(name = "params", value = "变量信息") Map<String, Object> params) {
+		BoundSql boundSql = new BoundSql(runtimeContext, sqlOrXml, params, this);
+		Dialect dialect = dataSourceNode.getDialect(dialectAdapter);
+		BoundSql countBoundSql = boundSql.copy(dialect.getCountSql(boundSql.getSql()));
+		return selectInt(countBoundSql);
+	}
+
 	/**
 	 * 查询int值
 	 */