From 7e82960350ea13b94bc52989ba3d7831296db526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8F=8B=E5=A5=87?= <2689725568@qq.com> Date: Tue, 19 Feb 2019 19:02:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/btn.sql | 2 +- .../nbt/controller/ArticleController.java | 102 ++++++++++++++++ .../nbt/controller/UserController.java | 23 ++-- .../nbt/controller/UserFriendsController.java | 114 +++++++++--------- .../nbt/model/view/ArticleEntityView.java | 7 ++ .../nbt/model/view/ComentEntityView.java | 6 + .../nbt/model/view/SortsEntityView.java | 7 ++ .../nbt/model/view/UserEntityView.java | 6 + .../nbt/model/view/UserFriendsEntityView.java | 7 ++ .../nbt/model/view/ViewsEntityView.java | 7 ++ .../library/nbt/service/ArticleService.java | 6 + .../com/library/nbt/service/BaseService.java | 4 +- .../com/library/nbt/service/UserService.java | 3 +- .../nbt/service/impl/ArticleServiceImpl.java | 82 +++++++++++++ .../service/impl/UserFriendsServiceImpl.java | 19 +-- .../nbt/service/impl/UserServiceImpl.java | 63 +++++----- src/main/resources/application.properties | 2 +- 17 files changed, 340 insertions(+), 120 deletions(-) create mode 100644 src/main/java/com/library/nbt/controller/ArticleController.java create mode 100644 src/main/java/com/library/nbt/model/view/ArticleEntityView.java create mode 100644 src/main/java/com/library/nbt/model/view/ComentEntityView.java create mode 100644 src/main/java/com/library/nbt/model/view/SortsEntityView.java create mode 100644 src/main/java/com/library/nbt/model/view/UserEntityView.java create mode 100644 src/main/java/com/library/nbt/model/view/UserFriendsEntityView.java create mode 100644 src/main/java/com/library/nbt/model/view/ViewsEntityView.java create mode 100644 src/main/java/com/library/nbt/service/ArticleService.java create mode 100644 src/main/java/com/library/nbt/service/impl/ArticleServiceImpl.java diff --git a/doc/btn.sql b/doc/btn.sql index b160504..a118ad1 100644 --- a/doc/btn.sql +++ b/doc/btn.sql @@ -14,7 +14,7 @@ CREATE TABLE `t_user` ( `profile_photo` VARCHAR(255) DEFAULT NULL COMMENT '用户头像 (头像保存路径,第一次注册使用默认头像)', `rights` int(1) DEFAULT NULL COMMENT '用户权限:0为管理员,1为用户,', `ip` VARCHAR(20) DEFAULT NULL COMMENT '用户所在ip', - `login_end_time` BIGINT NOT NULL default '' COMMENT '最后登录时间', + `login_end_time` BIGINT NOT NULL default 0 COMMENT '最后登录时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表'; diff --git a/src/main/java/com/library/nbt/controller/ArticleController.java b/src/main/java/com/library/nbt/controller/ArticleController.java new file mode 100644 index 0000000..c4a071d --- /dev/null +++ b/src/main/java/com/library/nbt/controller/ArticleController.java @@ -0,0 +1,102 @@ +package com.library.nbt.controller; + +import com.library.nbt.model.entity.ArticleEntity; +import com.library.nbt.model.entity.UserEntity; +import com.library.nbt.service.ArticleService; +import com.library.nbt.service.UserService; +import com.library.nbt.utils.Result; +import com.library.nbt.utils.ResultUtil; +import io.swagger.annotations.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping(value = "/api") +@Api(value = "文章表接口", tags = "文章表接口", description = "") +public class ArticleController { + + private static Logger LOG = LoggerFactory.getLogger(ArticleController.class); + + @Autowired + private ArticleService articleService; + + @Value("${db.url}") + private String url; + + + @ApiOperation(value = "通过ID获取单个文章表", notes = "通过id获取文章信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/article/select/{id}", method = RequestMethod.GET) + public Result getEntity(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) { + try { + ArticleEntity articleEntity = articleService.getEntity(id); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, articleEntity); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.SUCCESS_CODE_200, e.getMessage()); + } + } + + @ApiOperation(value = "进行插入新的一条文章信息", notes = "进行插入新的一条文章信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/article/insert", method = RequestMethod.POST) + public Result saveEntity(@RequestBody ArticleEntity articleEntity) { + try { + ArticleEntity articleEntityResult = articleService.saveEntity(articleEntity); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, articleEntityResult); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); + } + } + + @ApiOperation(value = "通过ID删除一条文章信息", notes = "通过ID删除一条文章信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/article/delete/{id}", method = RequestMethod.DELETE) + public Result deleteEntity(@ApiParam(value = "文章ID", required = true) @PathVariable(value = "id") Integer id) { + + try { + articleService.deleteEntity(id); + return ResultUtil.success(); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); + } + } + + @ApiOperation(value = "修改文章信息", notes = "修改文章信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/article/update", method = RequestMethod.POST) + public Result updateEntity(@RequestBody ArticleEntity articleEntity) { + try { + articleService.updateEntity(articleEntity); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, articleEntity); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); + } + } + + @ApiOperation(value = "获取所有文章", notes = "获取所有文章") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/article/findAll", method = RequestMethod.POST) + public Result findAll() { + try { + List resultArtcle = articleService.findAll(); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, resultArtcle); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); + } + } +} diff --git a/src/main/java/com/library/nbt/controller/UserController.java b/src/main/java/com/library/nbt/controller/UserController.java index 55c6a51..74f4195 100644 --- a/src/main/java/com/library/nbt/controller/UserController.java +++ b/src/main/java/com/library/nbt/controller/UserController.java @@ -1,6 +1,7 @@ package com.library.nbt.controller; import com.library.nbt.model.entity.UserEntity; +import com.library.nbt.model.view.UserEntityView; import com.library.nbt.service.UserService; import com.library.nbt.utils.Result; import com.library.nbt.utils.ResultUtil; @@ -32,10 +33,10 @@ public class UserController { @ApiResponse(code = 200, message = "successful request"), @ApiResponse(code = 500, message = "internal server error")}) @RequestMapping(value = "/v1/user/select/{id}", method = RequestMethod.GET) - public Result getEntity(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) { + public Result getEntity(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) { try { - UserEntity userEntity = userService.getEntity(id); - return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, userEntity); + UserEntityView userEntityView = userService.getEntity(id); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, userEntityView); } catch (Exception e) { return ResultUtil.error(ResultUtil.SUCCESS_CODE_200, e.getMessage()); } @@ -46,9 +47,9 @@ public class UserController { @ApiResponse(code = 200, message = "successful request"), @ApiResponse(code = 500, message = "internal server error")}) @RequestMapping(value = "/v1/user/insert", method = RequestMethod.POST) - public Result saveEntity(@RequestBody UserEntity userEntity) { + public Result saveEntity(@RequestBody UserEntityView userEntityView) { try { - UserEntity resultUser = userService.saveEntity(userEntity); + UserEntity resultUser = userService.saveEntity(userEntityView); return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, resultUser); } catch (Exception e) { return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); @@ -60,7 +61,7 @@ public class UserController { @ApiResponse(code = 200, message = "successful request"), @ApiResponse(code = 500, message = "internal server error")}) @RequestMapping(value = "/v1/user/delete/{id}", method = RequestMethod.DELETE) - public Result deleteEntity(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) { + public Result deleteEntity(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) { try { userService.deleteEntity(id); @@ -75,10 +76,10 @@ public class UserController { @ApiResponse(code = 200, message = "successful request"), @ApiResponse(code = 500, message = "internal server error")}) @RequestMapping(value = "/v1/user/update", method = RequestMethod.POST) - public Result updateEntity(@RequestBody UserEntity userEntity) { + public Result updateEntity(@RequestBody UserEntityView userEntityView) { try { - UserEntity resultUser = userService.updateEntity(userEntity); - return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, resultUser); + userService.updateEntity(userEntityView); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, userEntityView); } catch (Exception e) { return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); } @@ -89,9 +90,9 @@ public class UserController { @ApiResponse(code = 200, message = "successful request"), @ApiResponse(code = 500, message = "internal server error")}) @RequestMapping(value = "/v1/user/findAll", method = RequestMethod.POST) - public Result findAll() { + public Result findAll() { try { - List resultUser = userService.findAll(); + List resultUser = userService.findAll(); return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, resultUser); } catch (Exception e) { return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); diff --git a/src/main/java/com/library/nbt/controller/UserFriendsController.java b/src/main/java/com/library/nbt/controller/UserFriendsController.java index 3cc9b86..0802cff 100644 --- a/src/main/java/com/library/nbt/controller/UserFriendsController.java +++ b/src/main/java/com/library/nbt/controller/UserFriendsController.java @@ -1,6 +1,7 @@ package com.library.nbt.controller; import com.library.nbt.model.entity.UserEntity; +import com.library.nbt.model.entity.UserFriendsEntity; import com.library.nbt.service.UserFriendsService; import com.library.nbt.service.UserService; import com.library.nbt.utils.Result; @@ -21,63 +22,60 @@ public class UserFriendsController { @Autowired private UserFriendsService userFriendsService; - @Value("${db.url}") - private String url; -// -// @ApiOperation(value = "通过ID获取单个用户好友表", notes = "通过id获取用户好友信息") -// @ApiResponses(value = { -// @ApiResponse(code = 200, message = "successful request"), -// @ApiResponse(code = 500, message = "internal server error")}) -// @RequestMapping(value = "/v1/user/friends/select/{id}", method = RequestMethod.GET) -// public Result select(@ApiParam(value = "ID主键", required = true) @PathVariable(value = "id") Integer id) { -// try { -// UserEntity userEntity = userFriendsService.getEntity(id); -// return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, userEntity); -// } catch (Exception e) { -// return ResultUtil.error(ResultUtil.SUCCESS_CODE_200, e.getMessage()); -// } -// } -// -// @ApiOperation(value = "进行插入新的一条用户好友信息", notes = "进行插入新的一条用户好友信息") -// @ApiResponses(value = { -// @ApiResponse(code = 200, message = "successful request"), -// @ApiResponse(code = 500, message = "internal server error")}) -// @RequestMapping(value = "/v1/user/friends/insert", method = RequestMethod.POST) -// public Result saveEntiy(@RequestBody UserEntity userEntity) { -// try { -// UserEntity resultUser = userFriendsService.add(userEntity); -// return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, resultUser); -// } catch (Exception e) { -// return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); -// } -// } -// -// @ApiOperation(value = "通过ID删除一条用户好友信息", notes = "通过ID删除一条用户好友信息") -// @ApiResponses(value = { -// @ApiResponse(code = 200, message = "successful request"), -// @ApiResponse(code = 500, message = "internal server error")}) -// @RequestMapping(value = "/v1/user/friends/delete/{id}", method = RequestMethod.DELETE) -// public Result delete(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) { -// try { -// Integer result = userFriendsService.delete(id); -// return ResultUtil.success(); -// } catch (Exception e) { -// return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); -// } -// } -// -// @ApiOperation(value = "修改用户好友信息", notes = "修改用户好友信息") -// @ApiResponses(value = { -// @ApiResponse(code = 200, message = "successful request"), -// @ApiResponse(code = 500, message = "internal server error")}) -// @RequestMapping(value = "/v1/user/friends/update", method = RequestMethod.POST) -// public Result update(@RequestBody UserEntity userEntity) { -// try { -// UserEntity resultUser = userFriendsService.update(userEntity); -// return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, resultUser); -// } catch (Exception e) { -// return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); -// } -// } + @ApiOperation(value = "通过ID获取单个用户好友表", notes = "通过id获取用户好友信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/user/friends/select/{id}", method = RequestMethod.GET) + public Result getEntity(@ApiParam(value = "ID主键", required = true) @PathVariable(value = "id") Integer id) { + try { + UserFriendsEntity userFriendsEntity = userFriendsService.getEntity(id); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, userFriendsEntity); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.SUCCESS_CODE_200, e.getMessage()); + } + } + + @ApiOperation(value = "进行插入新的一条用户好友信息", notes = "进行插入新的一条用户好友信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/user/friends/insert", method = RequestMethod.POST) + public Result saveEntiy(@RequestBody UserFriendsEntity userFriendsEntity) { + try { + UserFriendsEntity userFriendsEntityResult = userFriendsService.saveEntity(userFriendsEntity); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, userFriendsEntityResult); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); + } + } + + @ApiOperation(value = "通过ID删除一条用户好友信息", notes = "通过ID删除一条用户好友信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/user/friends/delete/{id}", method = RequestMethod.DELETE) + public Result delete(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) { + try { + userFriendsService.deleteEntity(id); + return ResultUtil.success(); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); + } + } + + @ApiOperation(value = "修改用户好友信息", notes = "修改用户好友信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "successful request"), + @ApiResponse(code = 500, message = "internal server error")}) + @RequestMapping(value = "/v1/user/friends/update", method = RequestMethod.POST) + public Result update(@RequestBody UserFriendsEntity userFriendsEntity) { + try { + userFriendsService.updateEntity(userFriendsEntity); + return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, userFriendsEntity); + } catch (Exception e) { + return ResultUtil.error(ResultUtil.ERROR_CODE_500, e.getMessage()); + } + } } diff --git a/src/main/java/com/library/nbt/model/view/ArticleEntityView.java b/src/main/java/com/library/nbt/model/view/ArticleEntityView.java new file mode 100644 index 0000000..068d785 --- /dev/null +++ b/src/main/java/com/library/nbt/model/view/ArticleEntityView.java @@ -0,0 +1,7 @@ +package com.library.nbt.model.view; + +import com.library.nbt.model.entity.ArticleEntity; + +public class ArticleEntityView extends ArticleEntity { + +} \ No newline at end of file diff --git a/src/main/java/com/library/nbt/model/view/ComentEntityView.java b/src/main/java/com/library/nbt/model/view/ComentEntityView.java new file mode 100644 index 0000000..03ae108 --- /dev/null +++ b/src/main/java/com/library/nbt/model/view/ComentEntityView.java @@ -0,0 +1,6 @@ +package com.library.nbt.model.view; + +import com.library.nbt.model.entity.ComentEntity; + +public class ComentEntityView extends ComentEntity { +} diff --git a/src/main/java/com/library/nbt/model/view/SortsEntityView.java b/src/main/java/com/library/nbt/model/view/SortsEntityView.java new file mode 100644 index 0000000..59f8b2d --- /dev/null +++ b/src/main/java/com/library/nbt/model/view/SortsEntityView.java @@ -0,0 +1,7 @@ +package com.library.nbt.model.view; + +import com.library.nbt.model.entity.SortsEntity; + +public class SortsEntityView extends SortsEntity { + +} \ No newline at end of file diff --git a/src/main/java/com/library/nbt/model/view/UserEntityView.java b/src/main/java/com/library/nbt/model/view/UserEntityView.java new file mode 100644 index 0000000..09a7fc0 --- /dev/null +++ b/src/main/java/com/library/nbt/model/view/UserEntityView.java @@ -0,0 +1,6 @@ +package com.library.nbt.model.view; + +import com.library.nbt.model.entity.UserEntity; + +public class UserEntityView extends UserEntity { +} diff --git a/src/main/java/com/library/nbt/model/view/UserFriendsEntityView.java b/src/main/java/com/library/nbt/model/view/UserFriendsEntityView.java new file mode 100644 index 0000000..0b241d6 --- /dev/null +++ b/src/main/java/com/library/nbt/model/view/UserFriendsEntityView.java @@ -0,0 +1,7 @@ +package com.library.nbt.model.view; + +import com.library.nbt.model.entity.UserFriendsEntity; + +public class UserFriendsEntityView extends UserFriendsEntity { + +} \ No newline at end of file diff --git a/src/main/java/com/library/nbt/model/view/ViewsEntityView.java b/src/main/java/com/library/nbt/model/view/ViewsEntityView.java new file mode 100644 index 0000000..b38afaa --- /dev/null +++ b/src/main/java/com/library/nbt/model/view/ViewsEntityView.java @@ -0,0 +1,7 @@ +package com.library.nbt.model.view; + +import com.library.nbt.model.entity.ViewsEntity; + +public class ViewsEntityView extends ViewsEntity { + +} \ No newline at end of file diff --git a/src/main/java/com/library/nbt/service/ArticleService.java b/src/main/java/com/library/nbt/service/ArticleService.java new file mode 100644 index 0000000..6ae2059 --- /dev/null +++ b/src/main/java/com/library/nbt/service/ArticleService.java @@ -0,0 +1,6 @@ +package com.library.nbt.service; + +import com.library.nbt.model.entity.ArticleEntity; + +public interface ArticleService extends BaseService { +} diff --git a/src/main/java/com/library/nbt/service/BaseService.java b/src/main/java/com/library/nbt/service/BaseService.java index b0d0e11..f1b9068 100644 --- a/src/main/java/com/library/nbt/service/BaseService.java +++ b/src/main/java/com/library/nbt/service/BaseService.java @@ -19,7 +19,7 @@ public interface BaseService { * * @param id the id */ - public void deleteEntity(int id) throws Exception; + public void deleteEntity(int id); /** * 批量删除 entities. @@ -34,7 +34,7 @@ public interface BaseService { * * @param entity the entity */ - public T updateEntity(T entity) throws Exception; + public void updateEntity(T entity) throws Exception; /** * 获取 entity. diff --git a/src/main/java/com/library/nbt/service/UserService.java b/src/main/java/com/library/nbt/service/UserService.java index 141fca5..aac1d30 100644 --- a/src/main/java/com/library/nbt/service/UserService.java +++ b/src/main/java/com/library/nbt/service/UserService.java @@ -1,6 +1,7 @@ package com.library.nbt.service; import com.library.nbt.model.entity.UserEntity; +import com.library.nbt.model.view.UserEntityView; -public interface UserService extends BaseService{ +public interface UserService extends BaseService{ } diff --git a/src/main/java/com/library/nbt/service/impl/ArticleServiceImpl.java b/src/main/java/com/library/nbt/service/impl/ArticleServiceImpl.java new file mode 100644 index 0000000..233b926 --- /dev/null +++ b/src/main/java/com/library/nbt/service/impl/ArticleServiceImpl.java @@ -0,0 +1,82 @@ +package com.library.nbt.service.impl; + +import com.library.nbt.dao.ArticleEntityMapper; +import com.library.nbt.model.entity.ArticleEntity; +import com.library.nbt.service.ArticleService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.stereotype.Service; +import org.springframework.util.ObjectUtils; + +import java.util.List; + +@Service +public class ArticleServiceImpl implements ArticleService { + private static final Logger LOG = LoggerFactory.getLogger(ArticleEntity.class); + + @Autowired + private ArticleEntityMapper articleEntityMapper; + @Override + public ArticleEntity saveEntity(ArticleEntity articleEntity) throws Exception { + //发布文章需要后台管理员进行审核,审核通过将Enabled设置为1 + articleEntity.setCreateTime(System.currentTimeMillis()); + articleEntity.setUpdateTime(System.currentTimeMillis()); + articleEntity.setEnabled(0); + articleEntity.setViews(0); + articleEntity.setCommentCount(0); + Integer result = articleEntityMapper.insert(articleEntity); + LOG.info("article表插入了" + result + "条数据"); + if (result == 0){ + LOG.warn("article表插入数据失败,请联系管理员"); + throw new Exception("插入数据失败,请联系管理员"); + } + return articleEntity; + } + + @Override + public void deleteEntity(int id){ + articleEntityMapper.deleteByPrimaryKey(id); + } + + @Override + public void deleteEntities(String ids) { + + } + + @Override + public void updateEntity(ArticleEntity articleEntity) throws Exception { + Integer result = articleEntityMapper.updateByPrimaryKey(articleEntity); + LOG.info("article表更新了" + result + "条数据"); + if (result == 0){ + LOG.warn("article表更新数据失败,请联系管理员"); + throw new Exception("更新数据失败,请联系管理员"); + } + } + + @Override + public ArticleEntity getEntity(int id) throws Exception { + ArticleEntity articleEntity = articleEntityMapper.selectByPrimaryKey(id); + if (ObjectUtils.isEmpty(articleEntity)){ + LOG.warn("article表查询数据失败,查询的数据ID是:" + id); + throw new Exception("查询数据失败,请联系管理员"); + } + return articleEntity; + } + + @Override + public Page getEntitiesByParms(ArticleEntity articleEntity, int currentPage, int pageSize) { + return null; + } + + @Override + public long getEntitiesCount() { + return 0; + } + + @Override + public List findAll() { + return null; + } +} diff --git a/src/main/java/com/library/nbt/service/impl/UserFriendsServiceImpl.java b/src/main/java/com/library/nbt/service/impl/UserFriendsServiceImpl.java index 3135ca2..b26e519 100644 --- a/src/main/java/com/library/nbt/service/impl/UserFriendsServiceImpl.java +++ b/src/main/java/com/library/nbt/service/impl/UserFriendsServiceImpl.java @@ -35,18 +35,12 @@ public class UserFriendsServiceImpl implements UserFriendsService { if (result == 0){ throw new Exception("新增用户失败,请联系管理员"); } - UserFriendsEntity resultUserFirendsEntity = userFriendsEntityMapper.selectByPrimaryKey(userFriendsEntity.getId()); - - return resultUserFirendsEntity; + return userFriendsEntity; } @Override - public void deleteEntity(int id) throws Exception { - Integer result = userFriendsEntityMapper.deleteByPrimaryKey(id); - if (result == 0){ - LOG.info("找不到user_iD为:" + id + "的数据记录"); - throw new Exception("删除失败,无法正常删除,请联系管理员"); - } + public void deleteEntity(int id){ + userFriendsEntityMapper.deleteByPrimaryKey(id); } @Override @@ -55,7 +49,7 @@ public class UserFriendsServiceImpl implements UserFriendsService { } @Override - public UserFriendsEntity updateEntity(UserFriendsEntity userFriendsEntity) throws Exception { + public void updateEntity(UserFriendsEntity userFriendsEntity) throws Exception { if (userFriendsEntity.getCreateTime() == 0){ userFriendsEntity.setCreateTime(System.currentTimeMillis()); } @@ -63,13 +57,10 @@ public class UserFriendsServiceImpl implements UserFriendsService { userFriendsEntity.setUpdateTime(System.currentTimeMillis()); } Integer result = userFriendsEntityMapper.updateByPrimaryKey(userFriendsEntity); - LOG.info("更新的数据条数为:" + result); + LOG.info("userFriends更新的数据条数为:" + result); if (result == 0){ - LOG.info("找不到user_iD为:" + userFriendsEntity.getId() + "的数据记录"); throw new Exception("更新失败,请联系管理员"); } - UserFriendsEntity resultUser = userFriendsEntityMapper.selectByPrimaryKey(userFriendsEntity.getId()); - return resultUser; } @Override diff --git a/src/main/java/com/library/nbt/service/impl/UserServiceImpl.java b/src/main/java/com/library/nbt/service/impl/UserServiceImpl.java index 0c2e399..6e267e6 100644 --- a/src/main/java/com/library/nbt/service/impl/UserServiceImpl.java +++ b/src/main/java/com/library/nbt/service/impl/UserServiceImpl.java @@ -2,14 +2,17 @@ package com.library.nbt.service.impl; import com.library.nbt.dao.UserEntityMapper; import com.library.nbt.model.entity.UserEntity; +import com.library.nbt.model.view.UserEntityView; import com.library.nbt.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; +import java.util.ArrayList; import java.util.List; @Service @@ -21,15 +24,10 @@ public class UserServiceImpl implements UserService { private UserEntityMapper userEntityMapper; @Override - public UserEntity saveEntity(UserEntity userEntity) throws Exception { - //如果传过来的参数带有ID的话,则查看ID是否已经存在 -// if (!ObjectUtils.isEmpty(userEntity.getId())){ -// UserEntity userEntityNew = userEntityMapper.selectByPrimaryKey(userEntity.getId()); -// if (!ObjectUtils.isEmpty(userEntityNew)){ -// LOG.error("用户ID为:" + userEntity.getId() + "已存在,无法新增"); -// throw new Exception("该用户已存在,无法新增"); -// } -// } + public UserEntityView saveEntity(UserEntityView userEntityView) throws Exception { + UserEntity userEntity = new UserEntity(); + BeanUtils.copyProperties(userEntity,userEntityView); + userEntity.setCreateTime(System.currentTimeMillis()); userEntity.setUpdateTime(System.currentTimeMillis()); userEntity.setEnabled(1); @@ -37,23 +35,19 @@ public class UserServiceImpl implements UserService { if (ObjectUtils.isEmpty(userEntity.getNickName())){ userEntity.setNickName(userEntity.getUsername()); } + Integer result = userEntityMapper.insert(userEntity); LOG.info("在t_user表中插入了:" + result + "条信息"); if (result == 0){ throw new Exception("新增用户失败,请联系管理员"); } - UserEntity resultUserEntity = userEntityMapper.selectByPrimaryKey(userEntity.getId()); - - return resultUserEntity; + BeanUtils.copyProperties(userEntityView,userEntity); + return userEntityView; } @Override - public void deleteEntity(int id) throws Exception { - Integer result = userEntityMapper.deleteByPrimaryKey(id); - if (result == 0){ - LOG.info("找不到user_iD为:" + id + "的数据记录"); - throw new Exception("删除失败,无法正常删除,请联系管理员"); - } + public void deleteEntity(int id){ + userEntityMapper.deleteByPrimaryKey(id); } @Override @@ -62,35 +56,37 @@ public class UserServiceImpl implements UserService { } @Override - public UserEntity updateEntity(UserEntity userEntity) throws Exception { + public void updateEntity(UserEntityView userEntityView) throws Exception { + UserEntity userEntity = new UserEntity(); + BeanUtils.copyProperties(userEntity,userEntityView); + if (userEntity.getCreateTime() == 0){ userEntity.setCreateTime(System.currentTimeMillis()); } - if (userEntity.getUpdateTime() == 0){ - userEntity.setUpdateTime(System.currentTimeMillis()); - } - Integer result = userEntityMapper.updateByPrimaryKey(userEntity); - LOG.info("更新的数据条数为:" + result); - if (result == 0){ + userEntity.setUpdateTime(System.currentTimeMillis()); + Integer userResult = userEntityMapper.updateByPrimaryKey(userEntity); + if (userResult == 0){ LOG.info("找不到user_iD为:" + userEntity.getId() + "的数据记录"); throw new Exception("更新失败,请联系管理员"); } - UserEntity resultUser = userEntityMapper.selectByPrimaryKey(userEntity.getId()); - return resultUser; + LOG.info("user表更新的数据条数为:" + userResult); + BeanUtils.copyProperties(userEntityView,userEntity); } @Override - public UserEntity getEntity(int id) throws Exception { + public UserEntityView getEntity(int id) throws Exception { UserEntity userEntity = userEntityMapper.selectByPrimaryKey(id); if (ObjectUtils.isEmpty(userEntity)){ LOG.info("找不到user_iD为:" + id + "的数据记录"); throw new Exception("查无此用户"); } - return userEntity; + UserEntityView userEntityView = new UserEntityView(); + BeanUtils.copyProperties(userEntityView,userEntity); + return userEntityView; } @Override - public Page getEntitiesByParms(UserEntity userEntity, int currentPage, int pageSize) { + public Page getEntitiesByParms(UserEntityView userEntityView, int currentPage, int pageSize) { return null; } @@ -100,8 +96,11 @@ public class UserServiceImpl implements UserService { } @Override - public List findAll() { + public List findAll() { List userEntities = userEntityMapper.findAll(); - return userEntities; + List userEntityView = new ArrayList<>(); + + BeanUtils.copyProperties(userEntityView,userEntities); + return userEntityView; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 18ca375..cad3a0f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ -server.port=9036 +server.port=8080 ##jdbc mysql 8.1ϰ汾 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -- Gitee