最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)登錄與注冊(cè)功能

 更新時(shí)間:2021年02月18日 17:17:38   作者:孫不堅(jiān)1208  
這篇文章主要介紹了IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)登錄與注冊(cè)功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、搭建SpringBoot項(xiàng)目

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

注意選擇包依賴關(guān)系

在這里插入圖片描述

二、springboot整合mybatis.mysql

2.1、整體結(jié)構(gòu)

在這里插入圖片描述

2.2、設(shè)置所需要的依賴

即pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/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.2</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>springboot-web04</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>demo</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-data-jdbc</artifactId>
 </dependency>
 <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>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
<!--下面的都是我加的-->
 <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>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>2.1.3</version>
 </dependency>

 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 </dependency>

 <dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <version>1.18.12</version>
 <scope>provided</scope>
 </dependency>

 <dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>druid</artifactId>
 <version>1.2.1</version>
 </dependency>

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 <exclusions>
 <exclusion>
 <groupId>org.junit.vintage</groupId>
 <artifactId>junit-vintage-engine</artifactId>
 </exclusion>
 </exclusions>
 </dependency>
 <dependency>
 <groupId>org.xmlunit</groupId>
 <artifactId>xmlunit-core</artifactId>
 </dependency>
 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.4.6</version>
 </dependency>
 </dependencies>

 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>

</project>

2.3、設(shè)置application.yml文件與pplication.properties文件

在resources目錄下新建yml文件,用于存放數(shù)據(jù)庫連接需要的一些數(shù)據(jù)

spring:
 datasource:
 driver-class-name: com.mysql.cj.jdbc.Driver
 url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&useSSL=true
 username: root //數(shù)據(jù)庫
 password: sm1208 //密碼

在application.properties文件中加入

#端口號(hào)
server.port=8080
#druid數(shù)據(jù)庫連接池
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、新建數(shù)據(jù)庫,名為mybatis,創(chuàng)建用戶表,名為userLogin,創(chuàng)建username、password字段

2.5.1、數(shù)據(jù)庫名可以隨意,不過要與application.yml文件中的一致

在這里插入圖片描述

2.5.2、IDEA中連接數(shù)據(jù)庫

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

在這里插入圖片描述

在這里插入圖片描述

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<UserLogin> queryAll();
 //添加數(shù)據(jù)
 public int add(UserLogin userLogin);
 //根據(jù)用戶名查詢數(shù)據(jù)
 public UserLogin queryByName(String username);
}

2.7、resources目錄下的mapper目錄

在resources目錄下新建mapper目錄,并在這個(gè)目錄下新建UserLoginMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="springbootweb04.demo.mapper.UserLoginMapper">
 <select id="queryAll" resultType="springbootweb04.demo.pojo.UserLogin">
 select * from userLogin
 </select>

 <insert id="add" parameterType="springbootweb04.demo.pojo.UserLogin">
 insert into userLogin values (#{username},#{password})
 </insert>

 <select id="queryByName" resultType="springbootweb04.demo.pojo.UserLogin">
 select * from userLogin where username = #{username}
 </select>

</mapper>

2.8、測試

在test.Java.springbootweb04.demo類中,測試是否能聯(lián)通數(shù)據(jù)庫,沒有報(bào)錯(cuò)說明成功。

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<UserLogin> 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<UserLogin> queryAll();
 //添加數(shù)據(jù)
 public int add(UserLogin userLogin);
 //根據(jù)用戶名查詢數(shù)據(jù)
 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<UserLogin> 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","該用戶不存在,請(qǐng)先注冊(cè)");
 return "login";
 }
 }
 //登錄界面
 @RequestMapping("/toRegister")
 public String toRegister(){
 return "register";
 }
 @RequestMapping("/RegisterSuccess")
 public String toRegisterSuccess(Model model,UserLogin userLogin){
 //將賬號(hào)密碼加入到數(shù)據(jù)庫中
 int add = userLoginServicesImpl.add(userLogin);
 System.out.println("數(shù)據(jù)插入成功!");
 model.addAttribute("data","注冊(cè)成功,請(qǐng)登錄!");
 return "login";
 }
}

三、編寫前端頁面

將以下三個(gè)頁面放在templates下面

login.html:登錄頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body style="background: aqua">
<div align="center">
 <br><br><h2>登錄界面</h2><br><br>
 <span th:text="${data}" style="text-color:red;font-size: 10px"></span>
 <form method="get" action="/LoginSuccess">
 用戶名:<input type="text" name="username" placeholder="請(qǐng)輸入用戶名" required/><br><br>
 密碼:<input type="text" name="password" placeholder="請(qǐng)輸入密碼" required/><br><br>
 <input type="submit" value="登錄">
 </form>
 <br>
 <form method="get" action="/toRegister">
 <input type="submit" value="注冊(cè)">
 </form>
