Class P0_JdbcTemplateOp

java.lang.Object
com.pugwoo.dbhelper.impl.part.P0_JdbcTemplateOp
All Implemented Interfaces:
DBHelper, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware
Direct Known Subclasses:
P1_QueryOp

public abstract class P0_JdbcTemplateOp extends Object implements DBHelper, org.springframework.context.ApplicationContextAware
jdbcTemplate原生操作接口封装
Author:
NICK
  • Field Details

    • LOGGER

      protected static final org.slf4j.Logger LOGGER
    • jdbcTemplate

      protected org.springframework.jdbc.core.JdbcTemplate jdbcTemplate
    • namedParameterJdbcTemplate

      protected org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate namedParameterJdbcTemplate
    • timeoutWarningValve

      protected long timeoutWarningValve
    • maxPageSize

      protected Integer maxPageSize
    • fetchSize

      protected int fetchSize
    • applicationContext

      protected org.springframework.context.ApplicationContext applicationContext
    • interceptors

      protected List<DBHelperInterceptor> interceptors
    • features

      protected Map<FeatureEnum,Boolean> features
  • Constructor Details

    • P0_JdbcTemplateOp

      public P0_JdbcTemplateOp()
  • Method Details

    • log

      protected void log(String sql, int batchSize, Object args)
      批量和非批量的log
      Parameters:
      sql - 要log的sql
      batchSize - 如果大于0,则是批量log方式
      args - 参数
    • logSlow

      protected void logSlow(long cost, String sql, int batchSize, Object args)
      记录慢sql请求
      Parameters:
      cost - 请求耗时,毫秒
      sql - 要记录的sql
      batchSize - 批量大小,如果大于0,则是批量
      args - 参数
    • rollback

      public void rollback()
      Description copied from interface: DBHelper
      手动回滚@Transactional的事务。 对于已知需要回滚的动作,我更推荐主动调用让其回滚,而非抛出RuntimeException。 当前如果没有事务而调用该方法,将抛出org.springframework.transaction.NoTransactionException。

      对于手动回滚事务rollback抛出`org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only` 异常的情况,需要使用者保证满足以下两点中的任意一点:
      1) 调用rollback的代码所在的方法是注解了@Transactional最外层的方法;
      2) 调用rollback的代码最近的@Transactional注解加上propagation = Propagation.NESTED属性。
      Specified by:
      rollback in interface DBHelper
    • executeAfterCommit

      public boolean executeAfterCommit(Runnable runnable)
      Description copied from interface: DBHelper
      当前事务提交后执行
      Specified by:
      executeAfterCommit in interface DBHelper
      Parameters:
      runnable - 执行的回调方法
      Returns:
      当提交成功返回true,提交失败返回false;如果当前不在事务中时,返回false
    • jdbcExecuteUpdate

      protected int jdbcExecuteUpdate(String sql, Object... args)
      使用jdbcTemplate模版执行update,不支持in (?)表达式
      Returns:
      实际修改的行数
    • namedJdbcExecuteUpdate

      protected int namedJdbcExecuteUpdate(String sql, Object... args)
      使用namedParameterJdbcTemplate模版执行update,支持in(?)表达式
    • namedJdbcExecuteUpdateWithLog

      protected int namedJdbcExecuteUpdateWithLog(String sql, String logSql, int batchSize, List<Object> logArgs, Object... args)
      使用namedParameterJdbcTemplate模版执行update,支持in(?)表达式
      Parameters:
      logSql - 用于日志打印的sql
      batchSize - 批量修改的大小,当它的值大于0时,切换为batchLog方式log
      logArgs - 用于日志打印的参数
    • setJdbcTemplate

      public void setJdbcTemplate(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate)
    • setNamedParameterJdbcTemplate

      public void setNamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate namedParameterJdbcTemplate)
      可选,非必须 since 1.2
    • getJdbcTemplate

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

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

      public void setTimeoutWarningValve(long timeMS)
      Description copied from interface: DBHelper
      设置SQL执行超时的WARN log,超时时间默认为1秒
      Specified by:
      setTimeoutWarningValve in interface DBHelper
      Parameters:
      timeMS - 毫秒
    • setMaxPageSize

      public void setMaxPageSize(int maxPageSize)
      Description copied from interface: DBHelper
      设置允许的每页最大的个数,当页数超过允许的最大页数时,设置为最大页数。 默认对每页最大个数没有限制,该限制只对getPage和getPageWithoutCount接口生效。
      Specified by:
      setMaxPageSize in interface DBHelper
      Parameters:
      maxPageSize - 允许的每页最大的个数
    • setFetchSize

      public void setFetchSize(int fetchSize)
      Description copied from interface: DBHelper
      对于流式Stream获取数据的,可以指定fetchSize大小,默认1000
      Specified by:
      setFetchSize in interface DBHelper
      Parameters:
      fetchSize - fetchSize
    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • setInterceptors

      public void setInterceptors(List<DBHelperInterceptor> interceptors)
      Description copied from interface: DBHelper
      数据库拦截器
      Specified by:
      setInterceptors in interface DBHelper
      Parameters:
      interceptors - 拦截器列表,全量更新
    • setTimeoutWarningCallback

      public void setTimeoutWarningCallback(IDBHelperSlowSqlCallback callback)
      Description copied from interface: DBHelper
      设置SQL执行超时回调,可用于自行实现将慢sql存放到db
      Specified by:
      setTimeoutWarningCallback in interface DBHelper
      Parameters:
      callback - 执行的回调方法
    • turnOnFeature

      public void turnOnFeature(FeatureEnum featureEnum)
      Description copied from interface: DBHelper
      开启某个特性
      Specified by:
      turnOnFeature in interface DBHelper
      Parameters:
      featureEnum - 特性枚举,默认特性是否开启详见特性文档说明
    • turnOffFeature

      public void turnOffFeature(FeatureEnum featureEnum)
      Description copied from interface: DBHelper
      关闭某个特性
      Specified by:
      turnOffFeature in interface DBHelper
      Parameters:
      featureEnum - 特性枚举,默认特性是否开启详见特性文档说明
    • getFeature

      public boolean getFeature(FeatureEnum featureEnum)
    • addComment

      protected String addComment(String sql)
      给sql加上注释,返回加完注释之后的sql
      Parameters:
      sql - not null
    • getDatabaseType

      public DatabaseTypeEnum getDatabaseType()
      Description copied from interface: DBHelper
      查询dbHelper对应的数据库类型
      Specified by:
      getDatabaseType in interface DBHelper