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")
    @ResponseBody
    public 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")
    @ResponseBody
    public 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")
    @ResponseBody
    public 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")
    @ResponseBody
    public 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值