diff --git a/pom.xml b/pom.xml index 84cc9a400553483e5df6b27001a5d624a87346c2..c1f3652ecccc57a158ad7d4b73374d1c2c43e3b8 100644 --- a/pom.xml +++ b/pom.xml @@ -25,11 +25,13 @@ log4j 1.2.17 + org.slf4j slf4j-api 1.7.5 + org.slf4j slf4j-log4j12 diff --git a/src/main/java/com/yingjun/ssm/enums/ResultEnum.java b/src/main/java/com/yingjun/ssm/enums/ResultEnum.java index 43e519d39a27fc9aa40938c335e8a3b6c42f447e..4c31dc9e571ae62bff1d94680ebf2b242333dbc6 100644 --- a/src/main/java/com/yingjun/ssm/enums/ResultEnum.java +++ b/src/main/java/com/yingjun/ssm/enums/ResultEnum.java @@ -1,11 +1,26 @@ package com.yingjun.ssm.enums; +/** + * 业务异常基类,所有业务异常都必须继承于此异常 定义异常时,需要先确定异常所属模块。 例如:无效用户可以定义为 [10010001] + * 前四位数为系统模块编号,后4位为错误代码 ,唯一。 + * + * @author yingjun + * + */ public enum ResultEnum { - SUCCESS(1, "成功"), - INVALID_USER(-1, "无效用户"), - PARAM_USER(-2, "参数错误"), - INNER_ERROR(-3, "系统异常"); + // 数据库想操作异常 + DB_INSERT_RESULT_ERROR(99990001, "db insert error"), + DB_UPDATE_RESULT_ERROR(99990002, "db update error"), + DB_SELECTONE_IS_NULL(99990003,"db select return null"), + + // 系统异常 + INNER_ERROR(99980001, "系统错误"), + TOKEN_IS_ILLICIT(99980002, "Token验证非法"), + SESSION_IS_OUT_TIME(99980003, "会话超时"), + + // 用户相关异常 + INVALID_USER(1001001, "无效用户"); private int state; diff --git a/src/main/java/com/yingjun/ssm/exception/MyException.java b/src/main/java/com/yingjun/ssm/exception/MyException.java deleted file mode 100644 index 181c53d8cd0a48f340e3c9977247506e132a96f6..0000000000000000000000000000000000000000 --- a/src/main/java/com/yingjun/ssm/exception/MyException.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.yingjun.ssm.exception; -/** - * - * @author yingjun - * - */ -public class MyException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - public MyException(String message) { - super(message); - } - - public MyException(String message, Throwable cause) { - super(message, cause); - } - - - -} diff --git a/src/main/java/com/yingjun/ssm/service/impl/GoodsServiceImpl.java b/src/main/java/com/yingjun/ssm/service/impl/GoodsServiceImpl.java index ec00060a8b9d79d483da41cf7aef93ea5040e526..651582ec7801bf55591025b848cc96117593d31c 100644 --- a/src/main/java/com/yingjun/ssm/service/impl/GoodsServiceImpl.java +++ b/src/main/java/com/yingjun/ssm/service/impl/GoodsServiceImpl.java @@ -18,7 +18,7 @@ import com.yingjun.ssm.dao.UserDao; import com.yingjun.ssm.entity.Goods; import com.yingjun.ssm.entity.User; import com.yingjun.ssm.enums.ResultEnum; -import com.yingjun.ssm.exception.MyException; +import com.yingjun.ssm.exception.BizException; import com.yingjun.ssm.service.GoodsService; @Service @@ -36,42 +36,42 @@ public class GoodsServiceImpl implements GoodsService { @Override public List getGoodsList(int offset, int limit) { - String cache_key=RedisCache.CAHCENAME+"|getGoodsList|"+offset+"|"+limit; - List result_cache=cache.getListCache(cache_key, Goods.class); - if(result_cache==null){ - //缓存中没有再去数据库取,并插入缓存(缓存时间为60秒) - result_cache=goodsDao.queryAll(offset, limit); + String cache_key = RedisCache.CAHCENAME + "|getGoodsList|" + offset + "|" + limit; + List result_cache = cache.getListCache(cache_key, Goods.class); + if (result_cache == null) { + // 缓存中没有再去数据库取,并插入缓存(缓存时间为60秒) + result_cache = goodsDao.queryAll(offset, limit); cache.putListCacheWithExpireTime(cache_key, result_cache, RedisCache.CAHCETIME); - LOG.info("put cache with key:"+cache_key); + LOG.info("put cache with key:" + cache_key); return result_cache; - }else{ - LOG.info("get cache with key:"+cache_key); + } else { + LOG.info("get cache with key:" + cache_key); } return result_cache; } @Transactional @Override - public void buyGoods(long userPhone, long goodsId, boolean useProcedure){ + public void buyGoods(long userPhone, long goodsId, boolean useProcedure) { // 用户校验 User user = userDao.queryByPhone(userPhone); if (user == null) { - throw new MyException(ResultEnum.INVALID_USER.getMsg()); + throw new BizException(ResultEnum.INVALID_USER.getMsg()); } if (useProcedure) { - //通过存储方式的方法进行操作 + // 通过存储方式的方法进行操作 Map map = new HashMap(); map.put("userId", user.getUserId()); map.put("goodsId", goodsId); map.put("title", "抢购"); map.put("result", null); goodsDao.bugWithProcedure(map); - int result = MapUtils.getInteger(map, "result", ResultEnum.INNER_ERROR.getState()); + int result = MapUtils.getInteger(map, "result", -1); if (result <= 0) { // 买卖失败 - throw new MyException(ResultEnum.INNER_ERROR.getMsg()); + throw new BizException(ResultEnum.INNER_ERROR.getMsg()); } else { - // 买卖成功 + // 买卖成功 // 此时缓存中的数据不是最新的,需要对缓存进行清理(具体的缓存策略还是要根据具体需求制定) cache.deleteCacheWithPattern("getGoodsList*"); LOG.info("delete cache with key: getGoodsList*"); @@ -82,13 +82,13 @@ public class GoodsServiceImpl implements GoodsService { int inserCount = orderDao.insertOrder(user.getUserId(), goodsId, "普通买卖"); if (inserCount <= 0) { // 买卖失败 - throw new MyException(ResultEnum.INNER_ERROR.getMsg()); + throw new BizException(ResultEnum.DB_UPDATE_RESULT_ERROR.getMsg()); } else { // 减库存 int updateCount = goodsDao.reduceNumber(goodsId); if (updateCount <= 0) { // 减库存失败 - throw new MyException(ResultEnum.INNER_ERROR.getMsg()); + throw new BizException(ResultEnum.DB_UPDATE_RESULT_ERROR.getMsg()); } else { // 买卖成功 // 此时缓存中的数据不再是最新的,需要对缓存进行清理(具体的缓存策略还是要根据具体需求制定) diff --git a/src/main/java/com/yingjun/ssm/web/GoodsController.java b/src/main/java/com/yingjun/ssm/web/GoodsController.java index 7ce4342e213d515ff66b408eb5c250bfedfc010b..dd4af10ccaa9bee683a909c8fbde801d454a91ce 100644 --- a/src/main/java/com/yingjun/ssm/web/GoodsController.java +++ b/src/main/java/com/yingjun/ssm/web/GoodsController.java @@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.ResponseBody; import com.yingjun.ssm.dto.BaseResult; import com.yingjun.ssm.entity.Goods; import com.yingjun.ssm.enums.ResultEnum; -import com.yingjun.ssm.exception.MyException; import com.yingjun.ssm.service.GoodsService; @Controller diff --git a/src/main/resources/mapper/UserDao.xml b/src/main/resources/mapper/UserDao.xml index 679422fb0e9a276bee63d68ab70acb9f9fca9e3d..fdde4937eb549ba6556eae68c1677f30cb553ef4 100644 --- a/src/main/resources/mapper/UserDao.xml +++ b/src/main/resources/mapper/UserDao.xml @@ -16,17 +16,7 @@ limit #{offset},#{limit} - - - - + UPDATE _user SET diff --git a/src/main/resources/redis.properties b/src/main/resources/redis.properties index 9b72e701a20ac469e8bcdb8093ae45f569091d23..7749e376d9460e6c998b630a55cee8a9237a7080 100644 --- a/src/main/resources/redis.properties +++ b/src/main/resources/redis.properties @@ -1,15 +1,15 @@ #redis config -redis.pass=hundsun@1 +redis.pass=yingjun redis.pool.maxTotal=105 redis.pool.maxIdle=10 redis.pool.maxWaitMillis=5000 redis.pool.testOnBorrow=true -#redis\u5355\u8282\u70b9\u914d\u7f6e +#redis\u5355\u8282\u70B9\u914D\u7F6E redis.ip=120.27.141.45 redis.port=6379 -#redis\u4e3b\u4ece\u9ad8\u53ef\u7528\u914d\u7f6e +#redis\u4E3B\u4ECE\u9AD8\u53EF\u7528\u914D\u7F6E #sentinel1.ip=192.168.43.225 #sentinel1.port=63791 #sentinel2.ip=192.168.43.225 diff --git a/src/main/resources/sql/schema.sql b/src/main/resources/sql/schema.sql index d508e0d4eb3879f5b176aac88fd6ca317ee67b16..adffc3853d4089a94a15071dd13af374e2444bd8 100644 --- a/src/main/resources/sql/schema.sql +++ b/src/main/resources/sql/schema.sql @@ -1,5 +1,5 @@ ---需要 MySQL 5.6.5以上的版本 +-- 需要 MySQL 5.6.5以上的版本 CREATE DATABASE beauty_ssm; USE beauty_ssm; @@ -39,7 +39,7 @@ KEY `idx_goods_id`(`goods_id`) )ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT='订单表'; ---插入初始数据 +-- 插入初始数据 INSERT INTO _user(user_name, user_phone, score) VALUES @@ -47,7 +47,6 @@ VALUES ('小明', 18968129999, 0); - INSERT INTO _goods(title, state, price,number) VALUES