类 DatabaseConnector

java.lang.Object
com.walker.connector.AbstractConnector
com.walker.connector.support.DatabaseConnector
所有已实现的接口:
Connector
直接已知子类:
DamengConnector, MySqlConnector, OracleConnector, PostgresConnector, SQLServerConnector

public abstract class DatabaseConnector extends AbstractConnector
基于数据库实现的连接器对象,目前仅能查询数据集合
作者:
shikeying
  • 字段详细资料

    • OPTION_USER

      public static final String OPTION_USER
      另请参阅:
    • OPTION_PASSWORD

      public static final String OPTION_PASSWORD
      另请参阅:
    • OPTION_MAX_ACTIVE

      public static final String OPTION_MAX_ACTIVE
      另请参阅:
    • OPTION_MAX_IDLE

      public static final String OPTION_MAX_IDLE
      另请参阅:
    • OPTION_INIT_SIZE

      public static final String OPTION_INIT_SIZE
      另请参阅:
    • SQL_CREATE_DB_PREF

      public static final String SQL_CREATE_DB_PREF
      另请参阅:
    • SQL_CREATE_DB_SUF

      public static final String SQL_CREATE_DB_SUF
      另请参阅:
    • jdbcTemplate

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

      protected org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate namedJdbcTemplate
  • 构造器详细资料

    • DatabaseConnector

      public DatabaseConnector()
      没有数据源,只能通过设置参数方式初始化连接器,最后要调用
      invalid @link
      {@link this.initialize()
      }方法初始化
    • DatabaseConnector

      public DatabaseConnector(DataSource dataSource)
      如果从外部传入数据源,则通过构造函数直接创建即可。
      参数:
      dataSource -
  • 方法详细资料

    • getDataSource

      public DataSource getDataSource()
    • getJdbcTemplate

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

      public org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate getNamedJdbcTemplate()
    • isManageMode

      public boolean isManageMode()
      返回是否管理模式,管理模式中没有数据库名字,用于处理各种数据库命令
      返回:
    • setManageMode

      public void setManageMode()
      设置为管理模式:没有数据库名字
    • exeCreateDatabase

      public void exeCreateDatabase(String databaseName) throws com.walker.db.DatabaseException
      执行命令:创建数据库
      参数:
      databaseName -
      抛出:
      com.walker.db.DatabaseException
    • exeCreateTable

      public void exeCreateTable(String sql) throws com.walker.db.DatabaseException
      根据SQL语句创建表结构
      参数:
      sql -
      抛出:
      com.walker.db.DatabaseException
    • execBatchInsert

      public int execBatchInsert(String sql, org.springframework.jdbc.core.namedparam.MapSqlParameterSource[] spsArray)
      批量写入数据库记录
      参数:
      sql -
      spsArray -
    • execBatchUpdateForJdbc

      public void execBatchUpdateForJdbc(String sql, List<Object[]> batchArgs)
      使用JdbcTemplate实现批量更新。

      通过?替换变量数据即可。
      参数:
      sql -
      batchArgs -
    • execute

      public void execute(String sql) throws com.walker.db.DatabaseException
      抛出:
      com.walker.db.DatabaseException
    • queryForList

      public List<Map<String,Object>> queryForList(String sql, Object[] args)
      根据sql语句,检索列表数据,返回的一条记录是一个map对象。
      参数:
      sql -
      args -
      返回:
    • queryForRowMapper

      public <T> List<T> queryForRowMapper(String sql, Object[] args, org.springframework.jdbc.core.RowMapper<T> rowMapper)
    • queryForRowMapper

      public <T> List<T> queryForRowMapper(String sql, org.springframework.jdbc.core.RowMapper<T> rowMapper, org.springframework.jdbc.core.namedparam.SqlParameterSource paramSource)
      查询自定义rowMapper对象,该方法主要使用namedJdbcTemplate来查询。
      因为对于有些类似:where in (:ids)的查询必须使用命名参数,使用jdbcTemplate则无法查询
      参数:
      sql -
      rowMapper -
      paramSource -
      返回:
    • queryForInt

      public int queryForInt(String sql, Object[] args)
      根据sql语句查询一个整数值,如:总数等
      参数:
      sql -
      args -
      返回:
    • queryForLong

      public long queryForLong(String sql, Object[] args)
    • sqlMathQuery

      public <E> E sqlMathQuery(String sql, Object[] args, Class<E> clazz)
      给定统计公式,返回单个统计值。
      参数:
      sql -
      args -
      clazz -
      返回:
    • sqlListObjectWhereIn

      public <E> List<E> sqlListObjectWhereIn(String sql, org.springframework.jdbc.core.RowMapper<E> rowMapper, org.springframework.jdbc.core.namedparam.SqlParameterSource paramSource)
      查询自定义rowMapper对象,该方法主要使用namedJdbcTemplate来查询。
      因为对于有些类似:where in (:ids)的查询必须使用命名参数,使用jdbcTemplate则无法查询
      参数:
      sql -
      rowMapper -
      paramSource -
      返回:
    • sqlSimpleQueryPager

      public <E> com.walker.db.page.GenericPager<E> sqlSimpleQueryPager(String sql, Object[] args, org.springframework.jdbc.core.RowMapper<E> rowMapper, String pageSql)
      实现简单的分页数据返回,需要提供对象转换接口,使用了Spring框架的RowMapper.
      参数:
      sql -
      args -
      rowMapper - // * @param pageIndex 当前页(如:第五页则值为5) // * @param pageSize
      pageSql - 分页的sql语句,不同数据库不一样。如:mysql为" select * from table [limit ? offset ?]"
      返回:
    • getSqlPageArgs

      protected Object[] getSqlPageArgs(Object[] args, int firstRowIndex, int pageSize)
      组装并返回分页需要的参数数组
      参数:
      args -
      firstRowIndex -
      pageSize -
      返回:
    • batchUpdate

      public void batchUpdate(String sql)
      批量更新数据,不带任何参数
      参数:
      sql - 给定的SQL语句
    • batchUpdate

      public void batchUpdate(String sql, List<Object[]> parameters)
      批量更新数据
      参数:
      sql - 给定的SQL语句
      parameters - 参数集合,集合中每个参数都是数组,每次更新使用一个参数
    • invoke

      public Object invoke(Object... param) throws Exception
      抛出:
      Exception
    • initialize

      public void initialize()
      指定者:
      initialize 在接口中 Connector
      覆盖:
      initialize 在类中 AbstractConnector
    • acquireDruidDataSource

      protected DataSource acquireDruidDataSource(Map<String,String> params)
      创建实际可用的数据源实现,业务可以重写该方法,加载自己的数据源实现。

      目前,主要用于PG数据库多IP集群(主从)环境中,需要定义数据源。

      参数:
      params -
      返回:
    • destroy

      public void destroy()
    • getDatabaseType

      protected abstract com.walker.db.DatabaseType getDatabaseType()
      返回数据库类型,子类实现
      返回: