5. Springbootl快速回顾(REST风格)

保持一切对rest风格的疑问,本文将使用对比的方式,直观解释。
本项目练习使REST风格,对url上携带的参数获取

1 搭建基础案例环境

前提是数据库已经自己创建了表,在配置文件中连接数据库

1.1 创建实体类Stu

public class stu {
    String name;
    Integer age;
    String sex;
    Integer id;

    public stu() {
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return this.age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String toString() {
        return "stu{name='" + this.name + '\'' + ", age=" + this.age + ", sex='" + this.sex + '\'' + ", id=" + this.id + '}';
    }
}

1.2 创建dao层接口

//根据stuid查询学生全部信息
@Mapper
public interface StuDao {
    stu selectById(@Param("stuId") Integer id);
}

1.3 创建service层接口和实现

public interface StuService {
    stu queryStudent(Integer id);
}

@Service
public class StuServiceImpl implements StuService {
    @Resource
    private StuDao stuDao;

    public StuServiceImpl() {
    }

    public stu queryStudent(Integer id) {
        stu stu = this.stuDao.selectById(id);
        return stu;
    }
}

1.5 创建StuMapper.xml文件

<!--namespace是此mapper对应的dao接口-->
<mapper namespace="com.zjh.dao.StuDao">

    <!--resultType是此mapper对应的实体类-->
    <select id="selectById" resultType="com.zjh.model.stu">
        select
            *
        from
            stu
        where
            id=#{stuId};
    </select>

</mapper>

1.4 创建controller类

@Controller
public class stuController {
    @Resource
    private StuService stuService;

    public stuController() {
    }

    @RequestMapping({"/mapper"})
    @ResponseBody
    public String selectById(Integer id) {
        stu stu = this.stuService.queryStudent(id);
        return stu.toString();
    }
}

结束,测试运行即可!

2 普通风格


@RestController
//@RestController :替代注解 @Controller 和 @ResponseBody
public class stuController extends HttpServlet {
    @Resource
    private StuService stuService;

    /**
     * 传统的方法:获取url中传入的参数
     */
    @RequestMapping("/mapper")
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //获取url的传入的信息 :http://localhost:8080/mapper?name=阿伟&age=10&sex=男
        String name = request.getParameter("name");
        Integer age = Integer.valueOf(request.getParameter("age"));
        String sex = request.getParameter("sex");

        //打印出:阿伟 10 男
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(name);
        out.println(age);
        out.println(sex);
        out.flush();
        out.close();
    }

3 使用rest风格

/** 一、测试get
     * 我们发现根据上面的方式是可以获取在url上传入的参数,但能不能利用url查询呢,
     * 比如输入 http://localhost:8080/mapper?id=2后,直接返回 id=2的所有信息(名字,性别,年龄)
     *
     * 当然我们希望换一个风格————>rest风格表示url :http://localhost:8080/mapper/2
     * 逻辑是这样的:先获取url,找到GetMapping的{},传给value,再传给id
     */
	@RequestMapping(value = "/mapper/{stu_id}", method = RequestMethod.GET)
    //注解@PathVariable说明
    //此注解可以获取rest风格的url传入的参数,value可省略
    public String selectById (@PathVariable(value = "stu_id") Integer id){

        //输出:打印的内容id:1
        return "打印的内容id:"+id;
    }

    /**
     * 二、测试post
     * 需要创建一个html页面,写一个表单实现post提交
     */
     @RequestMapping(value = "/mapper/{stu_id}/{stu_name}", method = RequestMethod.POST)
    //注解@PathVariable说明
    //此注解可以获取rest风格的url传入的参数,value可省略
    public String selectById (@PathVariable("stu_id") Integer id,
                              @PathVariable("stu_name") String name){

        //输出:打印的内容id:1
        return "打印的内容id:"+id+"name: "+name;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值