update确保数据唯一性。
一、问题场景
如果spring mvc进行update操作时修改的值超过范围,直接报错违反唯一约束条件,程序报错。
希望不报错影响程序完整性,如果违反唯一约束条件,直接返回update失败。
二、解决办法
采取catch的方法捕获update错误,然后输出update失败就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
@Override public int updateByPrimaryKeySelective(shop record) { try { return shopmapper.updateByPrimaryKeySelective(record); } catch (Exception e) { return 0; } } @Override public int updateByPrimaryKeyWithBLOBs(shop record) { try { return shopmapper.updateByPrimaryKeyWithBLOBs(record); } catch (Exception e) { return 0; } } @Override public int updateByPrimaryKey(shop record) { try { return shopmapper.updateByPrimaryKey(record); } catch (Exception e) { return 0; } } |
三、总结
养成好习惯,抛出错误。