springboot+thymeleaf+mybatis简单实现用户登录

在之前文章基础上编写springboot整合mybatis实现简单CRUDspringboot+thymeleaf模板引擎实现国际化

一、UserMapper.java

    //登录
    User userLogin(@Param("email")String email, @Param("password")String password);

二、UserMapper.xml

    <select id="userLogin" resultType="User">
        select * from user where email = #{email} and password = #{password}
    </select>

三、UserController.java

这里就不写service层了,直接调用持久层实现效果即可。

package com.ltl.controller;

import com.ltl.domain.User;
import com.ltl.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author LTL
 * @date 2021-06-24 21:44
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
    @Autowired
    UserMapper userMapper;

    //用户登录
    @PostMapping("/login")
    public String login(@RequestParam("email") String email,
                        @RequestParam("password")String password,
                        HttpSession session, Map<String,Object> map){
     //注意数据库表里的用户邮箱和密码不要有一样的,否则会返回多个用户导致报错
     User user = userMapper.userLogin(email,password);
     if(user!=null){
         session.setAttribute("loginUser",user.getUsername());
         //这里的main.html只是达到隐藏真实登录成功的页面名称,重定向到/main.html请求
         return "redirect:/main.html";
     }else{
         //登陆失败
         map.put("msg","邮箱密码错误");
         return  "login";
     }
 }

四、MyMvcConfig.java

package com.ltl.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author LTL
 * @date 2021-06-24 14:49
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index").setViewName("login");
        //登录成功的跳转到页面dashboard.html
        registry.addViewController("/main.html").setViewName("dashboard");
    }
}

五、login.html

在这里插入图片描述

六、运行

在这里插入图片描述
登录成功跳转页面
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田思雨》

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值