接口 Dialect
-
- 所有超级接口:
Cloneable,Serializable
- 所有已知实现类:
AnsiDialect,DmDialect,MysqlDialect,PostgresqlDialect
public interface Dialect extends Serializable, Cloneable
SQL方言由于不同数据库间SQL语句的差异,导致无法统一拼接SQL,Dialect接口旨在根据不同的数据库,使用不同的方言实现类,来拼接对应的SQL。
设计模式借鉴于hutool-db
- 从以下版本开始:
- 2020年6月13日
- 作者:
- ylyue
-
-
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 DialectcloneDialect()克隆方言DialectNameEnumdialectName()方言名JdbcPropertiesgetJdbcProperties()Db可配置属性org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplategetNamedParameterJdbcTemplate()命名参数JdbcTemplateStringgetPageJoinSql()获得用于SQL字符串拼接的SQL分页字符串WrappergetWrapper()包装器LonginsertOrUpdate(String tableName, com.alibaba.fastjson.JSONObject paramJson, String[] conditions, DbUpdateEnum dBUpdateEnum)插入或更新 表中必须存在数据唯一性约束<T> PageVO<T>page(String tableName, PageIPO pageIPO, SortEnum sortEnum, Class<T> mappedClass)单表分页查询<T> PageVO<T>pageSql(String querySql, PageIPO pageIPO, Class<T> mappedClass)复杂SQL分页查询<T> PageVO<T>pageSql(String countSql, String querySql, PageIPO pageIPO, Class<T> mappedClass)复杂SQL分页查询<T> PageVO<T>pageWhere(String tableName, String whereSql, PageIPO pageIPO, Class<T> mappedClass)单表分页查询voidsetJdbcProperties(JdbcProperties jdbcProperties)设置Db可配置属性voidsetNamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate namedParameterJdbcTemplate)设置命名参数JdbcTemplatevoidsetWrapper(Wrapper wrapper)设置包装器PagetoPage(PageIPO pageIPO)转换为经过方言处理的分页查询参数,用于SQL分页查询
-
-
-
方法详细资料
-
dialectName
DialectNameEnum dialectName()
方言名- 返回:
- 方言名
-
cloneDialect
Dialect cloneDialect()
克隆方言- 返回:
- 克隆方言
-
getWrapper
Wrapper getWrapper()
包装器- 返回:
- 包装器
-
setWrapper
void setWrapper(Wrapper wrapper)
设置包装器- 参数:
wrapper- 包装器
-
getNamedParameterJdbcTemplate
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate getNamedParameterJdbcTemplate()
命名参数JdbcTemplate- 返回:
- 命名参数JdbcTemplate
-
setNamedParameterJdbcTemplate
void setNamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate namedParameterJdbcTemplate)
设置命名参数JdbcTemplate- 参数:
namedParameterJdbcTemplate- 命名参数JdbcTemplate
-
getJdbcProperties
JdbcProperties getJdbcProperties()
Db可配置属性- 返回:
- Db可配置属性
-
setJdbcProperties
void setJdbcProperties(JdbcProperties jdbcProperties)
设置Db可配置属性- 参数:
jdbcProperties- Db可配置属性
-
insertOrUpdate
Long insertOrUpdate(String tableName, com.alibaba.fastjson.JSONObject paramJson, String[] conditions, DbUpdateEnum dBUpdateEnum)
插入或更新 表中必须存在数据唯一性约束更新触发条件:此数据若存在唯一性约束则更新,否则便执行插入数据
MySQL执行示例:
INSERT INTO table (param1, param2, ...)
VALUES
(:param1, :param2, ...)
ON DUPLICATE KEY UPDATE
condition = condition + :condition, ...- 参数:
tableName- 表名paramJson- 插入或更新所用到的参数conditions- 更新条件(对应paramJson内的key值)dBUpdateEnum- 更新类型 DbUpdateEnum- 返回:
- 受影响的行数
-
getPageJoinSql
String getPageJoinSql()
获得用于SQL字符串拼接的SQL分页字符串- 返回:
- 用于SQL字符串拼接的SQL分页字符串(带具名参数的SQL字符串,非SQL拼接)
-
toPage
Page toPage(PageIPO pageIPO)
转换为经过方言处理的分页查询参数,用于SQL分页查询- 参数:
pageIPO- 分页查询参数- 返回:
- 经过方言处理的分页查询参数
-
page
<T> PageVO<T> page(String tableName, PageIPO pageIPO, SortEnum sortEnum, Class<T> mappedClass)
单表分页查询
阿里最优SQL示例:
SELECT a.* FROM 表1 a, (select id from 表1 where 条件 ORDER BY id LIMIT 100000,20 ) b where a.id=b.id
-
pageWhere
<T> PageVO<T> pageWhere(String tableName, String whereSql, PageIPO pageIPO, Class<T> mappedClass)
单表分页查询
阿里最优SQL示例:
SELECT a.* FROM 表1 a, (select id from 表1 where 条件 LIMIT 100000,20 ) b where a.id=b.id- 类型参数:
T- 泛型- 参数:
tableName- 表名whereSql- 自定义WHERE语句,若此参数为空,那么所有的条件参数,都将以等于的形式进行SQL拼接。
SQL示例:WHERE 条件pageIPO- 分页查询参数 PageIPOmappedClass- 映射类- 返回:
- count(总数),data(分页列表数据)
-
pageSql
<T> PageVO<T> pageSql(String querySql, PageIPO pageIPO, Class<T> mappedClass)
复杂SQL分页查询
阿里最优查询SQL示例:
SELECT a.* FROM 表1 a, (select id from 表1 where 条件 LIMIT :page, :limit) b where a.id=b.id- 类型参数:
T- 泛型- 参数:
querySql- 用于查询数据的sql语句pageIPO- 分页查询参数 PageIPOmappedClass- 映射类- 返回:
- count(总数),data(分页列表数据)
-
pageSql
<T> PageVO<T> pageSql(@Nullable String countSql, String querySql, PageIPO pageIPO, Class<T> mappedClass)
复杂SQL分页查询
统计SQL示例:
SELECT count(*) count FROM 表1 a, (select id from 表1 where 条件) b where a.id=b.id
阿里最优查询SQL示例:
SELECT a.* FROM 表1 a, (select id from 表1 where 条件 LIMIT :page, :limit) b where a.id=b.id- 参数:
countSql- 用于统计总数的sql语句 (注意:count(*)必须拥有count别名) 同时countSql可以为null表示不统计 可选参数querySql- 用于查询数据的sql语句pageIPO- 分页查询参数 PageIPOmappedClass- 映射类- 返回:
- count(总数),data(分页列表数据)
-
-