优化RowBound
This commit is contained in:
@@ -2,19 +2,19 @@
|
||||
  ```DataSqlSource```是```DataSource```的SQL扩展类,增强了对原生SQL的操作。
|
||||
|
||||
## SQL模板
|
||||
  Redkale中的SQL模板与Mybatis里的SQL模板用法类似, 最大区别在于不需要写很多```if/else```判断语句,也不用写xml文件, 框架会根据参数存在与否动态生成sql语句, 查询结果时带下划线的sql字段名和类中的驼峰字段名会自动匹配。
|
||||
  Redkale中的SQL模板与Mybatis里的SQL模板用法类似, 最大区别在于不需要写很多```if/else```判断语句,也不用写xml文件, 框架会根据参数存在与否动态生成sql语句, 查询结果时下划线式sql字段名和驼峰式类字段名会自动匹配。
|
||||
```sql
|
||||
SELECT * FROM user WHERE user_id IN ${bean.userIds} OR user_name = ${bean.userName}
|
||||
```
|
||||
  当bean.userIds=null,bean.userName='hello'时,sql语句转换成
|
||||
  当bean.userIds=null,bean.userName='hello'时,sql语句转换成:
|
||||
```sql
|
||||
SELECT * FROM user WHERE user_name = 'hello'
|
||||
```
|
||||
  当bean.userIds=[1,2,3],bean.userName=null时,sql语句转换成
|
||||
  当bean.userIds=[1,2,3],bean.userName=null时,sql语句转换成:
|
||||
```sql
|
||||
SELECT * FROM user WHERE user_id IN (1,2,3)
|
||||
```
|
||||
  IN或者NOT IN语句当参数不为null而是空数组或者空List会进行特殊处理,IN语句会转化成```1=1```, NOT IN语句会转化成```1=2```。 当bean.userIds=空数组,bean.userName='hello'时,sql语句转换成
|
||||
  IN或者NOT IN语句当参数不为null而是空数组或者空List会进行特殊处理,IN语句会转化成```1=1```, NOT IN语句会转化成```1=2```。 当bean.userIds=空数组,bean.userName='hello'时,sql语句转换成:
