Simple login function through Springboot and MYSQL+Mybatis+thymeleaf
First create the Springboot project package
Create a simple login user and password
configuration properties in mysql
First application.properties
This is a startup class generated by eclipse
Then write a User entity class in entity
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;
}
}
Then to the data access layer (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);
}
Then create an .xml file in the mapper package
and then go to the data service layer (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);
}
}
And Service interface
package com.example.second.service;
import com.example.second.entity.User;
public interface UserService {
public User Login(String username,String password);
}
Finally to the front controller (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;
}
}
Also write a login page login.html controller
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";
}
}
Because we use @Controller (I will do an explanation next time) and
finally write a login.html to implement front-end login
<!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>
Run the screenshot to
log in successfully.
Note: If you are not using the mysql user and password, you will be prompted that the login failed!