Browse Source

作者crud

master
nili 3 years ago
parent
commit
6e68f1cf13
  1. 19
      luigi-api/src/main/java/com/bzgame/server/luigi/api/rest/AuthorController.java
  2. 5
      luigi-api/src/main/java/com/bzgame/server/luigi/api/rest/PoemController.java
  3. 4
      luigi-dao/src/main/java/com/bzgame/server/luigi/dao/mapper/manual/ManualMapper.java
  4. 59
      luigi-service/src/main/java/com/bzgame/server/luigi/service/bo/AuthorService.java
  5. 6
      luigi-service/src/main/java/com/bzgame/server/luigi/service/bo/PoemService.java
  6. 2
      luigi-service/src/main/java/com/bzgame/server/luigi/service/bo/request/PaginationQuery.java

19
luigi-api/src/main/java/com/bzgame/server/luigi/api/rest/AuthorController.java

@ -4,13 +4,12 @@ import com.bczgame.server.common.vo.Response;
import com.bzgame.server.luigi.api.aop.ApiFlag; import com.bzgame.server.luigi.api.aop.ApiFlag;
import com.bzgame.server.luigi.dao.domain.Author; import com.bzgame.server.luigi.dao.domain.Author;
import com.bzgame.server.luigi.service.bo.AuthorService; import com.bzgame.server.luigi.service.bo.AuthorService;
import com.bzgame.server.luigi.service.bo.request.PaginationQuery;
import com.bzgame.server.luigi.service.bo.response.CommonList; import com.bzgame.server.luigi.service.bo.response.CommonList;
import com.bzgame.server.luigi.service.bo.response.PoemBo;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
@ -42,4 +41,16 @@ public class AuthorController {
public Response<Author> getAuthorById(@RequestParam Integer id) { public Response<Author> getAuthorById(@RequestParam Integer id) {
return new Response<>(authorService.getById(id), "ok", Response.CODE_SUCCESS); return new Response<>(authorService.getById(id), "ok", Response.CODE_SUCCESS);
} }
@PostMapping("/list")
@ApiFlag
public Response<CommonList<Author>> authorList(@RequestBody PaginationQuery query) {
return new Response<>(authorService.query(query), "ok", Response.CODE_SUCCESS);
}
@PostMapping("/save")
@ApiFlag
public Response<Integer> saveAuthor(@RequestBody Author author) {
return new Response<>(authorService.save(author), "ok", 1);
}
} }

5
luigi-api/src/main/java/com/bzgame/server/luigi/api/rest/PoemController.java

