Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
beauty_ssm
提交
412845c9
B
beauty_ssm
项目概览
爱吃血肠
/
beauty_ssm
通知
9
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beauty_ssm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
412845c9
编写于
7月 20, 2016
作者:
Y
yingjun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
配置文件完善
上级
6763a541
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
44 addition
and
60 deletion
+44
-60
pom.xml
pom.xml
+2
-0
src/main/java/com/yingjun/ssm/enums/ResultEnum.java
src/main/java/com/yingjun/ssm/enums/ResultEnum.java
+19
-4
src/main/java/com/yingjun/ssm/exception/MyException.java
src/main/java/com/yingjun/ssm/exception/MyException.java
+0
-21
src/main/java/com/yingjun/ssm/service/impl/GoodsServiceImpl.java
...n/java/com/yingjun/ssm/service/impl/GoodsServiceImpl.java
+17
-17
src/main/java/com/yingjun/ssm/web/GoodsController.java
src/main/java/com/yingjun/ssm/web/GoodsController.java
+0
-1
src/main/resources/mapper/UserDao.xml
src/main/resources/mapper/UserDao.xml
+1
-11
src/main/resources/redis.properties
src/main/resources/redis.properties
+3
-3
src/main/resources/sql/schema.sql
src/main/resources/sql/schema.sql
+2
-3
未找到文件。
pom.xml
浏览文件 @
412845c9
...
...
@@ -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>
...
...
src/main/java/com/yingjun/ssm/enums/ResultEnum.java
浏览文件 @
412845c9
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
;
...
...
src/main/java/com/yingjun/ssm/exception/MyException.java
已删除
100644 → 0
浏览文件 @
6763a541
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
);
}
}
src/main/java/com/yingjun/ssm/service/impl/GoodsServiceImpl.java
浏览文件 @
412845c9
...
...
@@ -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.
My
Exception
;
import
com.yingjun.ssm.exception.
Biz
Exception
;
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
My
Exception
(
ResultEnum
.
INVALID_USER
.
getMsg
());
throw
new
Biz
Exception
(
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
My
Exception
(
ResultEnum
.
INNER_ERROR
.
getMsg
());
throw
new
Biz
Exception
(
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
{
// 买卖成功
// 此时缓存中的数据不再是最新的,需要对缓存进行清理(具体的缓存策略还是要根据具体需求制定)
...
...
src/main/java/com/yingjun/ssm/web/GoodsController.java
浏览文件 @
412845c9
...
...
@@ -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
...
...
src/main/resources/mapper/UserDao.xml
浏览文件 @
412845c9
...
...
@@ -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
...
...
src/main/resources/redis.properties
浏览文件 @
412845c9
#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\u70
b9\u914d\u7f6e
#redis\u5355\u8282\u70
B9\u914D\u7F6E
redis.ip
=
120.27.141.45
redis.port
=
6379
#redis\u4
e3b\u4ece\u9ad8\u53ef\u7528\u914d\u7f6e
#redis\u4
E3B\u4ECE\u9AD8\u53EF\u7528\u914D\u7F6E
#sentinel1.ip=192.168.43.225
#sentinel1.port=63791
#sentinel2.ip=192.168.43.225
...
...
src/main/resources/sql/schema.sql
浏览文件 @
412845c9
--需要 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录