IDEA Springboot 整合Mybatis 完整教程系列 05

05 编写controller层


经过前面的工作,我们终于可以看到成果了,下面通过controller调用Service层的类,来执行我们想要的操作吧


如下,写了两个方法,用户登录和注册,将结果返回到页面,成功返回“success”,失败返回“failure”


package com.example.demo.controller;


import com.example.demo.entity.UserLogin;
import com.example.demo.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
//@RequestMapping("/easy")

public class UserController {

    @Autowired
    private UserLoginService userLoginService;


    //通过Javabean对象传递参数,参数大小写保持和类属性一致
    // 保持大小写一致,如若不一致,需要给@RequstParam的属性赋值

    @PostMapping("/user/login")
    //post 参数 :userAccount,userPassoword
    private String login(UserLogin userLogin){


        //成功:success
        //失败:failure
        return userLoginService.login(userLogin.getUserAccount(),userLogin.getUserPassword());

    }

    @PostMapping("/user/register")
    private String register(UserLogin userLogin){

        //成功:success
        //失败:failure
        return userLoginService.register(userLogin.getUserAccount(),userLogin.getUserPassword());



    }












}

    

最终结果,利用postman,进行测试:

(1)登录:


(2)注册:





猜你喜欢

转载自blog.csdn.net/qq_31815507/article/details/80962275