" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能

 更新時(shí)間:2023年11月16日 09:02:01   作者:freedomSTUDENT  
本文主要介紹了springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1.創(chuàng)建user表

注意:  phone這個(gè)字段設(shè)置的時(shí)候最好大于11位

2. mybatis-plus插件

安裝了mybatis-plus插件后,可以根據(jù)數(shù)據(jù)庫(kù)生成代碼 

首先連接數(shù)據(jù)庫(kù)

然后

3.導(dǎo)入相關(guān)依賴

<strong>muybatis-plus依賴</strong>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.2</version>
</dependency>

4.配置文件

server.port=8001
spring.datasource.url=jdbc:mysql://localhost:3306/test2?serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=1234

 5.前端代碼

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>register</title>
    <link rel="stylesheet" type="text/css" href="/css/style.css" rel="external nofollow" />
</head>
<body>
<div class="control">
    <div class="item">
        <div class="active">注冊(cè)</div>
    </div>
    <div class="content">
        <div style="display: block;">
            <form action="/user/register" method="post">
                <p>請(qǐng)輸入手機(jī)號(hào)</p>
                <input type="tel" placeholder="請(qǐng)輸入手機(jī)號(hào)" name="phone" id="phone"/>

                <input type="text" placeholder="請(qǐng)輸入驗(yàn)證碼" name="code"/>


                <button type="button">獲取驗(yàn)證碼</button>

                <p>請(qǐng)輸入密碼</p>
                <input type="password" placeholder="請(qǐng)輸入密碼" name="password"/>
                <br/>
                <input type="submit" value="注冊(cè)"/>
            </form>
            <p>已注冊(cè),<a href="/user/login" rel="external nofollow"  target="top">去登錄</a></p>
        </div>
    </div>
</div>


<script>
    var btn = document.querySelector('button');
    var phoneDom = document.getElementById("phone")
    // 全局變量,定義剩下的秒數(shù)
    var time = 59;
    // 注冊(cè)單擊事件  X     這里是獲取驗(yàn)證碼按鈕事件
    btn.addEventListener('click', function () {

        // btn.send('post',"/user/code")
        //判斷手機(jī)號(hào)為空
        if (phoneDom.value !== null && phoneDom.value !== '') {

            //發(fā)送請(qǐng)求,生成二維碼
            let xhr = new XMLHttpRequest();
            // methods:GET/POST請(qǐng)求方式等,url:請(qǐng)求地址,true異步(可為false同步)
            xhr.open("GET", "/user/code?phone=" + phoneDom.value, true);
            xhr.send();                                            // 發(fā)送
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {   // 成功,接收到數(shù)據(jù)
                    console.log(xhr.response);                 // 查看返回的數(shù)據(jù)(可輸出 xhr 哈)
                    // 禁用按鈕
                    btn.disabled = true;
                    // 開(kāi)啟定時(shí)器
                    var timer = setInterval(function () {
                        // 判斷剩余秒數(shù)
                        if (time == 0) {
                            // 清除定時(shí)器和復(fù)原按鈕
                            clearInterval(timer);
                            btn.disabled = false;
                            btn.innerHTML = '獲取驗(yàn)證碼';
                        } else {
                            btn.innerHTML = time + '秒';
                            time--;
                        }
                    }, 1000);

                } else if (xhr.status == 404) {
                    // 失敗,頁(yè)面未找到

                }
            }
        } else {
            alert("請(qǐng)輸入手機(jī)號(hào)!")
        }
    });
</script>


</body>
</html>

style.css

*{
    margin: 0;
    padding: 0;
}
body{
    background:#65cea7 ;
}
.control{
    width: 340px;
    background: white;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 5px;
}
.item{
    width: 340px;
    height: 60px;
    background: #eeeeee;
}
.item div{
    width: 340px;
    height: 60px;
    display: inline-block;
    color: black;
    font-size: 18px;
    text-align: center;
    line-height: 60px;
    cursor: pointer;
}
.content{
    width: 100%;
}
.content div{

    margin: 20px 30px;
    text-align: left;
}
p{
    color: #4a4a4a;
    margin-top: 30px;
    margin-bottom: 6px;
    font-size: 15px;
}