|
||||
```sql
|
||||
SELECT * FROM user WHERE 1=2 OR user_name = 'hello'
|
||||
```
|
||||
@@ -29,14 +29,14 @@
|
||||
public class ForumInfoService extends AbstractService {
|
||||
|
||||
//查询单个记录的sql
|
||||
private static final String findOneSql = "SELECT f.forum_groupid, s.forum_section_color "
|
||||
private static final String findSql = "SELECT f.forum_groupid, s.forum_section_color "
|
||||
+ "FROM forum_info f, forum_section s "
|
||||
+ " WHERE f.forumid = s.forumid AND "
|
||||
+ "s.forum_sectionid = ${bean.forumSectionid} AND "
|
||||
+ "f.forumid = ${bean.forumid} AND s.forum_section_color = ${bean.forumSectionColor}";
|
||||
|
||||
//查询列表记录的sql
|
||||
private static final String queryListSql = "SELECT f.forum_groupid, s.forum_section_color "
|
||||
private static final String querySql = "SELECT f.forum_groupid, s.forum_section_color "
|
||||
+ "FROM forum_info f, forum_section s "
|
||||
+ " WHERE f.forumid = s.forumid AND "
|
||||
+ "s.forum_sectionid = ${bean.forumSectionid} AND "
|
||||
@@ -46,17 +46,22 @@ public class ForumInfoService extends AbstractService {
|
||||
private DataSqlSource source;
|
||||
|
||||
public ForumResult findForumResultOne(ForumBean bean) {
|
||||
return source.nativeQueryOne(ForumResult.class, findOneSql, Map.of("bean", bean));
|
||||
return source.nativeQueryOne(ForumResult.class, findSql, Map.of("bean", bean));
|
||||
}
|
||||
|
||||
public CompletableFuture<List<ForumResult>> queryForumResultListAsync(ForumBean bean) {
|
||||
return source.nativeQueryListAsync(ForumResult.class, queryListSql, Map.of("bean", bean));
|
||||
return source.nativeQueryListAsync(ForumResult.class, querySql, Map.of("bean", bean));
|
||||
}
|
||||
|
||||
//翻页查询
|
||||
public Sheet<ForumResult> queryForumResult(RowBound bound, ForumBean bean){
|
||||
return source.nativeQuerySheet(ForumResult.class, querySql, round, Map.of("bean", bean));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## DataSqlMapper
|
||||
  DataSqlMapper与MyBatis里的Mapper用法类似。
|
||||
  DataSqlMapper与MyBatis里的Mapper用法类似,且都支持异步方法。
|
||||
```java
|
||||
public interface ForumInfoMapper extends BaseMapper<ForumInfo> {
|
||||
|
||||
@@ -76,6 +81,14 @@ public interface ForumInfoMapper extends BaseMapper<ForumInfo> {
|
||||
+ "AND s.forum_section_color = ${bean.forumSectionColor}")
|
||||
public CompletableFuture<ForumResult> findForumResultOneAsync(ForumBean bean);
|
||||
|
||||
//翻页查询
|
||||
@Sql("SELECT f.forum_groupid, s.forum_section_color "
|
||||
+ "FROM forum_info f, forum_section s "
|
||||
+ " WHERE f.forumid = s.forumid AND "
|
||||
+ "s.forum_sectionid = #{bean.forumSectionid} AND "
|
||||
+ "f.forumid = #{bean.forumid} AND s.forum_section_color = #{bean.forumSectionColor}")
|
||||
public Sheet<ForumResult> queryForumResult(RowBound bound, ForumBean bean);
|
||||
|
||||
@Sql("UPDATE forum_section s "
|
||||
+ " SET s.forum_sectionid = '' "
|
||||
+ " WHERE s.forum_section_color = $${bean.forumSectionColor}")
|
||||
|
||||
@@ -415,7 +415,7 @@ public final class ClassFilter<T> {
|
||||
* 例如:
|
||||
* *.platf.** 转成 ^(\w+)\.platf\.(.*)$
|
||||
*
|
||||
* @param regx
|
||||
* @param regx 正则表达式
|
||||
* @return Pattern
|
||||
*/
|
||||
public static String formatPackageRegx(String regx) {
|
||||
|
||||
@@ -116,7 +116,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
|
||||
}
|
||||
this.name = conf.getValue("name", "");
|
||||
this.enabled = conf.getBoolValue("enabled", true);
|
||||
this.schema = conf.getValue("schema", DEFAULT_SCHEMA);
|
||||
this.schema = checkSchema(conf.getValue("schema", DEFAULT_SCHEMA));
|
||||
if (this.enabled) {
|
||||
this.localSource.init(conf);
|
||||
String remoteSourceName = conf.getValue("remote", "");
|
||||
@@ -135,6 +135,23 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查name是否含特殊字符
|
||||
*
|
||||
* @param value 参数
|
||||
* @return
|
||||
*/
|
||||
protected String checkSchema(String value) {
|
||||
if (value != null && !value.isEmpty()) {
|
||||
for (char ch : value.toCharArray()) {
|
||||
if (ch == ':' || ch == '#' || ch == '@') { // 不能含特殊字符
|
||||
throw new RedkaleException("schema cannot contains : # @");
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy(AnyValue conf) {
|
||||
if (this.enabled) {
|
||||
|
||||
@@ -2877,7 +2877,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
return flipper;
|
||||
}
|
||||
if (maxLimit < 1) {
|
||||
maxLimit = org.redkale.source.Flipper.DEFAULT_LIMIT;
|
||||
maxLimit = org.redkale.source.Flipper.getDefaultLimit();
|
||||
}
|
||||
return new org.redkale.source.Flipper(maxLimit);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ public @interface Entity {
|
||||
/**
|
||||
* (Optional) 表名和字段名是否将驼峰式改成下划线式
|
||||
*
|
||||
* @since 2.8.0
|
||||
* @return boolean
|
||||
*/
|
||||
boolean camelCase() default false;
|
||||
|
||||
@@ -166,13 +166,13 @@ public interface DataSqlSource extends DataSource {
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
default <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, Flipper flipper) {
|
||||
return nativeQuerySheet(type, sql, flipper, Collections.emptyMap());
|
||||
default <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, RowBound round) {
|
||||
return nativeQuerySheet(type, sql, round, Collections.emptyMap());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
default <V> CompletableFuture<Sheet<V>> nativeQuerySheetAsync(Class<V> type, String sql, Flipper flipper) {
|
||||
return nativeQuerySheetAsync(type, sql, flipper, Collections.emptyMap());
|
||||
default <V> CompletableFuture<Sheet<V>> nativeQuerySheetAsync(Class<V> type, String sql, RowBound round) {
|
||||
return nativeQuerySheetAsync(type, sql, round, Collections.emptyMap());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
@@ -417,15 +417,15 @@ public interface DataSqlSource extends DataSource {
|
||||
Copier.copyToMap(bean, Copier.OPTION_SKIP_NULL_VALUE));
|
||||
}
|
||||
|
||||
default <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, Flipper flipper, Serializable bean) {
|
||||
default <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, RowBound round, Serializable bean) {
|
||||
return nativeQuerySheet(
|
||||
type, sql, flipper, (Map<String, Object>) Copier.copyToMap(bean, Copier.OPTION_SKIP_NULL_VALUE));
|
||||
type, sql, round, (Map<String, Object>) Copier.copyToMap(bean, Copier.OPTION_SKIP_NULL_VALUE));
|
||||
}
|
||||
|
||||
default <V> CompletableFuture<Sheet<V>> nativeQuerySheetAsync(
|
||||
Class<V> type, String sql, Flipper flipper, Serializable bean) {
|
||||
Class<V> type, String sql, RowBound round, Serializable bean) {
|
||||
return nativeQuerySheetAsync(
|
||||
type, sql, flipper, (Map<String, Object>) Copier.copyToMap(bean, Copier.OPTION_SKIP_NULL_VALUE));
|
||||
type, sql, round, (Map<String, Object>) Copier.copyToMap(bean, Copier.OPTION_SKIP_NULL_VALUE));
|
||||
}
|
||||
|
||||
default <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, PageBean pageBean) {
|
||||
|
||||
@@ -18,8 +18,6 @@ import org.redkale.convert.ConvertColumn;
|
||||
*/
|
||||
public final class Flipper extends RowBound {
|
||||
|
||||
public static int DEFAULT_LIMIT = 20;
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
@Comment("排序字段, 可多字段排序")
|
||||
private String sort = "";
|
||||
|
||||
@@ -13,12 +13,15 @@ import org.redkale.convert.ConvertColumn;
|
||||
*
|
||||
* <p>详情见: https://redkale.org
|
||||
*
|
||||
* @see org.redkale.source.Flipper
|
||||
* @see org.redkale.source.PageBean
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public class RowBound implements Serializable, Cloneable {
|
||||
|
||||
public static int DEFAULT_LIMIT = 20;
|
||||
private static int DEFAULT_LIMIT = 20;
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
@Comment("记录行的偏移量,从0开始")
|
||||
@@ -148,7 +151,17 @@ public class RowBound implements Serializable, Cloneable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static boolean validLimit(RowBound flipper) {
|
||||
return flipper != null && flipper.getLimit() > 0;
|
||||
public static boolean validLimit(RowBound round) {
|
||||
return round != null && round.getLimit() > 0;
|
||||
}
|
||||
|
||||
public static int getDefaultLimit() {
|
||||
return DEFAULT_LIMIT;
|
||||
}
|
||||
|
||||
public static void setDefaultLimit(int limit) {
|
||||
if (limit > 0) {
|
||||
DEFAULT_LIMIT = limit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.redkale.source.DataSqlSource;
|
||||
import org.redkale.source.RowBound;
|
||||
import org.redkale.util.Sheet;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class DynForumInfoMapperImpl implements ForumInfoMapper {
|
||||
@@ -25,6 +27,7 @@ public class DynForumInfoMapperImpl implements ForumInfoMapper {
|
||||
return dataSource().nativeQueryOne(ForumResult.class, sql, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ForumResult> findForumResultAsync(ForumBean bean) {
|
||||
String sql =
|
||||
"SELECT f.forum_groupid, s.forum_section_color FROM forum_info f, forum_section s WHERE f.forumid = s.forumid";
|
||||
@@ -42,6 +45,19 @@ public class DynForumInfoMapperImpl implements ForumInfoMapper {
|
||||
return dataSource().nativeQueryList(ForumResult.class, sql, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sheet<ForumResult> queryForumResult(RowBound bound, ForumBean bean) {
|
||||
String sql = "SELECT f.forum_groupid, s.forum_section_color "
|
||||
+ "FROM forum_info f, forum_section s "
|
||||
+ " WHERE f.forumid = s.forumid AND "
|
||||
+ "s.forum_sectionid = #{bean.forumSectionid} AND "
|
||||
+ "f.forumid = #{bean.forumid} AND s.forum_section_color = #{bean.forumSectionColor}";
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("bean", bean);
|
||||
return dataSource().nativeQuerySheet(ForumResult.class, sql, bound, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<ForumResult>> queryForumResultAsync(ForumBean bean) {
|
||||
String sql =
|
||||
"SELECT f.forum_groupid, s.forum_section_color FROM forum_info f, forum_section s WHERE f.forumid = s.forumid";
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.redkale.annotation.Param;
|
||||
import org.redkale.persistence.Sql;
|
||||
import org.redkale.source.RowBound;
|
||||
import org.redkale.util.Sheet;
|
||||
|
||||
/** @author zhangjx */
|
||||
public interface ForumInfoMapper extends BaseMapper<ForumInfo> {
|
||||
@@ -32,6 +34,13 @@ public interface ForumInfoMapper extends BaseMapper<ForumInfo> {
|
||||
+ "f.forumid = #{bean.forumid} AND s.forum_section_color = #{bean.forumSectionColor}")
|
||||
public List<ForumResult> queryForumResult(@Param("bean") ForumBean bean0);
|
||||
|
||||
@Sql("SELECT f.forum_groupid, s.forum_section_color "
|
||||
+ "FROM forum_info f, forum_section s "
|
||||
+ " WHERE f.forumid = s.forumid AND "
|
||||
+ "s.forum_sectionid = #{bean.forumSectionid} AND "
|
||||
+ "f.forumid = #{bean.forumid} AND s.forum_section_color = #{bean.forumSectionColor}")
|
||||
public Sheet<ForumResult> queryForumResult(RowBound bound, ForumBean bean);
|
||||
|
||||
@Sql("SELECT f.forum_groupid, s.forum_section_color "
|
||||
+ "FROM forum_info f, forum_section s "
|
||||
+ " WHERE f.forumid = s.forumid AND "
|
||||
|
||||
Reference in New Issue
Block a user