Skip to content

Commit ea53f3f

Browse files
committed
实现
1 parent 7e54e34 commit ea53f3f

File tree

7 files changed

+120
-40
lines changed

7 files changed

+120
-40
lines changed

aries/src/main/java/info/xiaomo/aries/base/BaseController.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import info.xiaomo.core.controller.Result;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
6+
import org.springframework.data.domain.Page;
67
import org.springframework.web.bind.annotation.PathVariable;
78
import org.springframework.web.bind.annotation.RequestBody;
89

@@ -18,52 +19,77 @@ public abstract class BaseController<T> {
1819

1920
/**
2021
* 查找所有(不带分页)
22+
*
2123
* @return result
2224
*/
2325
public abstract Result<List<T>> findAll();
2426

2527
/**
2628
* 带分页
27-
* @param start 起始页
29+
*
30+
* @param start 起始页
2831
* @param pageSize 页码数
2932
* @return result
3033
*/
31-
public abstract Result<List<T>> findAll(@PathVariable int start, @PathVariable int pageSize);
34+
public abstract Result<Page<T>> findAll(@PathVariable int start, @PathVariable int pageSize);
3235

3336
/**
3437
* 根据id查看模型
38+
*
3539
* @param id id
3640
* @return result
3741
*/
3842
public abstract Result<T> findById(@PathVariable Long id);
3943

4044
/**
4145
* 根据名字查找模型
46+
*
4247
* @param name name
4348
* @return result
4449
*/
4550
public abstract Result<T> findByName(@PathVariable String name);
4651

4752
/**
4853
* 根据名字删除模型
54+
*
4955
* @param name name
5056
* @return result
5157
*/
52-
public abstract Result<String> del(@PathVariable String name);
58+
public abstract Result<Boolean> delByName(@PathVariable String name);
5359

5460

5561
/**
5662
* 根据id删除模型
63+
*
5764
* @param id id
5865
* @return result
5966
*/
60-
public abstract Result<String> del(@PathVariable Long id);
67+
public abstract Result<Boolean> delById(@PathVariable Long id);
6168

6269
/**
6370
* 添加模型
71+
*
6472
* @param model model
6573
* @return result
6674
*/
67-
public abstract Result<T> add(@RequestBody T model);
75+
public abstract Result<Boolean> add(@RequestBody T model);
76+
77+
78+
/**
79+
* 更新
80+
*
81+
* @param model model
82+
* @return result
83+
*/
84+
public abstract Result<Boolean> update(@RequestBody T model);
85+
86+
87+
/**
88+
* 批量删除
89+
*
90+
* @param ids ids
91+
* @return result
92+
*/
93+
public abstract Result<Boolean> delByIds(@PathVariable List<Long> ids);
6894

6995
}

aries/src/main/java/info/xiaomo/aries/base/BaseModel.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lombok.Data;
88

99
import javax.persistence.*;
10+
import java.io.Serializable;
1011
import java.util.Date;
1112

