Commit 42246fae by s_guodong

dbcounter查一条返回多条的兼容处理

parent 007035bd
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
</select> </select>
<update id="updateCounter" > <update id="updateCounter" >
update TBLCNT set VAL = VAL + STP where TYP = #{typ} update TBLCNT set VAL = #{val} + STP where TYP = #{typ}
</update> </update>
<select id="seqNextval" resultType="java.lang.Integer" > <select id="seqNextval" resultType="java.lang.Integer" >
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
</select> </select>
<update id="updateCounter" > <update id="updateCounter" >
update CNT set VAL = VAL + STP where NAM = #{nam} update CNT set VAL = #{val} + STP where NAM = #{nam}
</update> </update>
<select id="seqNextval" resultType="java.lang.Integer" > <select id="seqNextval" resultType="java.lang.Integer" >
......
...@@ -38,14 +38,14 @@ public class CounterImplWithSpringTransaction implements CounterService { ...@@ -38,14 +38,14 @@ public class CounterImplWithSpringTransaction implements CounterService {
cnt = 0; cnt = 0;
counterMapper.insertNewCounter(seqname, 1); //插入新数 counterMapper.insertNewCounter(seqname, 1); //插入新数
} else } else
counterMapper.updateCounter(seqname); //计算器增加 counterMapper.updateCounter(seqname,cnt); //计算器增加
} catch (Exception e) { } catch (Exception e) {
throw (new RuleExecuteException("主键生成异常", e)); throw (new RuleExecuteException("主键生成异常", e));
} finally { } finally {
lock.unlock(); lock.unlock();
} }
} else } else
counterMapper.updateCounter(seqname); //计算器增加 counterMapper.updateCounter(seqname,cnt); //计算器增加
return cnt; return cnt;
} }
...@@ -62,14 +62,14 @@ public class CounterImplWithSpringTransaction implements CounterService { ...@@ -62,14 +62,14 @@ public class CounterImplWithSpringTransaction implements CounterService {
cnt = 0; cnt = 0;
tblCounterMapper.insertNewCounter(seqname, 1); //插入新数 tblCounterMapper.insertNewCounter(seqname, 1); //插入新数
} else } else
tblCounterMapper.updateCounter(seqname); //计算器增加 tblCounterMapper.updateCounter(seqname,cnt); //计算器增加
} catch (Exception e) { } catch (Exception e) {
throw (new RuleExecuteException("主键生成异常", e)); throw (new RuleExecuteException("主键生成异常", e));
} finally { } finally {
lock.unlock(); lock.unlock();
} }
} else } else
tblCounterMapper.updateCounter(seqname); //计算器增加 tblCounterMapper.updateCounter(seqname,cnt); //计算器增加
return cnt; return cnt;
} }
} }
package com.brilliance.mda.support.mybatis.count.mapper; package com.brilliance.mda.support.mybatis.count.mapper;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component @Component
public class CounterMapper{ public class CounterMapper {
final String FNAME = this.getClass().getName(); final String FNAME = this.getClass().getName();
...@@ -19,29 +20,36 @@ public class CounterMapper{ ...@@ -19,29 +20,36 @@ public class CounterMapper{
public SqlSessionTemplate template; public SqlSessionTemplate template;
public Integer getCountValWithUpdate(@Param("nam") String seqName){ public Integer getCountValWithUpdate(@Param("nam") String seqName) {
return template.selectOne(FNAME+".getCountValWithUpdate",seqName); List<Integer> lst = template.selectList(FNAME + ".getCountValWithUpdate", seqName);
if (lst.isEmpty()) {
return null;
}
return Collections.max(lst);
} }
public int insertNewCounter(@Param("nam") String nam, @Param("stp") int stp){ public int insertNewCounter(@Param("nam") String nam, @Param("stp") int stp) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("nam", nam); map.put("nam", nam);
map.put("start",1); map.put("start", 1);
map.put("stp", stp); map.put("stp", stp);
return template.insert(FNAME+".insertNewCounter",map); return template.insert(FNAME + ".insertNewCounter", map);
} }
public void updateCounter(@Param("nam") String seqName){ public void updateCounter(@Param("nam") String seqName, @Param("val") int val) {
template.selectOne(FNAME+".updateCounter",seqName); Map<String, Object> map = new HashMap<>();
map.put("nam", seqName);
map.put("val", val);
template.selectList(FNAME + ".updateCounter", map);
} }
public int seqNextval(String seqName) { public int seqNextval(String seqName) {
return template.selectOne(FNAME+".seqNextval",seqName); return template.selectOne(FNAME + ".seqNextval", seqName);
} }
public int dbCounter(String seqName){ public int dbCounter(String seqName) {
return template.selectOne(FNAME+".dbCounter",seqName); return template.selectOne(FNAME + ".dbCounter", seqName);
} }
} }
\ No newline at end of file
...@@ -5,7 +5,9 @@ import org.mybatis.spring.SqlSessionTemplate; ...@@ -5,7 +5,9 @@ import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -18,29 +20,35 @@ public class TblCounterMapper { ...@@ -18,29 +20,35 @@ public class TblCounterMapper {
public SqlSessionTemplate template; public SqlSessionTemplate template;
public Integer getCountValWithUpdate(@Param("typ") String seqName){ public Integer getCountValWithUpdate(@Param("typ") String seqName) {
return template.selectOne(FNAME+".getCountValWithUpdate",seqName); List<Integer> lst = template.selectList(FNAME + ".getCountValWithUpdate", seqName);
if (lst.isEmpty()) {
return null;
}
return Collections.max(lst);
} }
public int insertNewCounter(@Param("typ") String typ, @Param("stp") int stp){ public int insertNewCounter(@Param("typ") String typ, @Param("stp") int stp) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("typ", typ); map.put("typ", typ);
map.put("start",1); map.put("start", 1);
map.put("stp", stp); map.put("stp", stp);
return template.insert(FNAME+".insertNewCounter",map); return template.insert(FNAME + ".insertNewCounter", map);
} }
public void updateCounter(@Param("typ") String seqName, @Param("val") int val) {
public void updateCounter(@Param("typ") String seqName){ Map<String, Object> map = new HashMap<>();
template.selectOne(FNAME+".updateCounter",seqName); map.put("typ", seqName);
map.put("val", val);
template.selectList(FNAME + ".updateCounter", map);
} }
public int seqNextval(String seqName) { public int seqNextval(String seqName) {
return template.selectOne(FNAME+".seqNextval",seqName); return template.selectOne(FNAME + ".seqNextval", seqName);
} }
public int dbCounter(String seqName){ public int dbCounter(String seqName) {
return template.selectOne(FNAME+".dbCounter",seqName); return template.selectOne(FNAME + ".dbCounter", seqName);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment