Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java-training
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pengbin
java-training
Commits
fa51c670
Commit
fa51c670
authored
Nov 08, 2018
by
pengbin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
project
parent
24d0e449
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
42 additions
and
3 deletions
+42
-3
mydemo/src/main/java/com/saraad/common/util/IdWorker.java
+3
-0
mydemo/src/main/java/com/saraad/controller/TestController.java
+5
-0
mydemo/src/main/java/com/saraad/dao/ProductDao.java
+16
-3
mydemo/src/main/java/com/saraad/service/ProductService.java
+14
-0
mydemo/src/main/resources/application-dev.yml
+4
-0
No files found.
mydemo/src/main/java/com/saraad/common/util/IdWorker.java
View file @
fa51c670
...
@@ -95,6 +95,9 @@ public class IdWorker {
...
@@ -95,6 +95,9 @@ public class IdWorker {
* @Time2017-6-8 15:02:29
* @Time2017-6-8 15:02:29
*/
*/
public
static
long
generateId
()
{
public
static
long
generateId
()
{
//未初始化swquence时生成Id会报空指针,若未初始化,默认机器码为100初始化
if
(
null
==
sequence
)
init
(
100
);
try
{
try
{
while
(
true
)
{
while
(
true
)
{
//序号+1
//序号+1
...
...
mydemo/src/main/java/com/saraad/controller/TestController.java
View file @
fa51c670
...
@@ -49,6 +49,11 @@ public class TestController {
...
@@ -49,6 +49,11 @@ public class TestController {
return
productService
.
queryAll
();
return
productService
.
queryAll
();
}
}
@ApiOperation
(
"测试新增"
)
@PostMapping
(
"/addTest"
)
public
BaseResponse
addTest
(
@RequestBody
ProductVo
productVo
){
return
productService
.
addTest
(
productVo
);
}
...
...
mydemo/src/main/java/com/saraad/dao/ProductDao.java
View file @
fa51c670
...
@@ -27,9 +27,15 @@ public class ProductDao {
...
@@ -27,9 +27,15 @@ public class ProductDao {
static
{
static
{
ArrayList
<
ProductVo
>
products
=
new
ArrayList
<>();
ArrayList
<
ProductVo
>
products
=
new
ArrayList
<>();
products
.
add
(
new
ProductVo
(
IdWorker
.
generateId
(),
"product_01"
,
"10001"
,
"初始商品1"
));
try
{
products
.
add
(
new
ProductVo
(
IdWorker
.
generateId
(),
"product_02"
,
"10002"
,
"初始商品2"
));
products
.
add
(
new
ProductVo
(
IdWorker
.
generateId
(),
"product_01"
,
"10001"
,
"初始商品1"
));
products
.
add
(
new
ProductVo
(
IdWorker
.
generateId
(),
"product_03"
,
"10003"
,
"初始商品3"
));
Thread
.
sleep
(
1
);
products
.
add
(
new
ProductVo
(
IdWorker
.
generateId
(),
"product_02"
,
"10002"
,
"初始商品2"
));
Thread
.
sleep
(
1
);
products
.
add
(
new
ProductVo
(
IdWorker
.
generateId
(),
"product_03"
,
"10003"
,
"初始商品3"
));
}
catch
(
InterruptedException
e
)
{
}
list
=
products
;
list
=
products
;
}
}
...
@@ -44,7 +50,14 @@ public class ProductDao {
...
@@ -44,7 +50,14 @@ public class ProductDao {
if
(
null
==
productVo
)
return
0
;
if
(
null
==
productVo
)
return
0
;
if
(
null
==
productVo
.
getId
())
return
0
;
if
(
null
==
productVo
.
getId
())
return
0
;
//查看当前id的productVo是否已存在
//查看当前id的productVo是否已存在
// Stream<ProductVo> stream = list.stream().filter(p -> p.getId().longValue() == productVo.getId().longValue());
// List<ProductVo> exsitList = stream.collect(Collectors.toList());
List
<
ProductVo
>
exsitList
=
list
.
stream
().
filter
(
p
->
p
.
getId
().
longValue
()
==
productVo
.
getId
().
longValue
()).
collect
(
Collectors
.
toList
());
List
<
ProductVo
>
exsitList
=
list
.
stream
().
filter
(
p
->
p
.
getId
().
longValue
()
==
productVo
.
getId
().
longValue
()).
collect
(
Collectors
.
toList
());
// ArrayList<ProductVo> exsitList = new ArrayList<>();
// for (ProductVo vo : list) {
// if (productVo.getId().longValue() == vo.getId().longValue())
// exsitList.add(vo);
// }
if
(!
CollectionUtils
.
isEmpty
(
exsitList
))
if
(!
CollectionUtils
.
isEmpty
(
exsitList
))
return
0
;
return
0
;
list
.
add
(
productVo
);
list
.
add
(
productVo
);
...
...
mydemo/src/main/java/com/saraad/service/ProductService.java
View file @
fa51c670
...
@@ -57,4 +57,18 @@ public class ProductService {
...
@@ -57,4 +57,18 @@ public class ProductService {
return
new
BaseResponse
(
Version
.
VERSION_1
.
getCode
(),
StatusEnum
.
STATUS_100
.
getCode
(),
StatusEnum
.
STATUS_100
.
getMesssage
(),
list
);
return
new
BaseResponse
(
Version
.
VERSION_1
.
getCode
(),
StatusEnum
.
STATUS_100
.
getCode
(),
StatusEnum
.
STATUS_100
.
getMesssage
(),
list
);
}
}
/**
* add
* @param productVo
* @return
*/
public
BaseResponse
addTest
(
ProductVo
productVo
){
// productVo.setId(IdWorker.generateId());
Integer
res
=
productDao
.
add
(
productVo
);
if
(
1
==
res
){
return
new
BaseResponse
(
Version
.
VERSION_1
.
getCode
(),
StatusEnum
.
STATUS_100
.
getCode
(),
StatusEnum
.
STATUS_100
.
getMesssage
());
}
return
new
BaseResponse
(
Version
.
VERSION_1
.
getCode
(),
StatusEnum
.
STATUS_101
.
getCode
(),
StatusEnum
.
STATUS_101
.
getMesssage
());
}
}
}
mydemo/src/main/resources/application-dev.yml
View file @
fa51c670
#服务端口,路径
server
:
server
:
port
:
7777
port
:
7777
context-path
:
/product
context-path
:
/product
#服务名
spring
:
spring
:
application
:
application
:
name
:
product
name
:
product
#swagger配置
swagger
:
swagger
:
basepackage
:
"
com.saraad.controller"
basepackage
:
"
com.saraad.controller"
title
:
"
Product
Service"
title
:
"
Product
Service"
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment