码迷,mamicode.com
首页 > 数据库 > 详细

mysql+mybatis+事务+流水号

时间:2016-08-25 23:47:43      阅读:723      评论:0      收藏:0      [点我收藏+]

标签:

数据库存储过程

drop PROCEDURE generate_serial_number;

CREATE PROCEDURE generate_serial_number(
	IN param_key varchar(20),
	IN param_orgId int, 
	OUT result int)  
	begin 
		START TRANSACTION; 
		select number+1 into result from sys_serial_number 
			where table_key=param_key and orgId = param_orgId for update;
		update sys_serial_number set number = number+1 
			where table_key=param_key and orgId = param_orgId;
	commit;
end;  

  

mybatis配置

  <select id="generateSerialNumber" statementType="CALLABLE" parameterType="java.util.HashMap">
    <![CDATA[
           {
           call generate_serial_number (
            #{param_key,mode=IN,jdbcType=VARCHAR},
            #{param_orgId,mode=IN,jdbcType=BIGINT},
            #{result,mode=OUT,jdbcType=VARCHAR}
            )
           }
       ]]>
  </select>

 

 

测试代码

@Override
    public Map<String, Object> generateSerialNumber(Map<String, Object> param) {
        sysSerialNumberMapper.generateSerialNumber(param);
        return param;
    }

 

final Map<String, Object> param = new HashMap<String, Object>();
        param.put("param_key","contract");
        param.put("param_orgId", 84);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i =0; i<100; i++) {
                    Map<String, Object> map = serialNumberProvider.generateSerialNumber(param);
                    System.out.println("thread-1:" + map.get("result"));
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i =0; i<100; i++) {
                    Map<String, Object> map = serialNumberProvider.generateSerialNumber(param);
                    System.out.println("thread-2:" + map.get("result"));
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i =0; i<100; i++) {
                    Map<String, Object> map = serialNumberProvider.generateSerialNumber(param);
                    System.out.println("thread-3:" + map.get("result"));
                }
            }
        }).start();

        byte[] b = new byte[0];
        synchronized(b) {
            b.wait();
        }

 

mysql+mybatis+事务+流水号

标签:

原文地址:http://www.cnblogs.com/cocoat/p/5808684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!