Java 手动拼接分页查询SQL语句

import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * TODO
 *
 */
public class PageSelectSqlTest {

    public static void main(String[] args) {
        String table = "T_student";
        Set<String> tableFieldSet = new HashSet<>();
        tableFieldSet.add("name");
        tableFieldSet.add("gender");
        tableFieldSet.add("age");
        String whereSql = "age >= 18";
        String dbType = "oracle";
        String sql = getSelectPageSql(table, tableFieldSet, 0, 2, whereSql, dbType);
        System.out.println(sql);
    }

    /**
     * 拼接分页查询SQL语句
     * @param table 数据库表名
     * @param tableFieldSet 要查询字段名集合
     * @param pageNum 页数
     * @param pageSize  每页查询数量
     * @param whereSql  where后面条件语句
     * @param dbType    数据库类型
     * @return
     */
    private static String getSelectPageSql(String table, Set<String> tableFieldSet, int pageNum, int pageSize, String whereSql, String dbType) {
        // 拼接SQL
        StringBuffer fieldSb = new StringBuffer();
        tableFieldSet.forEach(field -> { if (StringUtils.isNotBlank(field)) fieldSb.append(field).append(","); });
        String fieldStr = fieldSb.toString().substring(0, fieldSb.toString().lastIndexOf(","));
        StringBuffer sqlSb = new StringBuffer();
        int start = pageNum * pageSize;
        if ("sql_server".equalsIgnoreCase(dbType)) {
            sqlSb.append("select top ").append(pageSize).append(" ").append(fieldStr).append(" from ").append(table);
            if (pageNum == 0) {
                if (StringUtils.isNotBlank(whereSql)) {
                    sqlSb.append(" where ").append(whereSql);
                }
            } else {
                sqlSb.append(" t1 where t1.id > (select max(a.id) from (select top ").append(start).append(" t2.id from ").append(table).append(" t2");
                if (StringUtils.isNotBlank(whereSql)) {
                    sqlSb.append(" where ").append(whereSql);
                }
                sqlSb.append(" order by id) a)");
                if (StringUtils.isNotBlank(whereSql)) {
                    sqlSb.append(" and ").append(whereSql);
                }
            }
            sqlSb.append(" order by id");
        } else if ("mysql".equalsIgnoreCase(dbType)) {
            sqlSb.append("select ").append(fieldStr).append(" from " ).append(table);
            if (StringUtils.isNotBlank(whereSql)) {
                sqlSb.append(" where ").append(whereSql);
            }
            sqlSb.append(" order by id limit ").append(start).append(", ").append(pageSize);
        } else if ("oracle".equalsIgnoreCase(dbType)) {
            int end = (pageNum + 1) * pageSize;
            sqlSb.append("select ").append(fieldStr).append(" from (");
            sqlSb.append("select table1.*, ROWNUM rowno from (");
            sqlSb.append("select ").append(fieldStr).append(" from ").append(table);
            if (StringUtils.isNotBlank(whereSql)) {
                sqlSb.append(" where ").append(whereSql);
            }
            sqlSb.append(" order by id) table1 where ROWNUM <= ").append(end);
            sqlSb.append(") table2 where table2.rowno > ").append(start);
        } else if ("db2".equalsIgnoreCase(dbType)) {
            int end = (pageNum + 1) * pageSize;
            sqlSb.append("select ").append(fieldStr).append(" from (");
            sqlSb.append("select row_number() over() as rownum, ").append(fieldStr);
            sqlSb.append(" from ").append(table);
            if (StringUtils.isNotBlank(whereSql)) {
                sqlSb.append(" where ").append(whereSql);
            }
            sqlSb.append(" order by id) table1 where rownum > ").append(start).append(" and rownum <= ").append(end);
        }

        return sqlSb.toString();
    }
}

  测试结果:

--sqlserver
select top 2 gender,name,age from T_student where age >= 18 order by id

select top 2 gender,name,age from T_student where id > (
	select max(a.id) from (select top 2 t1.id from T_student t1 where age >= 18 order by id) a
	) and age >= 18 order by id
	
--mysql
select gender,name,age from T_student where age >= 18 order by id limit 0, 2

--orcale
select gender,name,age from (
	select table1.*, ROWNUM rowno from (
		select gender,name,age from T_student where age >= 18 order by id
	) table1 where ROWNUM <= 2
) table2 where table2.rowno > 0

--db2
select gender,name,age from (
	select row_number() over() as rownum,gender,name,age from T_student where age >= 18 order by id
) table1 where rownum > 0 and rownum <= 2

  

Java 手动拼接分页查询SQL语句

上一篇:06 Spark SQL 及其DataFrame的基本操作


下一篇:JetBrains出品,一款好用到爆的数据库工具