.content input[type="tel"]{
    width: 100%;
    height: 40px;
    border-radius: 3px;
    border: 1px solid #adadad;
    padding: 0 10px;
    box-sizing: border-box;
}
.content  input[type="text"]{
    width: 55%;
    height: 40px;
    border-radius: 3px;
    border: 1px solid #adadad;
    padding: 0 10px;
    box-sizing: border-box;
}

.content input[type="password"]{
    width: 100%;
    height: 40px;
    border-radius: 3px;
    border: 1px solid #adadad;
    padding: 0 10px;
    box-sizing: border-box;
}
.content button{
    margin-top: 40px;
    width: 40%;
    height: 40px;
    border-radius: 5px;
    color: white;
    border: 1px solid #adadad;
    background: cyan;
    cursor: pointer;
    letter-spacing: 4px;
    margin-bottom: 40px;


}


.content input[type="submit"]{
    margin-top: 40px;
    width: 100%;
    height: 40px;
    border-radius: 5px;
    color: white;
    border: 1px solid #adadad;
    background: cyan;
    cursor: pointer;
    letter-spacing: 4px;
    margin-bottom: 40px;
}
.active{

    background: white;
}
.item div:hover{
    background: #f6f6f6;
}

6.后端代碼

entity層

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    private String phone;

    private String password;

    private String  code;

mapper層

@Mapper
public interface UserMapper extends BaseMapper<User> {


}

 sevice層

業(yè)務(wù)層接口

public interface IUserService extends IService<User> {



    String register(User user, HttpSession session);

    String sendCode(String phone, HttpSession session);
}

業(yè)務(wù)層實(shí)現(xiàn)類

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    private UserMapper userMapper;


    @Override
    public String sendCode(String phone, HttpSession session) {
        //這里手機(jī)號(hào)碼為空則報(bào)空指針,判斷不嚴(yán)謹(jǐn)
        if (StringUtils.hasText(phone) && RegexUtil.isMobile(phone)) {
            //生成驗(yàn)證碼
            String yzmCode = RandomUtil.randomNumbers(6);

            //保存驗(yàn)證碼到session
            session.setAttribute("yzmCode", yzmCode);

            System.out.println("發(fā)送短信驗(yàn)證碼成功" + yzmCode);
            return "發(fā)送短信驗(yàn)證碼成功!驗(yàn)證碼是:" + yzmCode;
        } else {
            return "手機(jī)號(hào)格式錯(cuò)誤";
        }
    }


    @Override
    public String register(User user, HttpSession session) {
        //判斷輸入手機(jī)號(hào)的格式
        if (StringUtils.hasText(user.getPhone()) && RegexUtil.isMobile(user.getPhone())) {
            //從session拿出緩存的驗(yàn)證碼
            Object cacheCode = session.getAttribute("yzmCode");
            String code = user.getCode();
            if (cacheCode == null || !cacheCode.equals(code)) {
                return "html/register";
            }
            //3.根據(jù)手機(jī)號(hào)查詢用戶
            User user1 = query().eq("phone", user.getPhone()).one();
            if (user1 == null) {
                userMapper.insert(user);
                return "html/login";
            }
            session.setAttribute("user1", user1);
            return "html/register";
        }
        return "html/register";
    }
}

controller層

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/code")
    @ResponseBody
    public String sendCode(String phone, HttpSession session) {
        return userService.sendCode(phone, session);
    }

    @RequestMapping("/register")
    public String register(User user, HttpSession session) {
        return userService.register(user, session);
    }
}

工具類(utils)

public class RegexUtil {


    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 驗(yàn)證手機(jī)號(hào)
        m = p.matcher(str);
        b = m.matches();
        return b;
    }
}

7.調(diào)試

前端頁(yè)面

1. 未輸入手機(jī)號(hào)的時(shí)候,直接點(diǎn)擊獲取驗(yàn)證碼按鈕

2.輸入手機(jī)號(hào),但是格式錯(cuò)誤

3.輸入手機(jī)號(hào),并且格式正確

驗(yàn)證碼是模擬生成的

 String yzmCode = RandomUtil.randomNumbers(6);

