国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

這篇具有很好參考價(jià)值的文章主要介紹了java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

準(zhǔn)備工作:

創(chuàng)建數(shù)據(jù)庫stu;? ? ? ? create database stu charset=utf8;

使用數(shù)據(jù)庫stu;? ? ? ? ? ? use stu;

創(chuàng)建用戶表user(id,username,password,nick)

create table user(id int primary key auto_increment,username varchar(50),password varchar(50),nick varchar(50));

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

?

1.開始創(chuàng)建springboot工程,勾選Web->spring Web,? SQL->MyBatis Frameword及MySQL Driver三個(gè)選項(xiàng),完成

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

?2.打開src->main->resources->application.properties,設(shè)置數(shù)據(jù)庫信息

輸入

spring.datasource.url=jdbc:mysql://數(shù)據(jù)庫路徑/數(shù)據(jù)庫名?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username="數(shù)據(jù)庫賬號"
spring.datasource.password="數(shù)據(jù)庫密碼"

3.在src->main->resources->static目錄下開始創(chuàng)建前端頁面首頁index.html

首頁實(shí)現(xiàn)兩個(gè)a標(biāo)簽跳轉(zhuǎn)到注冊頁面及登錄頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>首頁</h1>
    <a href="/reg.html">注冊</a>
    <a href="/login.html">登錄</a>
</div>
</body>
</html>

?創(chuàng)建注冊頁面reg.html和登錄頁面login.html

//-------------------------------注冊功能------------------------------

? ? ? ? ?3-1.注冊頁面準(zhǔn)備三個(gè)文本框( 用戶名,密碼,昵稱 )一個(gè)按鈕( 注冊 ),并導(dǎo)入vue和axios

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
  <input type="text" placeholder="請輸入用戶名">
  <input type="password" placeholder="請輸入密碼">
  <input type="text" placeholder="請輸入昵稱">
  <input type="button" value="注冊">
</div>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{

        },
        methods:{

        }
    })
</script>
</body>
</html>

? ? ? ? 3-2.創(chuàng)建一個(gè)對象保存數(shù)據(jù),用于與文本框雙向綁定

? ? ? ? 3-3.注冊按鈕添加點(diǎn)擊事件方法,在方法處聲明該方法發(fā)送請求,將返回的數(shù)據(jù)判斷用戶是否注冊完成

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <input type="text" v-model="user.username" placeholder="請輸入用戶名">
    <input type="password" v-model="user.password" placeholder="請輸入密碼">
    <input type="text" v-model="user.nick" placeholder="請輸入昵稱">
    <input type="button" value="注冊" @click="reg()">
</div>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            user: {
                username: "", password: "", nick: ""
            }
        },
        methods: {
            reg() {
                axios.post("/reg", v.user).then(function (response) {
                    if (response.data == 1) {
                        alert("注冊完成!")
                        location.href="/";//回到首頁
                    } else {
                        alert("用戶名已存在!")
                    }
                })
            }
        }
    })
</script>
</body>
</html>

4.在src->main->java->創(chuàng)建的目錄下創(chuàng)建子目錄controller,目錄entity及目錄mapper,在controller目錄下創(chuàng)建UserController的JAVA文件,在entity目錄下創(chuàng)建User的JAVA文件,在mapper目錄下創(chuàng)建UserMapper接口

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

? ? ? ? 4-1.在User文件中創(chuàng)建實(shí)體類用于封裝屬性并生成getter and setter方法

package cn.tedu.weibo2.entity;

public class User {
    private Integer id;
    private String username;
    private String password;
    private String nick;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }
}

? ? ? ? 4-2.?在UserMapper接口中添加Mapper注解

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

? ? ? ? ?在UserController類中添加RestController注解,并定義UserMapper及Autowired注解

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

?5.解決請求:在UserController類中解決/reg的請求定義reg方法,

? ? ? ? 5-1:到數(shù)據(jù)庫查詢用戶輸入的用戶名是否存在,若存在則返回2,若不存在則進(jìn)行數(shù)據(jù)庫添加用戶信息并返回1;

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

?

?

? ? ? ? 5-2:數(shù)據(jù)庫查詢,在UserMapper接口中定義sql語句查詢用戶名是否存在,并定義添加用戶sql語句用于用戶注冊

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

?

注冊完成檢測:

在首頁進(jìn)入到注冊頁面,輸入注冊信息

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

注冊完成進(jìn)入數(shù)據(jù)庫查看是否用戶添加成功!

?java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

注冊最終代碼:

UserController:文章來源地址http://www.zghlxwxcb.cn/news/detail-474474.html

package cn.tedu.weibo2.controller;

import cn.tedu.weibo2.entity.User;
import cn.tedu.weibo2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        User u = mapper.selectUser(user.getUsername());
        if (u!=null){
            return 2;//若用戶輸入的用戶名存在,則注冊失敗
        }
        mapper.insertUser(user);
        return 1;//用戶名不存在,注冊成功
    }
}

UserMapper:

package cn.tedu.weibo2.mapper;

import cn.tedu.weibo2.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {

    @Select("select id,password,nick from user where username=#{username}")
    User selectUser(String username);

    @Insert("insert into user values(null,#{username},#{password},#{nick})")
    void insertUser(User user);
}

?//---------------------------------------登錄功能-------------------------------

1.創(chuàng)建頁面login.html 包含兩個(gè)文本框(用戶名,密碼)及一個(gè)登錄按鈕

? ? ? ? 1-1.導(dǎo)入vue及axios文件

<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

? ? ? ? 1-2.定義user對象,儲存用戶名及密碼

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

? ? ? ? ?1-3.對用戶名輸入框及密碼框雙向綁定,獲取用戶輸入的內(nèi)容,登錄按鈕添加點(diǎn)擊事件

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

? ? ? ? ?1-4.點(diǎn)擊事件方法發(fā)送請求,判斷用戶名是否存在,若存在判斷密碼是否正確

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

?2.解決登錄發(fā)送過來的請求

? ? ? ? 2-1.在UserController類解決/login請求,定義login()方法

? ? ? ? 判斷用戶是否存在,若存在判斷密碼是否正確

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

運(yùn)行測試:

進(jìn)入登錄頁面,

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)?

java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)?

?

?最終代碼:

UserController:

package cn.tedu.weibo2.controller;

import cn.tedu.weibo2.entity.User;
import cn.tedu.weibo2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        User u = mapper.selectUser(user.getUsername());
        if (u!=null){
            return 2;//若用戶輸入的用戶名存在,則注冊失敗
        }
        mapper.insertUser(user);
        return 1;//用戶名不存在,注冊成功
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        User u =mapper.selectUser(user.getUsername());
        if (u!=null){//用戶存在
            if (user.getPassword().equals(u.getPassword())){//判斷用戶輸入的密碼是否與數(shù)據(jù)庫密碼相同
                return 1;//密碼一樣,登錄成功
            }
            return 2;//密碼錯(cuò)誤
        }
        return 3;//用戶不存在
    }

}

到了這里,關(guān)于java連接數(shù)據(jù)庫實(shí)現(xiàn)登錄與注冊小功能(小白版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包