How to use the interface class mapper and Service in the controller at the same time in SpringMVC?

When doing login-related functions, I found that the related functions conflict. When using @Autowired to automatically load, it is not possible to reference private UserMapper userMapper and private UserService userService at the same time. Neither can find a solution.

In a few days of study and exploration, I found two ways:

The first is to use the @Resource annotation to quote mapper content; the other is to not use UserService and integrate all its functions into the control class. This method directly solves the problem that cannot be used at the same time at the root.

Law one

Through the guidance of the following link: Realize user login function based on springmvc+mysql+ mybatis %257B%2522request%255Fid%2522%253A%2522165512297316782395343115%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%25 22%257D&request_id=165512297316782395343115&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~ All ~ fIRST_RANK_ECPM_V1 ~ RANK_V31_ECPM-6-79547655-NULL.142%5EV14%5eControl, 157%5EV14%5EV_3 & UTM_TERM = Springmvc%80%E4E%E4%E4%E4%E4%B4%B4%B4%E4%B4%E4%B4%B4% 9%88%E5%9C%A8Service%E9%87%87% 8C%E5%AE%9E%E7%8E%B0%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE%E5%88%B0%E6%95%B0% E6%8D%AE%E5%BA%93%E5%92%8C%E9%AA%8C%E8%AF%81%E7%9A%84%E5%8A%9F%E8%83%BD&spm=1018.2226. 3001.4187

 I finally realized something:

I wanted to control the registration page before, and I also wanted to insert the user's registration information into the database at the same time, but it didn't work. The expected code:

@Autowired
    private UserService userService;
    private UserMapper userMapper;

//控制注册
    @RequestMapping("pre_reg")
    public String preReg(){
        return "user/reg";
    }
    @RequestMapping("add_user")
    public String reg(String uid, String uname, String address,
                      String password, String password1, Model model)
    {
        //不允许有空的信息:
        if(uid.equals("") || uname.equals("") || address.equals("")
                || password.equals("") || password1.equals("")){
            model.addAttribute("reg1_error","信息有误,请完整填入信息!");
            return "redirect:pre_reg";
        }
        //验证密码是否正确:
        if(!password.equals(password1)){
            model.addAttribute("reg_error","两次密码不一致,请重新输入!");
            return "redirect:pre_reg";
        }
        model.addAttribute("uid",uid);
        return "redirect:pre_login";
    }

    @RequestMapping("add_user")
    public String addUser(User user){
        int count = userMapper.addUser(user);
        return "redirect:pre_login";
    }
//注意这只是部分代码!!并不是全部的!!

But when running, the idea reported an error, saying that two beans were referenced and could not be recognized. Because I have used UserService for login verification below, UserService cannot be removed.

//这是控制类中剩下的部分:
//控制登录
    @RequestMapping("pre_login")
    public String preLogin(){
        return "user/login";
    }
    @RequestMapping("login")
    public String login(User user, HttpSession session, Model model)
    {
        if (userService.isValidatedUser(user))
        {
            session.setAttribute("uid", user.getUid());
            request.setAttribute("user",user);
            return "redirect:success";
        }
        model.addAttribute("login_error", "名称或密码错误!");
        return "redirect: pre_login";
    }

This is modified to work with:

//引用mapper文件:
    @Resource
    private UserMapper userMapper;
    //主动装载服务类
    @Autowired
    private UserService userService;

    //控制注册
    @RequestMapping("pre_reg")
    public String preReg(){
        return "user/reg";
    }
    @RequestMapping("add_user")
    public String reg(User user, String uid, String uname, String address,
                      String password, String password1, Model model)
    {
        //不允许有空的信息:
        if(uid.equals("") || uname.equals("") || address.equals("")
                || password.equals("") || password1.equals("")){
            model.addAttribute("reg1_error","信息有误,请完整填入信息!");
            return "redirect:pre_reg";
        }
        //验证密码是否正确:
        if(!password.equals(password1)){
            model.addAttribute("reg_error","两次密码不一致,请重新输入!");
            return "redirect:pre_reg";
        }

        //添加用户:
        int count = userMapper.addUser(user);
        model.addAttribute("uid",uid);
        return "redirect:pre_login";
    }

//剩下的跟上面的控制登录一样。

After the modification, I used the @Resource annotation to reference the Mapper of the interface class, and automatically loaded the UserService service class (this function is to verify login, which cannot be lacked!!)

//这个是在UserService类里面的内容,主要用于验证登录,从数据库中查询信息:
@Autowired
    private UserMapper userMapper;

    public boolean isValidatedUser(User user){
        if (user == null)
            return false;
        User dbUser = userMapper.findUserByUid(user.getUid().trim());
        if (dbUser != null && dbUser.getPassword().equals(user.getPassword()) ){
            return true;
        }
        return false;
    }

law two

After a few days of tossing, I found that there is another way to implement it, which is to implement all the functions implemented by UserService in the control class. The code is as follows:

@RequestMapping("login")
    public String login(String uid, String uname, String password, HttpServletRequest request, HttpSession session, Model model)
    {
        //实现方法1,无需Service类
        //下面的user是从数据库里找
        User user = userMapper.findUserByUid(uid);
        if (user != null) {
            if (password.equals(user.getPassword())) {
                //保存用户登录状态
                uname = user.getUname();
                session.setAttribute("user", user);
                session.setAttribute("uname", uname);
                request.getSession().setAttribute("user",user);
                return "redirect:success";
            }
            else {
                model.addAttribute("login_error", "名称或者密码错误");
                return "redirect: pre_login";
            }
        }
        model.addAttribute("login_error", "名称或密码错误!");
        session.setAttribute("uid", "");
        return "redirect: pre_login";
}

Here is just a little modification of the code, grabbing the user directly from the database: if the uid is valid, the entity class user is not empty, then judge whether the password is correct; if the password is correct, the login is successful, if not, return the information "name or wrong password!".

Guess you like

Origin blog.csdn.net/m0_54066656/article/details/125267926