发表于: 2020-06-20 19:47:40
1 1817
今天完成的事情:
编写了module和StudioDetails的controller,实现模块的增删改查操作
/**
* Created with IntelliJ IDEA.
*
* @author: WangPeng
* @date: 2020/06/20/3:04 下午
* @description:
*/
@Controller
@RequestMapping("/module")
public class ModuleController {
private static final Logger logger = LogManager.getLogger(ModuleController.class);
@Autowired(required = false)
private ModuleService moduleService;
@RequestMapping(value = "/show", method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> selectModule(Module module){
logger.info("select Module is :" + module);
Map<String,Object> resultMap = new HashMap<>();
try {
List<Module> list = moduleService.findAllService();
resultMap.put("code",200);
resultMap.put("msg","查询成功");
resultMap.put("data",list);
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","查询失败");
e.printStackTrace();
logger.error("select Module error");
}
return resultMap;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> addModule(Module module){
logger.info("add Module is :" + module);
Map<String,Object> resultMap = new HashMap<>();
try {
long createAt = DateUtil.timestamp();
module.setCreateAt(createAt);
moduleService.insertSelectiveService(module);
resultMap.put("code",200);
resultMap.put("msg","添加成功");
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","添加失败");
e.printStackTrace();
logger.error("add Module error");
}
return resultMap;
}
@RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE)
@ResponseBody
public Map<String,Object> delModule(@PathVariable Long id){
logger.info("del Module's id is :" + id);
Map<String,Object> resultMap = new HashMap<>();
Module module=new Module();
module.setId(id);
try {
if(moduleService.findAllService().isEmpty()){
resultMap.put("code", 404);
resultMap.put("msg", "该module不存在!");
}else {
moduleService.deleteByPrimaryKeyService(id);
resultMap.put("code", 200);
resultMap.put("msg", "删除成功");
}
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","删除失败");
e.printStackTrace();
logger.error("del Module error");
}
return resultMap;
}
@RequestMapping(value = "/mod/{id}", method = RequestMethod.PUT)
@ResponseBody
public Map<String,Object> modModule(@PathVariable Long id,Module module){
logger.info("mod Module's id is :" + id);
Map<String,Object> resultMap = new HashMap<>();
Module module1=new Module();
module1.setId(id);
try {
if(moduleService.findAllService().isEmpty()){
resultMap.put("code", 404);
resultMap.put("msg", "该module不存在!");
}else {
moduleService.updateByPrimaryKeySelectiveService(module);
resultMap.put("code", 200);
resultMap.put("msg", "修改成功");
}
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","修改失败");
e.printStackTrace();
logger.error("mod Module error");
}
return resultMap;
}}
@Controller
@RequestMapping("/studio")
public class StudioDetailsController {
private static final Logger logger = LogManager.getLogger(ModuleController.class);
@Autowired(required = false)
private StudioDetailsService studioDetailsService;
@RequestMapping(value = "/show", method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> selectStudioDetails(StudioDetails studioDetails){
logger.info("select StudioDetails is :" + studioDetails);
Map<String,Object> resultMap = new HashMap<>();
try {
List<StudioDetails> list = studioDetailsService.findAllService();
resultMap.put("code",200);
resultMap.put("msg","查询成功");
resultMap.put("data",list);
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","查询失败");
e.printStackTrace();
logger.error("select StudioDetails error");
}
return resultMap;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> addStudioDetails(StudioDetails studioDetails){
logger.info("add StudioDetails is :" + studioDetails);
Map<String,Object> resultMap = new HashMap<>();
try {
long createAt = DateUtil.timestamp();
studioDetails.setCreateAt(createAt);
studioDetailsService.insertSelectiveService(studioDetails);
resultMap.put("code",200);
resultMap.put("msg","添加成功");
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","添加失败");
e.printStackTrace();
logger.error("add StudioDetails error");
}
return resultMap;
}
@RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE)
@ResponseBody
public Map<String,Object> delStudioDetails(@PathVariable Long id,StudioDetails studioDetails){
logger.info("del StudioDetails's id is :" + id);
Map<String,Object> resultMap = new HashMap<>();
try {
if(studioDetailsService.selectByPrimaryKeyService(id)==null){
resultMap.put("code", 404);
resultMap.put("msg", "该StudioDetails不存在!");
}else {
studioDetailsService.deleteByPrimaryKeyService(id);
resultMap.put("code", 200);
resultMap.put("msg", "删除成功");
}
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","删除失败");
e.printStackTrace();
logger.error("del StudioDetails error");
}
return resultMap;
}
@RequestMapping(value = "/mod/{id}", method = RequestMethod.PUT)
@ResponseBody
public Map<String,Object> modStudioDetails(@PathVariable Long id, StudioDetails studioDetails){
logger.info("mod StudioDetails's id is :" + id);
Map<String,Object> resultMap = new HashMap<>();
try {
if(studioDetailsService.selectByPrimaryKeyService(id)==null){
resultMap.put("code", 404);
resultMap.put("msg", "该StudioDetails不存在!");
}else {
studioDetailsService.updateByPrimaryKeySelectiveService(studioDetails);
resultMap.put("code", 200);
resultMap.put("msg", "修改成功");
}
}catch (Exception e){
resultMap.put("code",404);
resultMap.put("msg","修改失败");
e.printStackTrace();
logger.error("mod StudioDetails error");
}
return resultMap;
}
}
明天计划的事情:
完成其他controller编写,实现基础的增删改查功能
遇到的问题:
对角色表和角色模块关联表的操作不太理解,问过师兄后基本了解了,明天尝试编写代码实现相关功能。
收获:
1.对postman的使用比之前熟练了。
2.@PathVariable:接收请求路径中占位符的值
3.@ResponseBody的作用是将java对象转为json格式的数据
将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
在使用 @RequestMapping后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象。
评论