提交 412845c9 编写于 作者: Y yingjun

配置文件完善

上级 6763a541
......@@ -25,11 +25,13 @@
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
......
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;
......
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);
}
}
......@@ -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<Goods> getGoodsList(int offset, int limit) {
String cache_key=RedisCache.CAHCENAME+"|getGoodsList|"+offset+"|"+limit;
List<Goods> 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<Goods> 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<String, Object> map = new HashMap<String, Object>();
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 {
// 买卖成功
// 此时缓存中的数据不再是最新的,需要对缓存进行清理(具体的缓存策略还是要根据具体需求制定)
......
......@@ -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
......
......@@ -16,17 +16,7 @@
limit #{offset},#{limit}
</select>
<!--mybatis调用存储过程-->
<select id="bugWithProcedure" statementType="CALLABLE">
call execute_buy(
#{userId,jdbcType=BIGINT,mode=IN},
#{goodsId,jdbcType=BIGINT,mode=IN},
#{title,jdbcType=TIMESTAMP,mode=IN},
#{result,jdbcType=INTEGER,mode=OUT}
)
</select>
<update id="addScore">
UPDATE _user
SET
......
#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
......
--需要 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册