</div>
</body>
</html>

register.html:注冊(cè)頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body style="background: aqua">
<div align="center">
 <br><br>注冊(cè)界面<br><br>
 <form method="get" action="/RegisterSuccess">
 用戶名:<input type="text" name="username" placeholder="請(qǐng)輸入用戶名" required/><br><br>
 密碼:<input type="text" name="password" placeholder="請(qǐng)輸入密碼" required/><br><br>
 確認(rèn)密碼:<input type="text" name="password2" placeholder="請(qǐng)輸入密碼" required/><br><br>
 <input type="submit" value="注冊(cè)">
 </form>
</div>
</body>
</html>

success.html:成功界面

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>

</body>
</html>

四、運(yùn)行測試

:localhost:8080/toLogin

到此這篇關(guān)于IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)登錄與注冊(cè)功能的文章就介紹到這了,更多相關(guān)SpringBoot+MyBatis+MySql動(dòng)態(tài)登錄與注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IDEA中Services欄不顯示的解決方案小結(jié)

    IDEA中Services欄不顯示的解決方案小結(jié)

    正常編譯完一個(gè)SpringBoot或者SringCloud項(xiàng)目之后,Services都會(huì)顯示出你有哪些服務(wù),如果沒有services欄怎么解決呢?下面小編給大家分享IDEA中Services欄不顯示的解決方案小結(jié),感興趣的朋友一起看看吧
    2021-08-08
  • Java中String類的常用方法總結(jié)

    Java中String類的常用方法總結(jié)

    java.lang.String?類代表字符串。Java程序中所有的字符串文字(例如"abc"?)都可以被看作是實(shí)現(xiàn)此類的實(shí)例。本文主要為大家介紹了String類的常用方法,需要的可以參考一下
    2022-11-11
  • Java俄羅斯方塊小游戲

    Java俄羅斯方塊小游戲

    這篇文章主要為大家詳細(xì)介紹了Java俄羅斯方塊小游戲,實(shí)現(xiàn)了俄羅斯的經(jīng)典功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • springboot2中session超時(shí),退到登錄頁面方式

    springboot2中session超時(shí),退到登錄頁面方式

    這篇文章主要介紹了springboot2中session超時(shí),退到登錄頁面方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java方法及數(shù)組相關(guān)原理解析

    Java方法及數(shù)組相關(guān)原理解析

    這篇文章主要介紹了Java方法及數(shù)組相關(guān)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java中的split使用方法詳解

    Java中的split使用方法詳解

    這篇文章主要介紹了Java中的split使用方法詳解,Java 中 String 的 split 方法可以將字符串根據(jù)指定的間隔進(jìn)行切割,經(jīng)過切割后得到的返回值是一個(gè)字符串?dāng)?shù)組,需要的朋友可以參考下
    2023-10-10
  • idea中如何過濾某些文件不提交的方法實(shí)現(xiàn)

    idea中如何過濾某些文件不提交的方法實(shí)現(xiàn)

    本文主要介紹了idea中如何過濾某些文件不提交,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 深入解析SpringBatch適配器

    深入解析SpringBatch適配器

    Spring Batch是Spring的一個(gè)子項(xiàng)目,使用Java語言并基于Spring框架為基礎(chǔ)開發(fā),使得已經(jīng)使用 Spring 框架的開發(fā)者或者企業(yè)更容易訪問和利用企業(yè)服務(wù),本文給大家介紹SpringBatch適配器的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-11-11
  • Windows7下的Java運(yùn)行環(huán)境搭建過程圖解

    Windows7下的Java運(yùn)行環(huán)境搭建過程圖解

    這篇文章主要介紹了Windows7下的Java運(yùn)行環(huán)境搭建過程圖解,需要的朋友可以參考下
    2014-04-04
  • springboot項(xiàng)目中配置redis詳細(xì)的教程

    springboot項(xiàng)目中配置redis詳細(xì)的教程

    Redis是一種高性能的鍵值存儲(chǔ)數(shù)據(jù)庫,而Spring Boot是一個(gè)簡化了開發(fā)過程的Java框架,這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目中配置redis詳細(xì)的教程,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04

最新評(píng)論

碌曲县| 乌兰县| 青河县| 方正县| 雷波县| 微博| 万盛区| 南安市| 新平| 内乡县| 台江县| 长宁区| 公主岭市| 斗六市| 牙克石市| 聂拉木县| 冕宁县| 靖安县| 白银市| 原平市| 新乡县| 溧水县| 黑山县| 绥滨县| 阿克陶县| 宜宾市| 临安市| 安阳市| 德昌县| 乌兰县| 会昌县| 沅江市| 全南县| 洛扎县| 广汉市| 江口县| 合阳县| 巨野县| 昌黎县| 施秉县| 云梦县|