4.調(diào)試的時(shí)候驗(yàn)證碼錯(cuò)誤或者手機(jī)重復(fù)注冊(cè)都是還在注冊(cè)頁(yè)面

8.代碼解析

實(shí)體類

實(shí)體類的屬性對(duì)應(yīng)數(shù)據(jù)user表字段

控制層主要寫了兩個(gè)接口

一個(gè)是發(fā)送驗(yàn)證碼接口,當(dāng)我們點(diǎn)擊前端頁(yè)面獲取驗(yàn)證碼按鈕的時(shí)候,這個(gè)接口響應(yīng)

獲取驗(yàn)證碼事件

這里主要使用了ajax技術(shù)

<script>
    var btn = document.querySelector('button');
    var phoneDom = document.getElementById("phone")
    // 全局變量,定義剩下的秒數(shù)
    var time = 59;
    // 注冊(cè)單擊事件  X     這里是獲取驗(yàn)證碼按鈕事件
    btn.addEventListener('click', function () {

        // btn.send('post',"/user/code")
        //判斷手機(jī)號(hào)為空
        if (phoneDom.value !== null && phoneDom.value !== '') {

            //發(fā)送請(qǐng)求,生成驗(yàn)證碼
            let xhr = new XMLHttpRequest();
            // methods:GET/POST請(qǐng)求方式等,url:請(qǐng)求地址,true異步(可為false同步)
            xhr.open("GET", "/user/code?phone=" + phoneDom.value, true);
            xhr.send();                                            // 發(fā)送
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {   // 成功,接收到數(shù)據(jù)
                    console.log(xhr.response);                 // 查看返回的數(shù)據(jù)(可輸出 xhr 哈)
                    // 禁用按鈕
                    btn.disabled = true;
                    // 開(kāi)啟定時(shí)器
                    var timer = setInterval(function () {
                        // 判斷剩余秒數(shù)
                        if (time == 0) {
                            // 清除定時(shí)器和復(fù)原按鈕
                            clearInterval(timer);
                            btn.disabled = false;
                            btn.innerHTML = '獲取驗(yàn)證碼';
                        } else {
                            btn.innerHTML = time + '秒';
                            time--;
                        }
                    }, 1000);

                } else if (xhr.status == 404) {
                    // 失敗,頁(yè)面未找到

                }
            }
        } else {
            alert("請(qǐng)輸入手機(jī)號(hào)!")
        }
    });
</script>

控制層調(diào)用service的接口里的一個(gè)方法

實(shí)現(xiàn)類實(shí)現(xiàn)該方法

方法如下 

 @Override
    public String sendCode(String phone, HttpSession session) {
        //這里手機(jī)號(hào)碼為空則報(bào)空指針,判斷不嚴(yán)謹(jǐn)
        if (StringUtils.hasText(phone) && RegexUtil.isMobile(phone)) {
            //生成驗(yàn)證碼
            String yzmCode = RandomUtil.randomNumbers(6);

            //保存驗(yàn)證碼到session
            session.setAttribute("yzmCode", yzmCode);

            System.out.println("發(fā)送短信驗(yàn)證碼成功" + yzmCode);
            return "發(fā)送短信驗(yàn)證碼成功!驗(yàn)證碼是:" + yzmCode;
        } else {
            return "手機(jī)號(hào)格式錯(cuò)誤";
        }
    }

1.首先判斷手機(jī)號(hào)的格式

2.如果手機(jī)號(hào)格式不為空,且手機(jī)號(hào)格式正確

通過(guò)隨機(jī)生成驗(yàn)證碼,這里只是簡(jiǎn)單的模擬短信驗(yàn)證碼,真正的實(shí)現(xiàn)這里可以調(diào)用相關(guān)的方法

3.將驗(yàn)證碼保存到session中

4.在控制臺(tái)輸出該驗(yàn)證碼

