Springboot通过数据库实现简单登录功能

通过Springboot和MYSQL+Mybatis+thymeleaf实现简单的登录功能

先创建Springboot的项目包

在这里插入图片描述
在mysql创建一个简单的登录用户和密码
在这里插入图片描述
配置属性
首先application.properties
在这里插入图片描述
这是eclipse生成的一个启动类
在这里插入图片描述

然后在entity写一个User的实体类

package com.example.second.entity;

public class User {
    
    
	  private String username;
	    private String password;
		public String getUsername() {
    
    
			return username;
		}
		public void setUsername(String username) {
    
    
			this.username = username;
		}
		public String getPassword() {
    
    
			return password;
		}
		public void setPassword(String password) {
    
    
			this.password = password;
		}
	    
}

然后到数据访问层(Dao)

package com.example.second.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import com.example.second.entity.User;

@Mapper
@Component(value="userDao")
public interface UserDao {
    
    
	//登录功能接口
	public User Login(@Param("username")String username,
	   @Param("password")String password);

}

然后到mapper包里面建立.xml文件
在这里插入图片描述
然后到数据服务层(Service)

package com.example.second.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.second.dao.UserDao;
import com.example.second.entity.User;
import com.example.second.service.UserService;

@Service("UserService")
public class UserServiceImpl implements UserService{
    
    
	@Autowired
	private UserDao userDao;
	@Override
	public User Login(String username, String password) {
    
    
		return userDao.Login(username, password);
	}
}

还有Service的接口

package com.example.second.service;

import com.example.second.entity.User;

public interface UserService {
    
    
 public User Login(String username,String password);
}

最后到前端控制器(Controller)

package com.example.second.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.example.second.entity.User;
import com.example.second.service.UserService;
@Controller
public class UserController {
    
    
	@Autowired
	private UserService userService;
	@PostMapping(value="/logining")
	public ModelAndView login(HttpServletRequest request,ModelAndView mv, User u) {
    
    
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		u=userService.Login(username, password);
		if(u!=null) {
    
    
			mv.setViewName("logining");
			mv.addObject("info", "登陆成功");
			return mv;
		}
		mv.setViewName("logining");
		mv.addObject("info", "登陆失败");
		return mv;
		
	}
	
}

还要写一个登录页面login.html的控制器

package com.example.second.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//登录页面控制
@Controller
public class PageController {
    
    
	@RequestMapping(value="/login")
	public String str(String s) {
    
    
		return "logining";
	}
}

因为我们使用@Controller(下次我会做一篇解释)
最后还要写一个logining.html来实现前端登录

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>实现简单的登录</title>
</head>
<body> 
<center>
<table>
	<form action="/logining" method="post">
	<tr>
	<td>
	  <input type="text" name="username">
	  </td>
	   </tr>
	  <tr>
	  <td>
	  <input type="password"  name="password">
	    </td>
	  </tr>
	  <tr>
	  <td>
	  <input  type="submit" value="登陆" >
	     </td>
	  </tr>
	</form>
		<span th:text="${info}"></span>
</table>
</center>
</body>
</html>

运行截图
在这里插入图片描述
登录成功
在这里插入图片描述
注:如果不是使用mysql的用户和密码,会提示登陆失败!

猜你喜欢

转载自blog.csdn.net/qq_46046423/article/details/109304095