聲明
1)該文章整理自網上的大牛和專家無私奉獻的資料,具體引用的資料請看參考文獻。
2)本文僅供學術交流,非商用。如果某部分不小心侵犯了大家的利益,還望海涵,並聯系博主刪除。
3)博主才疏學淺,文中如有不當之處,請各位指出,共同進步,謝謝。
4)此屬於第一版本,若有錯誤,還需繼續修正與增刪。還望大家多多指點。大家都共享一點點,一起為祖國科研的推進添磚加瓦。
1 //Mapper層 2 int selectCountByCode(String code) 3 4 //mabatis層 5 <select id="selectCountByCode" parameterType="java.lang.String" 6 resultType="java.lang.Integer"> 7 select count(*) 8 from category 9 where code = #{code,jdbcType=VARCHAR} 10 </select> 11 12 //定義報錯 13 // []已經存在 14 public static final Integer ERROR_CODE_ALREADY_EXIST = 4001001; 15 public static final String ERROR_MSG_ALREADY_EXIST = "[%s]=%s 已經存在"; 16 17 //ServiceImpl 18 // 先驗證 code 的唯一性(盡管數據庫已經添加 unique 限制) 19 int countCode = hpCommodityCategoryMapper.selectCountByCode(code); 20 if (countCode > 0) { 21 // code 重復 22 LOGGER.error(String.format(ErrorCode.ERROR_MSG_ALREADY_EXIST, "code", code)); 23 responseBody.setCode(ErrorCode.ERROR_CODE_ALREADY_EXIST); 24 responseBody.setMessage(String.format(ErrorCode.ERROR_MSG_ALREADY_EXIST, "code", code)); 25 return responseBody; 26 }