一个生成唯一序号的服务,虽然技术不先进,但是很好用

package com.highcom.seqgen.dao.jdbc;

import java.sql.*;
import javax.sql.*;

import org.apache.commons.logging.*;
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.object.*;
import com.highcom.seqgen.dao.*;
import com.highcom.seqgen.domain.*;


public class SequenceDaoJdbcImpl
    implements SequenceDao, InitializingBean {

  private DataSource dataSource;
  private static Log logger = LogFactory.getLog(SequenceDaoJdbcImpl.class);

  //
  SequenceQuery sequenceQuery;
  SequenceUpdate sequenceUpdate;
  SequenceInsert sequenceInsert;
  //
  protected static final String INSERT_SQL =
      "insert into sequence(seq_name,seq_value) values( ? , ? )";
  protected static final String UPDATE_SQL =
      "update sequence set seq_value = ? where seq_name = ?";
  protected static final String SELECT_SQL =
      "select seq_name,seq_value from sequence where seq_name =?";

  public SequenceDaoJdbcImpl() {

  }

  public int getSequence(String seq_name) {
    int result;
    Object obj = sequenceQuery.findObject(new Object[] {seq_name});
    if (obj == null) {
      sequenceInsert.insert(seq_name, 0);
    }

    Sequence seq = (Sequence) sequenceQuery.findObject(new Object[] {seq_name});
    sequenceUpdate.update(seq.getName(), seq.getValue() + 1);
    result = seq.getValue() + 1;
    return result;
  }

  public void afterPropertiesSet() throws Exception {
    if (dataSource == null) {
      logger.error("Must set dataSource bean property on " + getClass());
      throw new ApplicationContextException(
          "Must set dataSource bean property on " + getClass());
    }
    //
    sequenceQuery = new SequenceQuery(this.dataSource);
    sequenceUpdate = new SequenceUpdate(this.dataSource);
    sequenceInsert = new SequenceInsert(this.dataSource);
  }

  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  ///jdbc内部类
  class SequenceInsert
      extends SqlUpdate {
    public SequenceInsert(DataSource dataSource) {
      super(dataSource, INSERT_SQL);
      declareParameter(new SqlParameter(Types.VARCHAR));
      declareParameter(new SqlParameter(Types.INTEGER));
      compile();
    }

    public void insert(String seqName, int segValue) {
      Object[] objs = new Object[] {
          seqName, new Integer(segValue)};
      super.update(objs);
    }
  }

  class SequenceUpdate
      extends SqlUpdate {
    public SequenceUpdate(DataSource dataSource) {
      super(dataSource, UPDATE_SQL);
      declareParameter(new SqlParameter(Types.INTEGER));
      declareParameter(new SqlParameter(Types.VARCHAR));

      compile();

    }

    public void update(String seqName, int segValue) {
      Object[] objs = new Object[] {
          new Integer(segValue), seqName};
      super.update(objs);
    }
  }

  class SequenceQuery
      extends MappingSqlQuery {
    public SequenceQuery(DataSource dataSource) {
      super(dataSource, SELECT_SQL);
      declareParameter(new SqlParameter(Types.VARCHAR));
      compile();
    }

    protected Object mapRow(ResultSet resultSet, int _int) throws SQLException {
      Sequence seq = new Sequence();
      seq.setName(resultSet.getString("seq_name"));
      seq.setValue(resultSet.getInt("seq_value"));
      return seq;
    }
  }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值