IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能

一、搭建SpringBoot项目

1.1、file ——> new ——> project——> Spring Initializr——> next——> next——> next——> finish

注意选择包依赖关系

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能_第1张图片

二、springboot整合mybatis.mysql

2.1、整体结构

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能_第2张图片

2.2、设置所需要的依赖

即pom.xml文件



 4.0.0
 
 org.springframework.boot
 spring-boot-starter-parent
 2.4.2
  
 
 springboot-web04
 demo
 0.0.1-SNAPSHOT
 demo
 Demo project for Spring Boot
 
 1.8
 
 
 
 org.springframework.boot
 spring-boot-starter-data-jdbc
 
 
 org.springframework.boot
 spring-boot-starter-jdbc
 
 
 org.springframework.boot
 spring-boot-starter-thymeleaf
 
 
 org.springframework.boot
 spring-boot-starter-web
 

 
 mysql
 mysql-connector-java
 runtime
 
 
 org.springframework.boot
 spring-boot-starter-test
 test
 

 
 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.3
 

 
 mysql
 mysql-connector-java
 

 
 org.projectlombok
 lombok
 1.18.12
 provided
 

 
 com.alibaba
 druid
 1.2.1
 

 
 org.springframework.boot
 spring-boot-starter-test
 test
 
 
 org.junit.vintage
 junit-vintage-engine
 
 
 
 
 org.xmlunit
 xmlunit-core
 
 
 org.mybatis
 mybatis
 3.4.6
 
 

 
 
 
 org.springframework.boot
 spring-boot-maven-plugin
 
 
 

2.3、设置application.yml文件与pplication.properties文件

在resources目录下新建yml文件,用于存放数据库连接需要的一些数据

spring:
 datasource:
 driver-class-name: com.mysql.cj.jdbc.Driver
 url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&useSSL=true
 username: root //数据库
 password: sm1208 //密码

在application.properties文件中加入

#端口号
server.port=8080
#druid数据库连接池
type=com.alibaba.druid.pool.DruidDataSource
#清除缓存
spring.thymeleaf.cache=false
#配置mapper
mybatis.mapper-locations=classpath:mapper/*.xml

2.4、在pojo下的新建类UserLogin

package springbootweb04.demo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor

public class UserLogin {
 private String username;
 private String password;

 public String getUsername() {
 return username;
 }
}

2.5、新建数据库,名为mybatis,创建用户表,名为userLogin,创建username、password字段

2.5.1、数据库名可以随意,不过要与application.yml文件中的一致

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能_第3张图片

2.5.2、IDEA中连接数据库

Database——> +——> Data Source——> Mysql

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能_第4张图片

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能_第5张图片

2.6、mapper层

新建UserLoginMapper接口

package springbootweb04.demo.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import springbootweb04.demo.pojo.UserLogin;

import java.util.List;

@Mapper
@Repository
public interface UserLoginMapper {
 //查询
 public List queryAll();
 //添加数据
 public int add(UserLogin userLogin);
 //根据用户名查询数据
 public UserLogin queryByName(String username);
}

2.7、resources目录下的mapper目录

在resources目录下新建mapper目录,并在这个目录下新建UserLoginMapper.xml文件




 

 
 insert into userLogin values (#{username},#{password})
 

 



2.8、测试

在test.Java.springbootweb04.demo类中,测试是否能联通数据库,没有报错说明成功。

package springbootweb04.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import springbootweb04.demo.mapper.UserLoginMapper;
import springbootweb04.demo.pojo.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@SpringBootTest
class DemoApplicationTests {
 @Autowired
 DataSource dataSource;
 @Test
 void contextLoads() throws SQLException {
 System.out.println(dataSource.getClass());
 Connection connection = dataSource.getConnection();
 System.out.println(connection);

 //template模板,拿来即用
 connection.close();
 }
 @Autowired
 UserLoginMapper userLoginMapper;
 @Test
 public void toTest(){
 List userLogins = userLoginMapper.queryAll();
 userLogins.forEach(e-> System.out.println(e));
 }
}

2.9、services层

在services下新建接口UserLoginServicesI和类UserLoginServicesImpl

UserLoginServicesI接口:

package springbootweb04.demo.services;

import springbootweb04.demo.pojo.UserLogin;

import java.util.List;

public interface UserLoginServicesI {
 //查询
 public List queryAll();
 //添加数据
 public int add(UserLogin userLogin);
 //根据用户名查询数据
 public UserLogin queryByName(String username);
}

UserLoginServicesImpl类

package springbootweb04.demo.services;

import springbootweb04.demo.mapper.UserLoginMapper;
import springbootweb04.demo.pojo.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLoginServicesImpl implements UserLoginServicesI {

 @Autowired
 UserLoginMapper userLoginMapper;
 @Override
 public List queryAll() {
 return userLoginMapper.queryAll();
 }

 @Override
 public int add(UserLogin userLogin) {
 return userLoginMapper.add(userLogin);
 }

 @Override
 public UserLogin queryByName(String username) {
 return userLoginMapper.queryByName(username);
 }
}

2.A、conteoller层

编写MyController类

package springbootweb04.demo.controller;

import springbootweb04.demo.pojo.UserLogin;
import springbootweb04.demo.services.UserLoginServicesImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {
 @Autowired
 UserLoginServicesImpl userLoginServicesImpl;

 @RequestMapping("/toLogin")
 public String toLogin(){
 return "login";
 }

 @RequestMapping("/LoginSuccess")
 public String LoginSuccess(Model model, UserLogin userLogin){
 //先查询看该用户名是否存在
 UserLogin userLogin1 = userLoginServicesImpl.queryByName(userLogin.getUsername());
 if(userLogin1 != null){ // 如果查询的用户不为空
 System.out.println(userLogin1.toString());
 return "success";
 }
 else{
 //返回到登录页面
 model.addAttribute("data","该用户不存在,请先注册");
 return "login";
 }
 }
 //登录界面
 @RequestMapping("/toRegister")
 public String toRegister(){
 return "register";
 }
 @RequestMapping("/RegisterSuccess")
 public String toRegisterSuccess(Model model,UserLogin userLogin){
 //将账号密码加入到数据库中
 int add = userLoginServicesImpl.add(userLogin);
 System.out.println("数据插入成功!");
 model.addAttribute("data","注册成功,请登录!");
 return "login";
 }
}

三、编写前端页面

将以下三个页面放在templates下面

login.html:登录页面




 
 Title




登录界面



用户名:

密码:


register.html:注册页面




 
 Title




注册界面

用户名:

密码:

确认密码:

success.html:成功界面




 
 Title





四、运行测试

:localhost:8080/toLogin

到此这篇关于IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能的文章就介绍到这了,更多相关SpringBoot+MyBatis+MySql动态登录与注册内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能)