Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
caopu16
whatsmars
提交
4ee8bb63
W
whatsmars
项目概览
caopu16
/
whatsmars
与 Fork 源项目一致
Fork自
武汉红喜 / whatsmars
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4ee8bb63
编写于
5月 17, 2018
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Controller接口统一返回ReturnItem/ReturnItems
上级
c38e2fe1
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
348 addition
and
29 deletion
+348
-29
whatsmars-common/pom.xml
whatsmars-common/pom.xml
+13
-0
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnItem.java
...n/java/org/hongxi/whatsmars/common/result/ReturnItem.java
+54
-0
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnItemUtils.java
...a/org/hongxi/whatsmars/common/result/ReturnItemUtils.java
+45
-0
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnItems.java
.../java/org/hongxi/whatsmars/common/result/ReturnItems.java
+78
-0
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnMessage.java
...ava/org/hongxi/whatsmars/common/result/ReturnMessage.java
+97
-0
whatsmars-earth/whatsmars-earth-web/src/main/java/org/hongxi/whatsmars/earth/web/controller/NewController.java
.../hongxi/whatsmars/earth/web/controller/NewController.java
+19
-8
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/UserController.java
...ngxi/whatsmars/spring/boot/controller/UserController.java
+31
-20
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java
...org/hongxi/whatsmars/spring/boot/service/UserService.java
+2
-0
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java
...i/whatsmars/spring/boot/service/impl/UserServiceImpl.java
+9
-1
未找到文件。
whatsmars-common/pom.xml
浏览文件 @
4ee8bb63
...
@@ -97,6 +97,19 @@
...
@@ -97,6 +97,19 @@
<version>
1.6.1
</version>
<version>
1.6.1
</version>
</dependency>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-core
</artifactId>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-databind
</artifactId>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-annotations
</artifactId>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnItem.java
0 → 100644
浏览文件 @
4ee8bb63
package
org.hongxi.whatsmars.common.result
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
public
class
ReturnItem
<
T
>
{
/* 请求状态 */
private
int
status
;
/* 查询结果 */
private
T
item
;
@JsonIgnore
private
String
[]
ignoreFields
=
new
String
[]{};
public
int
getStatus
()
{
return
status
;
}
public
void
setStatus
(
int
status
)
{
this
.
status
=
status
;
}
public
T
getItem
()
{
return
item
;
}
public
void
setItem
(
T
item
)
{
this
.
item
=
item
;
}
public
String
[]
getIgnoreFields
()
{
return
ignoreFields
;
}
public
void
setIgnoreFields
(
String
[]
ignoreFields
)
{
this
.
ignoreFields
=
ignoreFields
;
}
public
ReturnItem
()
{
super
();
}
public
ReturnItem
(
int
status
,
T
item
)
{
super
();
this
.
status
=
status
;
this
.
item
=
item
;
}
public
ReturnItem
(
int
status
,
T
item
,
String
[]
ignoreFields
)
{
this
.
status
=
status
;
this
.
item
=
item
;
this
.
ignoreFields
=
ignoreFields
;
}
}
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnItemUtils.java
0 → 100644
浏览文件 @
4ee8bb63
package
org.hongxi.whatsmars.common.result
;
import
java.util.List
;
/**
* Created by shenhongxi on 2017/11/27.
*/
public
abstract
class
ReturnItemUtils
{
public
static
ReturnItem
newSuccessReturnItem
()
{
return
newReturnItem
(
true
);
}
public
static
<
T
>
ReturnItems
newSuccessReturnItems
(
List
<
T
>
messages
,
Long
total
)
{
ReturnItems
<
T
>
items
=
new
ReturnItems
();
items
.
setItems
(
messages
);
items
.
setTotal
(
total
);
items
.
setStatus
(
200
);
return
items
;
}
public
static
<
T
>
ReturnItem
newSuccessReturnItem
(
T
message
)
{
return
newReturnItem
(
true
,
message
);
}
public
static
ReturnItem
newErrorReturnItem
()
{
return
newReturnItem
(
false
);
}
public
static
ReturnItem
newReturnItem
(
Boolean
isSuccess
)
{
return
newReturnItem
(
isSuccess
,
null
);
}
public
static
<
T
>
ReturnItem
<
T
>
newReturnItem
(
Boolean
isSuccess
,
T
message
)
{
if
(
Boolean
.
TRUE
.
equals
(
isSuccess
))
{
return
new
ReturnItem
(
200
,
returnItemMsg
(
message
,
ReturnMessage
.
Message
.
OPERATION_SUCCESS
.
getValue
()));
}
else
{
return
new
ReturnItem
(
400
,
returnItemMsg
(
message
,
ReturnMessage
.
Message
.
OPERATION_SUCCESS
.
getValue
()));
}
}
private
static
Object
returnItemMsg
(
Object
message
,
String
defaultMsg
)
{
return
null
==
message
?
defaultMsg
:
message
;
}
}
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnItems.java
0 → 100644
浏览文件 @
4ee8bb63
package
org.hongxi.whatsmars.common.result
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
java.util.Date
;
import
java.util.List
;
public
class
ReturnItems
<
T
>
{
/**
* 请求状态
*/
private
int
status
;
/**
* 总条数
*/
private
Long
total
;
/**
* 查询结果集
*/
private
List
<
T
>
items
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
,
timezone
=
"GMT+8"
)
private
Date
serverDateTime
=
new
Date
();
public
int
getStatus
()
{
return
status
;
}
public
void
setStatus
(
int
status
)
{
this
.
status
=
status
;
}
public
long
getTotal
()
{
if
(
total
==
null
){
return
items
!=
null
?
items
.
size
()
:
0
;
}
return
total
;
}
public
void
setTotal
(
long
total
)
{
this
.
total
=
total
;
}
public
List
<
T
>
getItems
()
{
return
items
;
}
public
void
setItems
(
List
<
T
>
items
)
{
this
.
items
=
items
;
}
public
Date
getServerDateTime
()
{
return
serverDateTime
;
}
public
void
setServerDateTime
(
Date
serverDateTime
)
{
this
.
serverDateTime
=
serverDateTime
;
}
public
ReturnItems
()
{
super
();
}
public
ReturnItems
(
int
status
,
long
total
,
List
<
T
>
items
)
{
super
();
this
.
status
=
status
;
this
.
total
=
total
;
this
.
items
=
items
;
}
public
ReturnItems
(
List
<
T
>
items
)
{
super
();
this
.
items
=
items
;
}
}
whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ReturnMessage.java
0 → 100644
浏览文件 @
4ee8bb63
package
org.hongxi.whatsmars.common.result
;
public
class
ReturnMessage
{
private
int
status
=
200
;
private
String
message
=
"操作成功"
;
public
int
getStatus
()
{
return
status
;
}
public
void
setStatus
(
int
status
)
{
this
.
status
=
status
;
}
public
String
getMessage
()
{
return
message
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
public
ReturnMessage
()
{
super
();
}
public
ReturnMessage
(
int
status
)
{
super
();
this
.
status
=
status
;
}
public
ReturnMessage
(
int
status
,
String
message
)
{
super
();
this
.
status
=
status
;
this
.
message
=
message
;
}
public
enum
Message
{
/**
* 操作成功
*/
OPERATION_SUCCESS
(
"操作成功"
),
/**
* 操作失败
*/
OPERATION_ERROR
(
"操作失败"
),
/**
* 操作异常
*/
OPERATION_ABNORMAL
(
"操作异常"
),
/**
* 更新异常
*/
UPDATE_ANOMALIES
(
"更新异常"
),
/**
* 参数错误
*/
PARAMETER_ERROR
(
"参数错误"
),
/**
* 非法请求
*/
ILLEGAL_REQUEST
(
"非法请求"
),
/**
* 登陆超时
*/
LOGIN_TIMEOUT
(
"登陆超时"
),
/**
* 未登录
*/
NOT_LOGGED_IN
(
"未登录"
),
/**
* 登陆失败
*/
FAIL_LOGIN
(
"登陆失败"
),
/**
* 存在
*/
EXIST
(
"存在"
),
/**
* 不存在
*/
NOT_EXIST
(
"不存在"
);
String
value
;
public
String
getValue
()
{
return
value
;
}
private
Message
(
String
value
)
{
this
.
value
=
value
;
}
}
}
whatsmars-earth/whatsmars-earth-web/src/main/java/org/hongxi/whatsmars/earth/web/controller/NewController.java
浏览文件 @
4ee8bb63
package
org.hongxi.whatsmars.earth.web.controller
;
package
org.hongxi.whatsmars.earth.web.controller
;
import
org.apache.log4j.Logger
;
import
org.hongxi.whatsmars.common.result.ReturnItem
;
import
org.hongxi.whatsmars.common.result.ReturnItemUtils
;
import
org.hongxi.whatsmars.earth.service.UserService
;
import
org.hongxi.whatsmars.earth.service.UserService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
...
@@ -17,22 +20,30 @@ import org.hongxi.whatsmars.earth.domain.pojo.User;
...
@@ -17,22 +20,30 @@ import org.hongxi.whatsmars.earth.domain.pojo.User;
@RequestMapping
(
"/test"
)
@RequestMapping
(
"/test"
)
public
class
NewController
{
public
class
NewController
{
private
final
Logger
logger
=
Logger
.
getLogger
(
getClass
());
@Autowired
@Autowired
private
UserService
userService
;
private
UserService
userService
;
// localhost:8080/test/add.jhtml
// localhost:8080/test/add.jhtml
@RequestMapping
(
value
=
"/add"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
"/add"
,
method
=
RequestMethod
.
POST
)
public
HttpStatus
add
(
@RequestParam
(
name
=
"name"
)
String
username
,
public
ReturnItem
<
String
>
add
(
@RequestParam
(
name
=
"name"
)
String
username
,
@RequestParam
(
required
=
false
)
String
nickname
,
@RequestParam
(
required
=
false
)
String
nickname
,
@RequestParam
(
required
=
false
,
defaultValue
=
"1"
)
Integer
gender
,
@RequestParam
(
required
=
false
,
defaultValue
=
"1"
)
Integer
gender
,
@RequestParam
Integer
age
)
{
@RequestParam
Integer
age
)
{
try
{
User
user
=
new
User
();
User
user
=
new
User
();
user
.
setUsername
(
username
);
user
.
setUsername
(
username
);
user
.
setNickname
(
nickname
);
user
.
setNickname
(
nickname
);
user
.
setGender
(
gender
);
user
.
setGender
(
gender
);
user
.
setAge
(
age
);
user
.
setAge
(
age
);
userService
.
add
(
user
);
userService
.
add
(
user
);
return
HttpStatus
.
OK
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"##########test add error"
,
e
);
return
ReturnItemUtils
.
newErrorReturnItem
();
}
return
ReturnItemUtils
.
newSuccessReturnItem
();
}
}
}
}
...
...
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/UserController.java
浏览文件 @
4ee8bb63
package
org.hongxi.whatsmars.spring.boot.controller
;
package
org.hongxi.whatsmars.spring.boot.controller
;
import
com.github.pagehelper.Page
;
import
com.github.pagehelper.Page
;
import
org.apache.logging.log4j.LogManager
;
import
org.hongxi.whatsmars.spring.boot.common.ReturnItemUtils
;
import
org.hongxi.whatsmars.spring.boot.common.pojo.ReturnItem
;
import
org.hongxi.whatsmars.spring.boot.common.pojo.ReturnItems
;
import
org.hongxi.whatsmars.spring.boot.common.pojo.ReturnItems
;
import
org.hongxi.whatsmars.spring.boot.exception.AppException
;
import
org.hongxi.whatsmars.spring.boot.exception.AppException
;
import
org.hongxi.whatsmars.spring.boot.model.User
;
import
org.hongxi.whatsmars.spring.boot.model.User
;
...
@@ -13,21 +16,25 @@ import java.util.*;
...
@@ -13,21 +16,25 @@ import java.util.*;
/**
/**
* Created by shenhongxi on 2017/11/16.
* Created by shenhongxi on 2017/11/16.
* @see org.hongxi.whatsmars.spring.boot.exception.AppExceptionHandler
*/
*/
@RestController
@RestController
@RequestMapping
(
"/user"
)
@RequestMapping
(
"/user"
)
public
class
UserController
{
public
class
UserController
{
private
final
org
.
apache
.
logging
.
log4j
.
Logger
logger
=
LogManager
.
getLogger
(
getClass
());
@Autowired
@Autowired
private
UserService
userService
;
private
UserService
userService
;
@GetMapping
(
"/find/{username}"
)
@GetMapping
(
"/find/{username}"
)
public
User
find
(
@PathVariable
(
"username"
)
String
username
)
{
public
ReturnItem
<
User
>
find
(
@PathVariable
(
"username"
)
String
username
)
{
return
userService
.
findByUsername
(
username
);
User
user
=
userService
.
findByUsername
(
username
);
return
ReturnItemUtils
.
newSuccessReturnItem
(
user
);
}
}
@PostMapping
(
"/add"
)
@PostMapping
(
"/add"
)
public
HttpStatus
add
(
@RequestParam
(
name
=
"name"
)
String
username
,
public
ReturnItem
<
String
>
add
(
@RequestParam
(
name
=
"name"
)
String
username
,
@RequestParam
(
required
=
false
)
String
nickname
,
@RequestParam
(
required
=
false
)
String
nickname
,
@RequestParam
(
required
=
false
,
defaultValue
=
"1"
)
Integer
gender
,
@RequestParam
(
required
=
false
,
defaultValue
=
"1"
)
Integer
gender
,
@RequestParam
Integer
age
)
{
@RequestParam
Integer
age
)
{
...
@@ -37,19 +44,19 @@ public class UserController {
...
@@ -37,19 +44,19 @@ public class UserController {
user
.
setGender
(
gender
);
user
.
setGender
(
gender
);
user
.
setAge
(
age
);
user
.
setAge
(
age
);
userService
.
add
(
user
);
userService
.
add
(
user
);
return
HttpStatus
.
OK
;
return
ReturnItemUtils
.
newSuccessReturnItem
()
;
}
}
@PutMapping
(
"/update"
)
@PutMapping
(
"/update"
)
public
HttpStatus
update
(
@RequestBody
User
user
)
{
// 以json格式接收参数, RequestBody也可省略
public
ReturnItem
<
String
>
update
(
@RequestBody
User
user
)
{
// 以json格式接收参数, RequestBody也可省略
userService
.
update
(
user
);
userService
.
update
(
user
);
return
HttpStatus
.
OK
;
return
ReturnItemUtils
.
newSuccessReturnItem
()
;
}
}
@DeleteMapping
(
"/delete"
)
@DeleteMapping
(
"/delete"
)
public
HttpStatus
delete
(
@RequestParam
Long
id
)
{
public
ReturnItem
<
String
>
delete
(
@RequestParam
Long
id
)
{
userService
.
delete
(
id
);
userService
.
delete
(
id
);
return
HttpStatus
.
OK
;
return
ReturnItemUtils
.
newSuccessReturnItem
()
;
}
}
@RequestMapping
(
value
=
"/e"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
value
=
"/e"
,
method
=
RequestMethod
.
GET
)
...
@@ -61,12 +68,8 @@ public class UserController {
...
@@ -61,12 +68,8 @@ public class UserController {
@GetMapping
(
"/query"
)
@GetMapping
(
"/query"
)
public
ReturnItems
<
User
>
query
(
@RequestParam
(
value
=
"offset"
,
defaultValue
=
"0"
)
Integer
offset
,
public
ReturnItems
<
User
>
query
(
@RequestParam
(
value
=
"offset"
,
defaultValue
=
"0"
)
Integer
offset
,
@RequestParam
(
value
=
"limit"
,
defaultValue
=
"30"
)
Integer
limit
)
{
@RequestParam
(
value
=
"limit"
,
defaultValue
=
"30"
)
Integer
limit
)
{
ReturnItems
<
User
>
returnItems
=
new
ReturnItems
<>();
Page
<
User
>
page
=
userService
.
query
(
offset
,
limit
);
Page
<
User
>
page
=
userService
.
query
(
offset
,
limit
);
returnItems
.
setItems
(
page
.
getResult
());
return
ReturnItemUtils
.
newSuccessReturnItems
(
page
.
getResult
(),
page
.
getTotal
());
returnItems
.
setTotal
(
page
.
getTotal
());
returnItems
.
setStatus
(
200
);
return
returnItems
;
}
}
@GetMapping
(
"/findByNicknameAndGender"
)
@GetMapping
(
"/findByNicknameAndGender"
)
...
@@ -74,14 +77,23 @@ public class UserController {
...
@@ -74,14 +77,23 @@ public class UserController {
@RequestParam
Integer
gender
)
{
@RequestParam
Integer
gender
)
{
ReturnItems
<
User
>
returnItems
=
new
ReturnItems
<>();
ReturnItems
<
User
>
returnItems
=
new
ReturnItems
<>();
List
<
User
>
users
=
userService
.
findByNicknameAndGender
(
nickname
,
gender
);
List
<
User
>
users
=
userService
.
findByNicknameAndGender
(
nickname
,
gender
);
returnItems
.
setItems
(
users
);
long
total
=
users
==
null
?
0
:
users
.
size
();
returnItems
.
setTotal
(
users
==
null
?
0
:
users
.
size
());
return
ReturnItemUtils
.
newSuccessReturnItems
(
users
,
total
);
returnItems
.
setStatus
(
200
);
return
returnItems
;
}
}
@PostMapping
(
"/addBatch"
)
@PostMapping
(
"/addBatch"
)
public
HttpStatus
addBatch
()
{
public
ReturnItem
<
String
>
addBatch
()
{
try
{
userService
.
testTransaction
(
buildUsers
());
}
catch
(
Exception
e
)
{
logger
.
error
(
"#########user addBatch error"
,
e
);
return
ReturnItemUtils
.
newErrorReturnItem
();
}
return
ReturnItemUtils
.
newSuccessReturnItem
();
}
private
List
<
User
>
buildUsers
()
{
List
<
User
>
users
=
new
ArrayList
<>();
List
<
User
>
users
=
new
ArrayList
<>();
Date
now
=
new
Date
();
Date
now
=
new
Date
();
long
t
=
now
.
getTime
();
long
t
=
now
.
getTime
();
...
@@ -101,7 +113,6 @@ public class UserController {
...
@@ -101,7 +113,6 @@ public class UserController {
user
.
setCreateDate
(
now
);
user
.
setCreateDate
(
now
);
user
.
setUpdateDate
(
now
);
user
.
setUpdateDate
(
now
);
users
.
add
(
user
);
users
.
add
(
user
);
userService
.
addBatch
(
users
);
return
users
;
return
HttpStatus
.
OK
;
}
}
}
}
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java
浏览文件 @
4ee8bb63
...
@@ -26,4 +26,6 @@ public interface UserService {
...
@@ -26,4 +26,6 @@ public interface UserService {
List
<
User
>
findByNicknameAndGender
(
String
nickname
,
Integer
gender
);
List
<
User
>
findByNicknameAndGender
(
String
nickname
,
Integer
gender
);
void
testTransaction
(
List
<
User
>
users
);
}
}
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java
浏览文件 @
4ee8bb63
...
@@ -55,10 +55,12 @@ public class UserServiceImpl implements UserService {
...
@@ -55,10 +55,12 @@ public class UserServiceImpl implements UserService {
userMapper
.
insertBatch
(
users
);
userMapper
.
insertBatch
(
users
);
}
}
@Transactional
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Override
@Override
public
void
add
(
List
<
User
>
users
)
{
public
void
add
(
List
<
User
>
users
)
{
int
i
=
0
;
for
(
User
user
:
users
)
{
for
(
User
user
:
users
)
{
// if (i++ == 1) throw new RuntimeException("TEST TX");
userMapper
.
insert
(
user
);
userMapper
.
insert
(
user
);
}
}
}
}
...
@@ -67,4 +69,10 @@ public class UserServiceImpl implements UserService {
...
@@ -67,4 +69,10 @@ public class UserServiceImpl implements UserService {
public
List
<
User
>
findByNicknameAndGender
(
String
nickname
,
Integer
gender
)
{
public
List
<
User
>
findByNicknameAndGender
(
String
nickname
,
Integer
gender
)
{
return
userMapper
.
findByNicknameAndGender
(
nickname
,
gender
);
return
userMapper
.
findByNicknameAndGender
(
nickname
,
gender
);
}
}
@Override
public
void
testTransaction
(
List
<
User
>
users
)
{
// 同一个类中调有事务的方法,无事务,因为事务的本质是调代理
add
(
users
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录