Springboot项目控制层注释
Springboot主流的 ----------------------- 简略写法
package com.dx.wlmq.controller;import com.dx.wlmq.domain.Address;
import com.dx.wlmq.service.AddresssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController // 相当于 @Controller+@ResponseBody
@CrossOrigin(origins = "*") // 取消跨域
@RequestMapping("/address") // 访问路径
public class AddresssController {@Autowired // 自动装配AddresssService addresssService;/*** 查询方法* *///相当于 @RequestMapping(value = "/list", method = RequestMethod.GET,produces = "text/html;charset=UTF-8" )@GetMapping(value = "/list")public Object index11() {return addresssService.Addresslist();}// @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@PostMapping("/add")public Object index12(@RequestBody Address address) {return addresssService.Addressadd(address);}// @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@DeleteMapping("/delete")public Object index13(Integer id) {return addresssService.Addressdelete(id);}// @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@PutMapping(value = "/update")public Object index14(@RequestBody Address address) {return addresssService.Addressupdate(address);}}
原Mybits主流写法(案例)
package com.dx.wlmq.controller;import com.alibaba.fastjson.JSON;
import com.dx.wlmq.domain.Role;
import com.dx.wlmq.service.RolesService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;@Controller
@RequestMapping("/roles")
public class RolesController {@Resource(name = "rolesService")RolesService rolesService;@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index11(HttpServletResponse response) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Rolelist());}@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index12(HttpServletResponse response,@RequestBody Role role) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Roleadd(role));}@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index13(HttpServletResponse response, Integer id) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Roledelete(id));}@RequestMapping(value = "/update", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index14(HttpServletResponse response,@RequestBody Role role) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Roleupdate(role));}}
Springboot主流的 ----------------------- 简略写法(优点:写法更简略,更直接)
简单写法的笔记:
《《《《《《《《《------ springboot@注释解析最简写法 ------》》》》》》》》》
在控制层开头:
书写@RestController // 相当于 @Controller+@ResponseBody
添加取消跨域注释 @CrossOrigin(origins = "*")
书写类,然后在第一行添加 @Autowired //作用是自动装配 ,装配了业务层的调用实例
private SysEmpService sysEmpService;
然后增删改查都有不同的Mapping注释,当这样写注解的时候,return的值直接为JSON格式,详细如下:
/**
* 查询方法注解写法
* */
@GetMapping(value = "/list") //相当于 @RequestMapping(value = "/list", method = RequestMethod.GET,produces = "text/html;charset=UTF-8" )
public Object index11() {
return addresssService.Addresslist();
}
/**
* 增加方法注解写法
* */
@PostMapping("/add") // 相当于 @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
public Object index12(@RequestBody Address address) {
return addresssService.Addressadd(address);
}
/**
* 删除方法注解写法
* */
@DeleteMapping("/delete") //相当于 @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
public Object index13(Integer id) {
return addresssService.Addressdelete(id);
}
/**
* 修改方法注解写法
* */
@PutMapping(value = "/update") //相当于 @RequestMapping(value="/update", method=RequestMethod.POST, produces = "text/html;charset=UTF-8")
public Object index14(@RequestBody Address address) {
return addresssService.Addressupdate(address);
}
在服务层,将调用Dao层的时用装配注入的注解@Autowired,但服务层实现类里的@Service("rolesService")不能省略
在Dao层的mapper里面添加注解:@Mapper