@ -3,11 +3,10 @@ package com.bzgame.server.luigi.api.rest;
import com.bczgame.server.common.vo.Response; import com.bczgame.server.common.vo.Response;
import com.bzgame.server.luigi.api.aop.ApiFlag; import com.bzgame.server.luigi.api.aop.ApiFlag;
import com.bzgame.server.luigi.service.bo.PoemService; import com.bzgame.server.luigi.service.bo.PoemService;
import com.bzgame.server.luigi.service.bo.request.PoemQuery; import com.bzgame.server.luigi.service.bo.request.PaginationQuery;
import com.bzgame.server.luigi.service.bo.response.CommonList; import com.bzgame.server.luigi.service.bo.response.CommonList;
import com.bzgame.server.luigi.service.bo.response.PoemBo; import com.bzgame.server.luigi.service.bo.response.PoemBo;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -24,7 +23,7 @@ public class PoemController {
@PostMapping("/list") @PostMapping("/list")
@ApiFlag @ApiFlag
public Response<CommonList<PoemBo>> poemList(@RequestBody PoemQuery query) { public Response<CommonList<PoemBo>> poemList(@RequestBody PaginationQuery query) {
return new Response<>(poemService.query(query), "ok", Response.CODE_SUCCESS); return new Response<>(poemService.query(query), "ok", Response.CODE_SUCCESS);
} }

4
luigi-dao/src/main/java/com/bzgame/server/luigi/dao/mapper/manual/ManualMapper.java

@ -1,6 +1,7 @@
package com.bzgame.server.luigi.dao.mapper.manual; package com.bzgame.server.luigi.dao.mapper.manual;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Update;
public interface ManualMapper { public interface ManualMapper {
@ -9,4 +10,7 @@ public interface ManualMapper {
@Update("update author set poem_cnt = poem_cnt + 1 where id = #{id}") @Update("update author set poem_cnt = poem_cnt + 1 where id = #{id}")
void incrAuthorPoemCnt(@Param("id") Integer authorId); void incrAuthorPoemCnt(@Param("id") Integer authorId);
@Select("select count(*) from poem where author_id = #{id}")
int getPoemCntByAuthor(@Param("id") Integer authorId);
} }

59
luigi-service/src/main/java/com/bzgame/server/luigi/service/bo/AuthorService.java

@ -1,10 +1,18 @@
package com.bzgame.server.luigi.service.bo; package com.bzgame.server.luigi.service.bo;
import com.bczgame.server.common.exception.BizException;
import com.bczgame.server.common.exception.ExceptionCode;
import com.bzgame.server.luigi.dao.domain.Author; import com.bzgame.server.luigi.dao.domain.Author;
import com.bzgame.server.luigi.dao.domain.AuthorExample; import com.bzgame.server.luigi.dao.domain.AuthorExample;
import com.bzgame.server.luigi.dao.domain.Poem;
import com.bzgame.server.luigi.dao.domain.PoemExample;
import com.bzgame.server.luigi.dao.mapper.AuthorMapper; import com.bzgame.server.luigi.dao.mapper.AuthorMapper;
import com.bzgame.server.luigi.dao.mapper.manual.ManualMapper;
import com.bzgame.server.luigi.service.bo.request.PaginationQuery;
import com.bzgame.server.luigi.service.bo.response.CommonList; import com.bzgame.server.luigi.service.bo.response.CommonList;
import com.bzgame.server.luigi.service.bo.response.PoemBo;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
@ -19,6 +27,34 @@ public class AuthorService {
@Resource @Resource
private AuthorMapper authorMapper; private AuthorMapper authorMapper;
@Resource
private ManualMapper manualMapper;
public CommonList<Author> query(PaginationQuery query) {
AuthorExample example = new AuthorExample();
AuthorExample.Criteria criteria = example.createCriteria();
setQuery(query, criteria);
example.setOffset((query.getCurrent() - 1) * query.getPageSize());
example.setLimit(query.getPageSize());
List<Author> authors = authorMapper.selectByExampleWithBLOBs(example);
long total = authorMapper.countByExample(example);
CommonList<Author> res = new CommonList<>();
res.setTotal((int) total);
res.setList(authors);
return res;
}
public Integer save(Author a) {
if (a.getId() != null) {
int poemCnt = manualMapper.getPoemCntByAuthor(a.getId());
a.setPoemCnt(poemCnt);
authorMapper.updateByPrimaryKeySelective(a);
} else {
authorMapper.insertSelective(a);
}
return a.getId();
}
public Map<Integer, Author> getByIds(List<Integer> ids) { public Map<Integer, Author> getByIds(List<Integer> ids) {
AuthorExample example = new AuthorExample(); AuthorExample example = new AuthorExample();
example.createCriteria().andIdIn(ids); example.createCriteria().andIdIn(ids);
@ -39,4 +75,27 @@ public class AuthorService {
res.setList(authors); res.setList(authors);
return res; return res;
} }
private void setQuery(PaginationQuery query, AuthorExample.Criteria criteria) {
criteria.andStatusGreaterThanOrEqualTo(0);
if (CollectionUtils.isEmpty(query.getQuery())) {
return;
}
query.getQuery().forEach(x -> {
switch (x.getKey()) {
case "status":
criteria.andStatusEqualTo(Integer.valueOf(x.getVal()));
break;
case "id":
criteria.andIdEqualTo(Integer.valueOf(x.getVal()));
break;
case "name":
criteria.andNameLike(x.getVal() + "%");
break;
default:
throw new BizException(ExceptionCode.PARAM_ILLEGAL_WARN, "参数非法");
}
});
}
} }

6
luigi-service/src/main/java/com/bzgame/server/luigi/service/bo/PoemService.java

@ -7,7 +7,7 @@ import com.bzgame.server.luigi.dao.domain.Poem;
import com.bzgame.server.luigi.dao.domain.PoemExample; import com.bzgame.server.luigi.dao.domain.PoemExample;
import com.bzgame.server.luigi.dao.mapper.PoemMapper; import com.bzgame.server.luigi.dao.mapper.PoemMapper;
import com.bzgame.server.luigi.dao.mapper.manual.ManualMapper; import com.bzgame.server.luigi.dao.mapper.manual.ManualMapper;
import com.bzgame.server.luigi.service.bo.request.PoemQuery; import com.bzgame.server.luigi.service.bo.request.PaginationQuery;
import com.bzgame.server.luigi.service.bo.response.CommonList; import com.bzgame.server.luigi.service.bo.response.CommonList;
import com.bzgame.server.luigi.service.bo.response.PoemBo; import com.bzgame.server.luigi.service.bo.response.PoemBo;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -35,7 +35,7 @@ public class PoemService {
@Resource @Resource
private ManualMapper manualMapper; private ManualMapper manualMapper;
public CommonList<PoemBo> query(PoemQuery query) { public CommonList<PoemBo> query(PaginationQuery query) {
PoemExample example = new PoemExample(); PoemExample example = new PoemExample();
PoemExample.Criteria criteria = example.createCriteria(); PoemExample.Criteria criteria = example.createCriteria();
setQuery(query, criteria); setQuery(query, criteria);
@ -98,7 +98,7 @@ public class PoemService {
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
private void setQuery(PoemQuery query, PoemExample.Criteria criteria) { private void setQuery(PaginationQuery query, PoemExample.Criteria criteria) {
criteria.andStatusGreaterThanOrEqualTo(0); criteria.andStatusGreaterThanOrEqualTo(0);
if (CollectionUtils.isEmpty(query.getQuery())) { if (CollectionUtils.isEmpty(query.getQuery())) {

2
luigi-service/src/main/java/com/bzgame/server/luigi/service/bo/request/PoemQuery.java → luigi-service/src/main/java/com/bzgame/server/luigi/service/bo/request/PaginationQuery.java

@ -5,7 +5,7 @@ import java.util.List;
/** /**
* @author nidaren * @author nidaren
*/ */
public class PoemQuery { public class PaginationQuery {
private List<QueryParam> query; private List<QueryParam> query;
private int pageSize; private int pageSize;
private int current; private int current;
Loading…
Cancel
Save