类 DbBase

  • 直接已知子类:
    AnsiDialect, Db

    public class DbBase
    extends Object

    SQL优化型数据库操作

    Created by sunJinChuan on 2016/6/6
    从以下版本开始:
    0.0.1
    作者:
    ylyue
    • 字段详细资料

      • dialect

        protected Dialect dialect
    • 构造器详细资料

      • DbBase

        public DbBase()
    • 方法详细资料

      • getDataSource

        public DataSource getDataSource()
        获得数据源
        返回:
        数据源
      • setDataSource

        public void setDataSource​(DataSource dataSource)
        设置数据源
        参数:
        dataSource - 数据源
      • getNamedParameterJdbcTemplate

        public org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate getNamedParameterJdbcTemplate()
      • getJdbcTemplate

        public org.springframework.jdbc.core.JdbcTemplate getJdbcTemplate()
      • getRowMapper

        public <T> org.springframework.jdbc.core.RowMapper<T> getRowMapper​(Class<T> mappedClass,
                                                                           DbBase dbBase,
                                                                           String... tableNames)
      • listResultToGetResult

        public <T> T listResultToGetResult​(List<T> list)
        多行查询结果转换为单行查询结果

        queryForObject(String, JSONObject, Class) 提供安全的查询结果获取

        queryForJson(String, JSONObject) 提供安全的查询结果获取

        参数:
        list - 多行查询结果
        返回:
        根据如下规则,返回正确的单行查询结果:
        1. size < 1 return null
        2. size = 1 return list.get(0)
        3. size > 1 throw Exception
      • queryForJson

        public com.alibaba.fastjson.JSONObject queryForJson​(String sql,
                                                            com.alibaba.fastjson.JSONObject paramJson)
        查询一行数据

        对 NamedParameterJdbcTemplate.queryForMap(String, Map) 方法的优化实现

        参数:
        sql - 要执行的SQL查询
        paramJson - 要绑定到查询的参数映射
        返回:
        可以是一个正确的单行查询结果、或null、或查询结果是多条数据而引发的预期错误异常
      • queryForObject

        public <T> T queryForObject​(String sql,
                                    com.alibaba.fastjson.JSONObject paramJson,
                                    Class<T> mappedClass)
        查询一行数据

        对 NamedParameterJdbcTemplate.queryForObject(String, Map, org.springframework.jdbc.core.RowMapper) 方法的优化实现

        参数:
        sql - 要执行的SQL查询
        paramJson - 要绑定到查询的参数映射
        mappedClass - 查询结果映射类型,支持JavaBean与简单类型(如:Long, String, Boolean)
        返回:
        可以是一个正确的单行查询结果、或null、或查询结果是多条数据而引发的预期错误异常
      • queryForRowSet

        public org.springframework.jdbc.support.rowset.SqlRowSet queryForRowSet​(String sql,
                                                                                com.alibaba.fastjson.JSONObject paramJson)
        查询多行数据

        同 NamedParameterJdbcTemplate.queryForRowSet(String, Map)

        不支持数据自动解密

        参数:
        sql - 要执行的SQL查询
        paramJson - 要绑定到查询的参数映射
        返回:
        可用于方便的获取各种数据类型的结果集
      • queryForList

        public List<com.alibaba.fastjson.JSONObject> queryForList​(String sql,
                                                                  com.alibaba.fastjson.JSONObject paramJson)
        查询多行数据

        NamedParameterJdbcTemplate.queryForList(String, Map) 方法的优化实现

        参数:
        sql - 要执行的查询SQL
        paramJson - 要绑定到查询的参数映射
        返回:
        多行查询结果
      • queryForList

        public <T> List<T> queryForList​(String sql,
                                        com.alibaba.fastjson.JSONObject paramJson,
                                        Class<T> mappedClass)
        查询多行数据

        对 NamedParameterJdbcTemplate.queryForList(String, Map, Class) 方法的优化实现

        参数:
        sql - 要执行的查询SQL
        paramJson - 要绑定到查询的参数映射
        mappedClass - 查询结果映射类型,支持JavaBean与简单类型(如:Long, String, Boolean)
        返回:
        多行查询结果
      • isDataSize

        @Deprecated
        public boolean isDataSize​(long dataSize)
        已过时。
        请自行条件判断
        是否有数据
        参数:
        dataSize - 数据大小
        返回:
        是否 > 0
      • isUpdateAndExpectedEqual

        public boolean isUpdateAndExpectedEqual​(long updateRowsNumber,
                                                int expectedValue)
        判断更新所影响的行数是否 等于 预期值

        若不是预期值,同时 updateRowsNumber > 0 那么将会抛出一个 DbException

        参数:
        updateRowsNumber - 更新所影响的行数
        expectedValue - 预期值
        返回:
        是否 等于 预期值
      • isUpdateAndExpectedGreaterThanEqual

        public boolean isUpdateAndExpectedGreaterThanEqual​(long updateRowsNumber,
                                                           int expectedValue)
        判断更新所影响的行数是否 大于等于 预期值

        若不是预期结果,同时 updateRowsNumber < expectedValue 那么将会抛出一个DbException

        参数:
        updateRowsNumber - 更新所影响的行数
        expectedValue - 预期值
        返回:
        是否 大于等于 预期值
      • updateAndExpectedEqual

        public void updateAndExpectedEqual​(long updateRowsNumber,
                                           int expectedValue)
        判断更新所影响的行数是否 等于 预期值

        若不是预期值,那么将会抛出一个DbException

        参数:
        updateRowsNumber - 更新所影响的行数
        expectedValue - 预期值
      • updateAndExpectedGreaterThanEqual

        public void updateAndExpectedGreaterThanEqual​(long updateRowsNumber,
                                                      int expectedValue)
        判断更新所影响的行数是否 大于等于 预期值

        若不是预期结果,那么将会抛出一个DbException

        参数:
        updateRowsNumber - 更新所影响的行数
        expectedValue - 预期值
      • updateBatchAndExpectedEqual

        public void updateBatchAndExpectedEqual​(int[] updateRowsNumberArray,
                                                int expectedValue)
        确认批量更新每组参数所影响的行数,是否 全部都等于 同一个预期值

        若不是预期值,那么将会抛出一个DbException

        参数:
        updateRowsNumberArray - 每组参数更新所影响的行数数组
        expectedValue - 预期值
      • paramToWhereSql

        public String paramToWhereSql​(com.alibaba.fastjson.JSONObject paramJson,
                                      String... conditions)
        绝对条件查询参数whereSql化

        已对 NULL 值进行特殊处理(IS NULL)

        已对 List 类型值进行特殊处理(IN (?, ?))

        结果示例:

         WHERE 1 = 1
        AND
        param1 = :param1
        AND
        param2 IS NULL :param2
        AND
        param3 IN :param3
        AND ...
        参数:
        paramJson - 参数
        conditions - where条件(对应paramJson key)
        返回:
        whereSql
      • paramToWhereSql

        public String paramToWhereSql​(com.alibaba.fastjson.JSONObject paramJson)
        绝对条件查询参数whereSql化

        已对 NULL 值进行特殊处理(IS NULL)

        已对 List 类型值进行特殊处理(IN (?, ?))

        结果示例:

         WHERE 1 = 1
        AND
        param1 = :param1
        AND
        param2 IS NULL :param2
        AND
        param3 IN :param3
        AND ...
        参数:
        paramJson - 参数
        返回:
        whereSql
      • getDeleteWhereSql

        public String getDeleteWhereSql()
        返回:
        逻辑删除条件SQL
      • paramValidate

        protected void paramValidate​(String tableName)
        参数验证
        参数:
        tableName - 表名
      • paramValidate

        protected void paramValidate​(String tableName,
                                     String whereSql)
        参数验证
        参数:
        tableName - 表名
        whereSql - 条件sql
      • paramValidate

        protected void paramValidate​(String tableName,
                                     Long id)
        参数验证
        参数:
        tableName - 表名
        id - 主键ID
      • paramValidate

        protected void paramValidate​(String tableName,
                                     String... columnNames)
        参数验证
        参数:
        tableName - 表名
        columnNames - 列名
      • paramValidate

        protected void paramValidate​(String tableName,
                                     Long id,
                                     String[] fieldName)
        参数验证
        参数:
        tableName - 表名
        id - 主键ID
        fieldName - 字段名称
      • paramValidate

        protected void paramValidate​(String tableName,
                                     com.alibaba.fastjson.JSONObject paramJson)
        参数验证
        参数:
        tableName - 表名
        paramJson - 参数
      • paramValidate

        protected void paramValidate​(String tableName,
                                     com.alibaba.fastjson.JSONObject[] paramJsons)
        参数验证
        参数:
        tableName - 表名
        paramJsons - 参数数组
      • paramValidate

        protected void paramValidate​(String tableName,
                                     com.alibaba.fastjson.JSONObject paramJson,
                                     String[] conditions)
        参数验证
        参数:
        tableName - 表名
        paramJson - 参数
        conditions - 条件(对应paramJson key)
      • paramFormat

        public void paramFormat​(com.alibaba.fastjson.JSONObject paramJson)
        参数美化(对SpringJDBC不支持的类型进行转换与布尔值映射识别)

        Character 转 String

        JSONObject 转 JsonString

        LocalDataTime 转 Date

        参数:
        paramJson - 需要进行类型处理的paramJson
      • extractTables

        protected String[] extractTables​(String sql)
      • aopBefore

        protected void aopBefore​(@Nullable
                                 String sql,
                                 @Nullable
                                 String tableName,
                                 @Nullable
                                 com.alibaba.fastjson.JSONObject paramJson)
        动作执行前
        参数:
        tableName - 表名
        paramJson - 参数
      • aopBefore

        protected void aopBefore​(@Nullable
                                 String sql,
                                 @Nullable
                                 String tableName,
                                 @Nullable
                                 com.alibaba.fastjson.JSONObject[] paramJsons)
        动作执行前
        参数:
        tableName - 表名
        paramJsons - 参数
      • aopAfter

        public void aopAfter​(String[] tableNames,
                             com.alibaba.fastjson.JSONObject resultJson)
        动作执行后(这是一个不适用于业务中调用的方法,请忽略)
        参数:
        tableNames - 表名
        resultJson - 单行结果数据
      • aopAfter

        protected void aopAfter​(@Nullable
                                String sql,
                                @Nullable
                                String tableName,
                                @Nullable
                                com.alibaba.fastjson.JSONObject resultJson)
        动作执行后
        参数:
        tableName - 表名
        resultJson - 单行结果数据
      • aopAfter

        protected void aopAfter​(@Nullable
                                String sql,
                                @Nullable
                                String tableName,
                                @Nullable
                                com.alibaba.fastjson.JSONObject[] resultJsons)
        动作执行后
        参数:
        tableName - 表名
        resultJsons - 多行结果数据
      • audit

        protected void audit​(String tableName,
                             com.alibaba.fastjson.JSONObject paramJson)