1213
/**
@@ -17,14 +18,18 @@
1718
@MappedSuperclass
1819
@Data
1920
@ApiModel("基类")
20-
public class BaseModel {
21+
public class BaseModel implements Serializable{
2122

2223
@Id
2324
@GeneratedValue(strategy = GenerationType.AUTO)
2425
@Column(name = "Id")
2526
@ApiModelProperty(value = "id")
2627
private long id;
2728

29+
@ApiModelProperty(value = "名字")
30+
@Column(name = "Name")
31+
private String name;
32+
2833
@JsonSerialize(using = CustomDateSerializer.class)
2934
@Column(name = "CreateTime")
3035
@ApiModelProperty(value = "创建时间")

aries/src/main/java/info/xiaomo/aries/base/BaseService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public interface BaseService<T> {
1818

1919
Page<T> findAll(int start, int pageSize);
2020

21-
void delById(Long id);
21+
boolean delById(Long id);
2222

23-
void delByName(String name);
23+
boolean delByName(String name);
2424

25-
void add(T model);
25+
boolean add(T model);
2626

27-
void update(T model);
27+
boolean update(T model);
28+
29+
boolean delByIds(List<Long> ids);
2830
}

aries/src/main/java/info/xiaomo/aries/controller/UserController.java

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import info.xiaomo.aries.model.UserModel;
55
import info.xiaomo.aries.service.UserService;
66
import info.xiaomo.core.controller.Result;
7+
import io.swagger.annotations.Api;
78
import io.swagger.annotations.ApiOperation;
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
109
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.domain.Page;
1111
import org.springframework.http.MediaType;
1212
import org.springframework.web.bind.annotation.*;
1313

@@ -20,9 +20,8 @@
2020
*/
2121
@RestController
2222
@RequestMapping("/user")
23-
public class UserController extends BaseController {
24-
25-
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
23+
@Api(value = "UserController", description = "用户相关api")
24+
public class UserController extends BaseController<UserModel> {
2625

2726
private final UserService service;
2827

@@ -39,34 +38,70 @@ public Result<List<UserModel>> findAll() {
3938
return new Result<>(all);
4039
}
4140

41+
@RequestMapping(value = "findAllWithPage", method = RequestMethod.GET)
42+
@ApiOperation(value = "返回所有用户数据,带分页", notes = "返回所有用户数据,传入页码和分页数。", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
43+
@Override
44+
public Result<Page<UserModel>> findAll(@PathVariable int start, @PathVariable int pageSize) {
45+
Page<UserModel> all = service.findAll(start, pageSize);
46+
return new Result<>(all);
47+
}
48+
49+
@RequestMapping(value = "findById", method = RequestMethod.GET)
50+
@ApiOperation(value = "根据Id查找数据", notes = "根据Id查找数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
4251
@Override
43-
public Result<List> findAll(@PathVariable int start, @PathVariable int pageSize) {
44-
return null;
52+
public Result<UserModel> findById(@PathVariable Long id) {
53+
UserModel model = service.findById(id);
54+
return new Result<>(model);
4555
}
4656

57+
@RequestMapping(value = "findByName", method = RequestMethod.GET)
58+
@ApiOperation(value = "根据名字查找数据", notes = "根据名字查找数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
4759
@Override
48-
public Result findById(@PathVariable Long id) {
49-
return null;
60+
public Result<UserModel> findByName(@PathVariable String name) {
61+
UserModel model = service.findByName(name);
62+
return new Result<>(model);
5063
}
5164

65+
@RequestMapping(value = "delByName", method = RequestMethod.GET)
66+
@ApiOperation(value = "根据名字删除数据", notes = "根据名字删除数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
5267
@Override
53-
public Result findByName(@PathVariable String name) {
54-
return null;
68+
public Result<Boolean> delByName(@PathVariable String name) {
69+
boolean b = service.delByName(name);
70+
return new Result<>(b);
5571
}
5672

73+
@RequestMapping(value = "delById", method = RequestMethod.GET)
74+
@ApiOperation(value = "根据id删除数据", notes = "根据id删除数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
5775
@Override
58-
public Result<String> del(@PathVariable String name) {
59-
return null;
76+
public Result<Boolean> delById(@PathVariable Long id) {
77+
boolean b = service.delById(id);
78+
return new Result<>(b);
6079
}
6180

81+
82+
@RequestMapping(value = "add", method = RequestMethod.POST)
83+
@ApiOperation(value = "添加数据", notes = "添加数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
6284
@Override
63-
public Result<String> del(@PathVariable Long id) {
64-
return null;
85+
public Result<Boolean> add(@RequestBody UserModel model) {
86+
boolean b = service.add(model);
87+
return new Result<>(b);
6588
}
6689

90+
@RequestMapping(value = "update", method = RequestMethod.POST)
91+
@ApiOperation(value = "更新数据", notes = "添加数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
6792
@Override
68-
public Result add(@RequestBody Object model) {
69-
return null;
93+
public Result<Boolean> update(@RequestBody UserModel model) {
94+
boolean update = service.update(model);
95+
return new Result<>(update);
7096
}
7197

98+
@RequestMapping(value = "delByIds", method = RequestMethod.GET)
99+
@ApiOperation(value = "根据ids批量删除数据", notes = "根据ids批量删除数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
100+
@Override
101+
public Result<Boolean> delByIds(@PathVariable List<Long> ids) {
102+
boolean b = service.delByIds(ids);
103+
return new Result<>(b);
104+
}
105+
106+
72107
}

aries/src/main/java/info/xiaomo/aries/dao/UserDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public interface UserDao extends JpaRepository<UserModel, Long> {
1515

1616
UserModel findByName(String name);
1717

18-
void deleteByName(String name);
18+
boolean deleteByName(String name);
1919

2020
}

aries/src/main/java/info/xiaomo/aries/model/UserModel.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import javax.persistence.Entity;
1111
import javax.persistence.Table;
12-
import java.io.Serializable;
1312

1413
/**
1514
* @author 小莫 (https://xiaomo.info) (https://github.com/syoubaku)
@@ -22,8 +21,6 @@
2221
@EqualsAndHashCode(callSuper = false)
2322
@AllArgsConstructor
2423
@ApiModel(value = "用户实体类")
25-
public class UserModel extends BaseModel implements Serializable {
26-
27-
private String name;
24+
public class UserModel extends BaseModel {
2825

2926
}

aries/src/main/java/info/xiaomo/aries/service/impl/UserServiceImpl.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,47 @@ public Page<UserModel> findAll(int start, int pageSize) {
4949
}
5050

5151
@Override
52-
public void delById(Long id) {
53-
userDao.delete(id);
52+
public boolean delById(Long id) {
53+
try {
54+
userDao.delete(id);
55+
} catch (Exception e) {
56+
return false;
57+
}
58+
return true;
5459
}
5560

5661
@Override
57-
public void delByName(String name) {
58-
userDao.deleteByName(name);
62+
public boolean delByName(String name) {
63+
return userDao.deleteByName(name);
5964
}
6065

6166
@Override
62-
public void add(UserModel model) {
67+
public boolean add(UserModel model) {
6368
UserModel userModel = userDao.findByName(model.getName());
6469
if (userModel != null) {
6570
LOGGER.debug("用户{}己经存在", userModel.getName());
66-
return;
71+
return false;
6772
}
6873
userDao.save(model);
74+
return true;
6975
}
7076

7177
@Override
72-
public void update(UserModel model) {
78+
public boolean update(UserModel model) {
7379
UserModel userModel = userDao.findById(model.getId());
7480
if (userModel == null) {
7581
LOGGER.debug("用户{}不存在", model.getName());
76-
return;
82+
return false;
7783
}
7884
userDao.save(model);
85+
return true;
86+
}
87+
88+
@Override
89+
public boolean delByIds(List<Long> ids) {
90+
for (Long id : ids) {
91+
userDao.delete(id);
92+
}
93+
return true;
7994
}
8095
}

0 commit comments

Comments
 (0)