一個(gè)是注冊(cè)接口

  @Override
    public String register(User user, HttpSession session) {
        //判斷輸入手機(jī)號(hào)的格式
        if (StringUtils.hasText(user.getPhone()) && RegexUtil.isMobile(user.getPhone())) {
            //從session拿出緩存的驗(yàn)證碼
            Object cacheCode = session.getAttribute("yzmCode");
            String code = user.getCode();
            if (cacheCode == null || !cacheCode.equals(code)) {
                return "html/register";
            }
            //3.根據(jù)手機(jī)號(hào)查詢用戶
            User user1 = query().eq("phone", user.getPhone()).one();
            if (user1 == null) {
                userMapper.insert(user);
                return "html/login";
            }
         
            return "html/register";
        }
        return "html/register";
    }
}

1.判斷手機(jī)號(hào)格式

2.判斷輸入的驗(yàn)證碼和session保存的驗(yàn)證碼是否相等

3.根據(jù)手機(jī)號(hào)查詢?cè)撚脩羰欠翊嬖?/p>

  User user1 = query().eq("phone", user.getPhone()).one();

到此這篇關(guān)于springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能的文章就介紹到這了,更多相關(guān)springboot session模擬短信注冊(cè) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mac中IntelliJ IDEA 2019.1注冊(cè)過(guò)程分享

    Mac中IntelliJ IDEA 2019.1注冊(cè)過(guò)程分享

    這篇文章主要介紹了Mac中IntelliJ IDEA 2019.1注冊(cè)過(guò)程,本文給大家分享到腳本之家平臺(tái)供大家學(xué)習(xí),需要的朋友可以參考下
    2020-02-02
  • Java利用for循環(huán)輸出空心菱形的實(shí)例代碼

    Java利用for循環(huán)輸出空心菱形的實(shí)例代碼

    這篇文章主要介紹了Java利用for循環(huán)輸出空心菱形的實(shí)例代碼,需要的朋友可以參考下
    2014-02-02
  • Spring Cloud之服務(wù)監(jiān)控turbine的示例

    Spring Cloud之服務(wù)監(jiān)控turbine的示例

    這篇文章主要介紹了Spring Cloud之服務(wù)監(jiān)控turbine的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Java如何使用while循環(huán)計(jì)算一個(gè)整數(shù)的位數(shù)

    Java如何使用while循環(huán)計(jì)算一個(gè)整數(shù)的位數(shù)

    這篇文章主要介紹了Java使用while循環(huán)計(jì)算一個(gè)整數(shù)的位數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java動(dòng)態(tài)加載類示例詳解

    Java動(dòng)態(tài)加載類示例詳解

    這篇文章主要給大家介紹了關(guān)于Java動(dòng)態(tài)加載類的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 在IDEA中集成maven詳細(xì)流程圖示例

    在IDEA中集成maven詳細(xì)流程圖示例

    最近利用兩天時(shí)間學(xué)習(xí)了MyBatis以及maven,避免經(jīng)典的學(xué)過(guò)就忘記,我打算做出點(diǎn)總結(jié)以便日后復(fù)習(xí),當(dāng)然如果能幫到需要的人也是極好的,需要的朋友可以參考下
    2021-06-06
  • 解讀SpringMVC?請(qǐng)求參數(shù)接收

    解讀SpringMVC?請(qǐng)求參數(shù)接收

    這篇文章主要介紹了SpringMVC請(qǐng)求參數(shù)接收的相關(guān)操作,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • IDEA如何解決properties文件亂碼問(wèn)題

    IDEA如何解決properties文件亂碼問(wèn)題

    IDEA編輯器中打開(kāi)properties文件時(shí)出現(xiàn)中文亂碼,可以通過(guò)修改文件編碼格式來(lái)解決,具體步驟為:設(shè)置》Setting》Editor》FileEncodings,將編碼格式更改為UTF-8
    2025-01-01
  • Java 使用json-lib處理JSON詳解及實(shí)例代碼

    Java 使用json-lib處理JSON詳解及實(shí)例代碼

    這篇文章主要介紹了Java 使用json-lib處理JSON詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • SpringBoot 如何實(shí)現(xiàn)自定義Redis序列化

    SpringBoot 如何實(shí)現(xiàn)自定義Redis序列化

    這篇文章主要介紹了SpringBoot 如何實(shí)現(xiàn)自定義Redis序列化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論