In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to achieve simple login registration on SpringBoot. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Step 1: build a simple project
Step 2: create a simple data table
Step 3: the configuration file is as follows:
Pom.xml file configuration:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.5 com.wei demo01 0.0.1-SNAPSHOT demo01 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot Spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0 mysql mysql-connector-java 8.0.24 Runtime org.springframework.boot spring-boot-starter-test test junit junit test org.projectlombok lombok true Org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
Application.properties file configuration:
# change the port number: server.port=8080# prefixes the entire project so that multiple projects will not be repeated: # server.servlet.context-path=/first#school.grade=3#school.classNum=4 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.password=rootspring.datasource.username=rootspring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.thymeleaf.prefix=classpath:/templates/#mapper.mappers=tk.mybatis.mapper.common.Mapper#mapper.not-empty=true
The project directory is as follows:
LoginController.java file:
Note: @ Controller can only use this, not @ RestController
Package com.wei.demo01.controller;import com.wei.demo01.entity.UserBean;import com.wei.demo01.service.UserService;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.Resource;@Slf4j@Controllerpublic class LoginController {/ / inject Service into Web layer @ Resource UserService userService / / log in to @ RequestMapping ("/ login") public String show () {return "login";} @ RequestMapping (value = "/ loginIn", method = RequestMethod.POST) public String login (String name,String password) {UserBean userBean = userService.LoginIn (name, password); log.info ("name: {}", name); log.info ("password: {}", password) If {return "success";} else {return "error";}} @ RequestMapping ("/ signup") public String disp () {return "signup" } / / implement registration @ RequestMapping (value = "/ register", method = RequestMethod.POST) public String signUp (String name,String password) {userService.Insert (name, password); return "success";}}
UserBean.java file
Package com.wei.demo01.entity; import java.io.Serializable; public class UserBean implements Serializable {private Integer id; private String name; private String password; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name } public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}}
UserMapper interface file:
Note: in order to be lazy, I started adding @ Mapper annotations here in order to stop writing mapper mapping files (that xml is really too slow)
Package com.wei.demo01.mapper; import com.wei.demo01.entity.UserBean;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Component;import org.springframework.stereotype.Repository @ Mapperpublic interface UserMapper {/ / query, you can implement the login function @ Select ("SELECT * FROM user WHERE name = # {name} AND password = # {password}") UserBean getInfo (@ Param ("name") String name, @ Param ("password") String password) / / to add @ Param modification to multiple parameters, you can register @ Insert ("insert into user (name,password) values (# {name}, # {password})") void saveInfo (@ Param ("name") String name, @ Param ("password") String password);}
UserService.java file
Package com.wei.demo01.service; import com.wei.demo01.entity.UserBean;import com.wei.demo01.mapper.UserMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; import javax.annotation.Resource; @ Servicepublic class UserService {/ / inject dao layer attributes into service layer @ Resource private UserMapper userMapper; public UserBean LoginIn (String name, String password) {return userMapper.getInfo (name,password) } public void Insert (String name,String password) {userMapper.saveInfo (name, password);}}
Here are some static html pages:
Let's put all the code in one box in order.
Error login failed! Hello, index. Login account:
Password:
If registration is successful, please enter your name:
Please enter your password:
Welcome to success. Congratulations on successful login / registration.
Finally, the login effect is displayed:
Registration effect display:
Registered successfully! Database updated successfully!
Some netizens said that my registration button could not be opened and could not jump to the registration page, so I would like to update it here:
Just add the following to the login page:
This is the end of the article on "how to achieve simple login and registration in SpringBoot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.