SpringBoot实现简单的登录注册(超级详细)第⼀步:建⽴简单的项⽬
第⼆步:建⼀个简单的数据表
第三步:配置⽂件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.
0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wei</groupId>
<artifactId>demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo01</name>
<name>demo01</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties⽂件配置:
#改变端⼝号:
server.port=8080
#给整个项⽬加个前缀,多个项⽬就不会重复:
#t-path=/first
#ade=3
#school.classNum=4
spring.datasource.sql.cj.jdbc.Driver
spring.datasource.password=root
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.thymeleaf.prefix=classpath:/templates/
#mapper.batis.mappermon.Mapper
#-empty=true
项⽬⽬录如下:
LoginController.java⽂件:
注意:@Controller只能⽤这个,不能⽤@RestController
package com.ller;
import com.ity.UserBean;
import com.wei.demo01.service.UserService;
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
@Controller
public class LoginController {
//将Service注⼊Web层
@Resource
UserService userService;
param name//实现登录
@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(userBean!=null){
return "success";
}else {
return "error";
}
}
@RequestMapping("/signup")
public String disp(){
return "signup";
}
//实现注册功能
@RequestMapping(value = "/register",method = RequestMethod.POST) public String signUp(String name,String password){
userService.Insert(name, password);
return "success";
}
}
UserBean.java⽂件
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论