MyBatis+Mysql生成序列号

一、创建序列表

CREATE TABLE `t_bas_sequence` (
  `seq_name` varchar(64) NOT NULL DEFAULT '' COMMENT '序列名',
  `current_val` bigint(20) DEFAULT '0' COMMENT '序列值',
  `increment_val` int(11) DEFAULT '1' COMMENT '步增值',
  PRIMARY KEY (`seq_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

二、创建存储过程(函数)

CREATE DEFINER=`root`@`localhost` FUNCTION `nextval`(`v_seq_name` varchar(64),`count` int) RETURNS bigint(20)
BEGIN
  DECLARE val BIGINT;
  DECLARE increment int;
  set val = currval(v_seq_name),increment = 0;
  SELECT increment_val into increment from t_bas_sequence
  where seq_name = v_seq_name;
  UPDATE t_bas_sequence
  set current_val = current_val + increment_val*count
  where seq_name = v_seq_name;
	RETURN val+increment;
END

CREATE DEFINER=`root`@`localhost` FUNCTION `nextval`(`v_seq_name` varchar(64),`count` int) RETURNS bigint(20)
BEGIN
  DECLARE val BIGINT;
  DECLARE increment int;
  set val = currval(v_seq_name),increment = 0;
  SELECT increment_val into increment from t_bas_sequence
  where seq_name = v_seq_name;
  UPDATE t_bas_sequence
  set current_val = current_val + increment_val*count
  where seq_name = v_seq_name;
	RETURN val+increment;
END

三、Java实体代码

/**
 * 模块名称:系统序列实体类
 * 模块作者:WANGTAO
 * 开发时间:2016年10月9日 下午6:08:28
 * 模块路径:com.paireach.tts.module.basedata.common.dao.SequenceEntity
 * 更新记录:
 */
public class SequenceEntity {
	
	private String seqName ; // 序列名
	
	private String currentVal; // 序列值
	
	private String incrementVal; // 步增值

	public String getSeqName() {
		return seqName;
	}

	public void setSeqName(String seqName) {
		this.seqName = seqName;
	}

	public String getCurrentVal() {
		return currentVal;
	}

	public void setCurrentVal(String currentVal) {
		this.currentVal = currentVal;
	}

	public String getIncrementVal() {
		return incrementVal;
	}

	public void setIncrementVal(String incrementVal) {
		this.incrementVal = incrementVal;
	}
	
}

四、JavaDAO代码

import java.util.List;

import com.didong.server.dubbo.api.bean.common.SequenceEntity;


/**
 * 模块名称:ServiceBaseDao
 * 功能列表:
 * 模块作者:WANGTAO
 * 开发时间:2016年7月15日 上午11:31:35
 * 模块路径:com.paireach.tts.module.basedata.common.dao.ServiceBaseDao
 * 更新记录:
 */
public interface IServiceBaseDao {	
	 /**
	  * 功能描述:判断序列是否存在
	  * 开发时间:2016年7月15日 上午11:33:59
	  * 更新记录:
	  * 返回数据:boolean
	  */
     public  boolean isExistSequence(String seqName);
     
     /**
      * 功能描述:如果count=1,那么返回序列的下一个步长的值
      * 开发时间:2016年7月15日 上午11:34:18
      * 更新记录:
      * 返回数据:long
      */
     public  long getNextValue(String seqName,int count);
     
     /**
      * 功能描述:根据序列名称及步长值,新增序列
      * 开发时间:2016年7月15日 上午11:34:34
      * 更新记录:
      * 返回数据:boolean
      */
     public boolean addNewSequence(String seqName,int step);
 	
 	/**
 	 * 功能描述:查询序列
 	 * 开发时间:2016年10月10日 上午9:56:42
 	 * 更新记录:
 	 * 返回数据:List
 	 */
 	List query(SequenceEntity sequence, int start, int limit);
 	
 	/**
 	 * 功能描述:查询序列记录数
 	 * 开发时间:2016年10月10日 上午9:58:16
 	 * 更新记录:
 	 * 返回数据:Long
 	 */
 	Long count(SequenceEntity sequence);
 	
 	/**
	 * 功能描述:根据序列名查询序列实体
	 * 开发时间:2016年10月10日 下午3:35:41
	 * 更新记录:
	 * 返回数据:SequenceEntity
	 */
 	SequenceEntity queryEntityBySeqName(String seqName);
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.didong.manager.frame.core.stringRedis.RedisDataStringTemplate;
import com.didong.server.dubbo.api.bean.common.SequenceEntity;
import com.paireach.tts.module.basedata.common.dao.IServiceBaseDao;

@Repository("serviceBaseDao")
public class ServiceBaseDaoImpl extends SqlSessionDaoSupport implements IServiceBaseDao 
{
private final String REDIS_SEQ = "t_bas_sequence".toUpperCase();
	
	@Resource
	RedisDataStringTemplate redisDataCacheTemplate;
	
	
	@Override
	public boolean isExistSequence(String seqName)
	{
        int count = (int) this.getSqlSession().selectOne("service.selectSequenceByName", seqName);
		return count > 0;
	}

	@Override
	public long getNextValue(String seqName, int count)
	{
		Map map = new HashMap();
		map.put("seqName", seqName);
		map.put("idCount", count);		
		return (long) this.getSqlSession().selectOne("service.selectNextIdBySeqName", map);
	}

	@Override
	public boolean addNewSequence(String seqName, int step)
	{
		Map map = new HashMap();
		map.put("seqName", seqName);
		map.put("step", step);
        int rs = this.getSqlSession().insert("service.addNewSequence", map);
		return rs > 0;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List query(SequenceEntity sequence, int start,
			int limit) {
		if(0 < limit) {
			RowBounds rowBounds = new RowBounds(start, limit);
			return (List) getSqlSession().selectList("service.query", sequence, rowBounds);
		} else {
			return (List) getSqlSession().selectList("service.query");
		}
	}

	@Override
	public Long count(SequenceEntity sequence) {
		return (Long) getSqlSession().selectOne("service.count", sequence);
	}

	@Override
	public SequenceEntity queryEntityBySeqName(String seqName) {
		return (SequenceEntity) getSqlSession().selectOne("service.queryEntityBySeqName", seqName);
	}
}

五、最后贴上MyBatis文件




	
	
		
        
        
	
	
	
        MASTER.seq_name AS seq_name,
        MASTER.current_val AS current_val,
        MASTER.increment_val AS increment_val
	
	
	
		
             AND MASTER.seqName = #{seqName}
             AND MASTER.currentVal = #{currentVal}
             AND MASTER.incrementVal = #{incrementVal}
		
	
					   
	
	
	
	
	
	
	
		update t_bas_sequence 
		 
			current_val = #{currentVal,jdbcType=VARCHAR},
			increment_val = #{incrementVal,jdbcType=VARCHAR},
		
		where seq_name = #{seqName}
	
	
    
    
    
    
    
		                   
    
    
    
    
	


你可能感兴趣的:(Java,JavaWeb,Mysql)