微服务系列文章之 Springboot+Vue实现登录注册

news2024/10/6 20:26:34

一、springBoot

创建springBoot项目

分为三个包,分别为controller,service, dao以及resource目录下的xml文件。

UserController.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

package springbootmybatis.controller;

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import springbootmybatis.pojo.User;

import springbootmybatis.service.UserService;

import javax.annotation.Resource;

@RestController

public class UserController {

    @Resource

    UserService userService;

    @PostMapping("/register/")

    @CrossOrigin("*")

    String register(@RequestBody User user) {

        System.out.println("有人请求注册!");

        int res = userService.register(user.getAccount(), user.getPassword());

        if(res==1) {

            return "注册成功";

        } else {

            return "注册失败";

        }

    }

    @PostMapping("/login/")

    @CrossOrigin("*")

    String login(@RequestBody User user) {

        int res = userService.login(user.getAccount(), user.getPassword());

        if(res==1) {

            return "登录成功";

        } else {

            return "登录失败";

        }

    }

}

UserService.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package springbootmybatis.service;

import org.springframework.stereotype.Service;

import springbootmybatis.dao.UserMapper;

import javax.annotation.Resource;

@Service

public class UserService {

    @Resource

    UserMapper userMapper;

    public int register(String account, String password) {

        return userMapper.register(account, password);

    }

    public int login(String account, String password) {

        return userMapper.login(account, password);

    }

}

User.java (我安装了lombok插件)

1

2

3

4

5

6

7

8

9

package springbootmybatis.pojo;

import lombok.Data;

@Data

public class User {

    private String account;

    private String password;

}

UserMapper.java

1

2

3

4

5

6

7

8

9

10

package springbootmybatis.dao;

import org.apache.ibatis.annotations.Mapper;

@Mapper

public interface UserMapper {

    int register(String account, String password);

    int login(String account, String password);

}

UserMapper.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?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="springbootmybatis.dao.UserMapper">

    <insert id="register">

       insert into User (account, password) values (#{account}, #{password});

    </insert>

    <select id="login" resultType="Integer">

        select count(*) from User where account=#{account} and password=#{password};

    </select>

</mapper>

主干配置

application.yaml

1

2

3

4

5

6

7

8

9

10

11

12

server.port: 8000

spring:

  datasource:

    username: root

    password: 123456

    url: jdbc:mysql://localhost:3306/community?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8

    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:

    type-aliases-package: springbootmybatis.pojo

    mapper-locations: classpath:mybatis/mapper/*.xml

    configuration:

      map-underscore-to-camel-case: true

数据库需要建相应得到表

1

2

3

4

CREATE TABLE `user` (

  `account` varchar(255) COLLATE utf8_bin DEFAULT NULL,

  `password` varchar(255) COLLATE utf8_bin DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

二、创建VUE项目

安装node,npm,配置环境变量。
配置cnpm仓库,下载的时候可以快一些。

1

npm i -g cnpm --registry=https://registry.npm.taobao.org

安装VUE

1

npm i -g vue-cli

初始化包结构

1

vue init webpack project

启动项目

1

2

3

4

5

6

# 进入项目目录

cd vue-01

# 编译

npm install

# 启动

npm run dev

修改项目文件,按照如下结构

 

APP.vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<template>

  <div id="app">

    <router-view/>

  </div>

</template>

<script>

export default {

  name: 'App'

}

</script>

<style>

</style>

welcome.vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<template>

  <div>

    <el-input v-model="account" placeholder="请输入帐号"></el-input>

    <el-input v-model="password" placeholder="请输入密码" show-password></el-input>

    <el-button type="primary" @click="login">登录</el-button>

    <el-button type="primary" @click="register">注册</el-button>

  </div>

</template>

<script>

export default {

  name: 'welcome',

  data () {

    return {

      account: '',

      password: ''

    }

  },

  methods: {

    register: function () {

      this.axios.post('/api/register/', {

        account: this.account,

        password: this.password

      }).then(function (response) {

        console.log(response);

      }).catch(function (error) {

        console.log(error);

      });

      // this.$router.push({path:'/registry'});

    },

    login: function () {

      this.axios.post('/api/login/', {

        account: this.account,

        password: this.password

      }).then(function () {

        alert('登录成功');

      }).catch(function (e) {

        alert(e)

      })

      // this.$router.push({path: '/board'});

    }

  }

}

</script>

<style scoped>

</style>

main.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

// The Vue build version to load with the `import` command

// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from 'vue'

import App from './App'

import router from './router'

import ElementUI from 'element-ui'

import 'element-ui/lib/theme-chalk/index.css'

import axios from 'axios'

import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

Vue.use(ElementUI)

Vue.config.productionTip = false

/* eslint-disable no-new */

new Vue({

  el: '#app',

  router,

  components: {App},

  template: '<App/>'

})

router/index.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import Vue from 'vue'

import Router from 'vue-router'

import welcome from '@/components/welcome'

Vue.use(Router)

export default new Router({

  routes: [

    {

      path: '/',

      name: 'welcome',

      component: welcome

    }

  ]

})

config/index.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

'use strict'

// Template version: 1.3.1

// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')

module.exports = {

  dev: {

    // Paths

    assetsSubDirectory: 'static',

    assetsPublicPath: '/',

    proxyTable: {

      '/api': {

        target: 'http://localhost:8000', // 后端接口的域名

        // secure: false,  // 如果是https接口,需要配置这个参数

        changeOrigin: true, // 如果接口跨域,需要进行这个参数配置

        pathRewrite: {

          '^/api': '' //路径重写,当你的url带有api字段时如/api/v1/tenant,

          //可以将路径重写为与规则一样的名称,即你在开发时省去了再添加api的操作

        }

      }

    },

    // Various Dev Server settings

    host: 'localhost', // can be overwritten by process.env.HOST

    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined

    autoOpenBrowser: false,

    errorOverlay: true,

    notifyOnErrors: true,

    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    // Use Eslint Loader?

    // If true, your code will be linted during bundling and

    // linting errors and warnings will be shown in the console.

    useEslint: true,

    // If true, eslint errors and warnings will also be shown in the error overlay

    // in the browser.

    showEslintErrorsInOverlay: false,

    /**

     * Source Maps

     */

    // https://webpack.js.org/configuration/devtool/#development

    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,

    // set this to false - it *may* help

    // https://vue-loader.vuejs.org/en/options.html#cachebusting

    cacheBusting: true,

    cssSourceMap: true

  },

  build: {

    // Template for index.html

    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths

    assetsRoot: path.resolve(__dirname, '../dist'),

    assetsSubDirectory: 'static',

    assetsPublicPath: '/',

    /**

     * Source Maps

     */

    productionSourceMap: true,

    // https://webpack.js.org/configuration/devtool/#production

    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as

    // Surge or Netlify already gzip all static assets for you.

    // Before setting to `true`, make sure to:

    // npm install --save-dev compression-webpack-plugin

    productionGzip: false,

    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to

    // View the bundle analyzer report after build finishes:

    // `npm run build --report`

    // Set to `true` or `false` to always turn it on or off

    bundleAnalyzerReport: process.env.npm_config_report

  }

}

输入账号密码,实现简单的注册,登录功能。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/880278.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

代理模式概述

1.代理模式概述 学习内容 1&#xff09;概述 为什么要有 “代理” &#xff1f; 生活中就有很多例子&#xff0c;比如委托业务&#xff0c;黄牛&#xff08;票贩子&#xff09;等等代理就是被代理者没有能力或者不愿意去完成某件事情&#xff0c;需要找个人代替自己去完成这…

地理数据的双重呈现:GIS与数据可视化

前一篇文章带大家了解了GIS与三维GIS的关系&#xff0c;本文就GIS话题带大家一起探讨一下GIS和数据可视化之间的关系。 GIS&#xff08;地理信息系统&#xff09;和数据可视化在地理信息科学领域扮演着重要的角色&#xff0c;它们之间密切相关且相互增强。GIS是一种用于采集、…

算法:深度优先遍历

文章目录 什么是深搜典型题目积累 本篇主要积累的是深度优先遍历算法 什么是深搜 深度优先搜索英文缩写为 DFS 即Depth First Search 其过程是对每一个可能的分支路径深入到不能再深入为止&#xff0c;而且每个节点只能访问一次 简单来说就是: 一路走到头&#xff0c;不撞墙…

ESD实时监测报警系统的主要功能

ESD&#xff08;Electrostatic Discharge&#xff09;实时监测报警系统是一种用于检测和预防静电放电的系统。静电放电是指在两个或多个物体之间由于电荷失衡而引起的突然放电现象&#xff0c;可能会对电子设备、化学品等敏感物体造成损害。 ESD实时监测报警系统的主要功能是通…

冠达管理:价格破发是什么意思啊?

价格破发是股票商场中一个比较常见的术语&#xff0c;也是常常让出资者感到困惑的现象之一。价格破发是指新股发行后&#xff0c;由于各种原因&#xff0c;股票价格低于发行价的现象。那么&#xff0c;价格破发的原因是什么呢&#xff1f;价格破发与出资者有哪些联系呢&#xf…

Stephen Wolfram:那么…ChatGPT 在做什么,为什么它有效呢?

So … What Is ChatGPT Doing, and Why Does It Work? 那么…ChatGPT在做什么&#xff0c;为什么它有效呢&#xff1f; The basic concept of ChatGPT is at some level rather simple. Start from a huge sample of human-created text from the web, books, etc. Then train…

ChatGPT写论文不靠谱?但可处理简单任务如编写摘要或生成代码

日前&#xff0c;一项题为《水果蔬菜消费和体力活动对成年人糖尿病风险的影响》的学术研究论文发布。乍一看&#xff0c;这似乎是在学术期刊上又发表的一个调查。然而这篇论文并不常见&#xff0c;因为它是在ChatGPT的帮助下只用了不到一个小时的时间完成的。[1] 本文包括摘要…

护眼灯值不值得买?什么护眼灯对眼睛好

想要选好护眼台灯首先我们要知道什么是护眼台灯&#xff0c;大的方向来看&#xff0c;护眼台灯就是可以保护视力的台灯&#xff0c;深入些讲就是具备让灯发出接近自然光特性的光线&#xff0c;同时光线不会伤害人眼而出现造成眼部不适甚至是视力降低的照明设备。 从细节上看就…

日常BUG——代码提交到了本地但是没有push,删除了本地分支如何恢复

&#x1f61c;作 者&#xff1a;是江迪呀✒️本文关键词&#xff1a;日常BUG、BUG、问题分析☀️每日 一言 &#xff1a;存在错误说明你在进步&#xff01; 一、问题描述 代码在本地提交了&#xff0c;但是没有push到远程&#xff0c;然后删除了本地的分支。想要恢…

中国生产了5.07亿台,库存高达近4亿台?国产手机彻底卖不动了?

统计数据显示今年上半年中国的手机产量达到5.07亿台&#xff0c;国内市场手机出货量仅有1.24亿台&#xff0c;都出现了下滑&#xff0c;那么中国手机的产量比销量多出了3.83亿台&#xff0c;这些手机都成为了库存&#xff1f; 中国手机市场确实不如早年那么辉煌&#xff0c;201…

网盘与相册服务PDS

引言&#xff1a;作为一名开发者&#xff0c;我将通过对PDS&#xff08;Personal/Enterprise Drive System&#xff09;的体验使用&#xff0c;分享一下本人对以下方面的使用体验。 1. 开发个人/企业网盘 功能与应用 PDS作为一种网盘服务中间件产品&#xff0c;为开发者提供了…

常见程序搜索关键字转码

个别搜索类的网站因为用户恶意搜索出现误拦截情况&#xff0c;这类网站本身没有非法信息&#xff0c;只是因为把搜索关键字显示在网页中&#xff08;如下图&#xff09;&#xff0c;可以参考下面方法对输出的关键字进行转码 DEDECMS程序 本文针对Dedecms程序进行搜索转码&…

【腾讯云 TDSQL-C Serverless 产品体验】基于TDSQL-C 存储爬取的QQ音乐歌单数据

【腾讯云 TDSQL-C Serverless 产品体验】基于TDSQL-C 存储爬取的QQ音乐歌单数据 文章目录 【腾讯云 TDSQL-C Serverless 产品体验】基于TDSQL-C 存储爬取的QQ音乐歌单数据前言出现的背景一、TDSQL-C数据库是什么&#xff1f;二、TDSQL-C 的特点三、TDSQL-C的应用场景四、基于TD…

SpringBoot3入门第一章

现状 俗话说&#xff0c;买新不买旧&#xff0c;学技术也一样&#xff0c;学最新的技术&#xff0c;因为时代会给先行者最为优厚的回报 第一章 构建SpringBoot项目 这个因为太久没有用SpringBoot&#xff0c;导致好多东西都忘记了&#xff0c;就很烦 好在这里面东西并不多&…

【网络基础】网络通信

【网络基础】网络通信 文章目录 【网络基础】网络通信1、网络基础1.1 计算机网络1.2 网络模型TCP & UDP1&#xff09;IP地址2&#xff09;端口3&#xff09;TCP协议与UDP协议的比较 1.3 网络传输1.3.1 传输逻辑1.3.2 传输条件1.3.3 传输流程 1.4 地址管理 2、网络编程2.1 基…

React源码解析18(7)------ 实现事件机制(onClick事件)

摘要 在上一篇中&#xff0c;我们实现了useState的hook&#xff0c;但由于没有实现事件机制&#xff0c;所以我们只能将setState挂载在window上。 而这一篇主要就是来实现事件系统&#xff0c;从而实现通过点击事件进行setState。 而在React中&#xff0c;虽然我们是将事件绑…

ssm基于ssm的人才招聘网站源码和论文

ssm基于ssm的人才招聘网站源码和论文020 开发工具&#xff1a;idea 数据库mysql5.7 数据库链接工具&#xff1a;navcat,小海豚等 技术&#xff1a;ssm 选题依据&#xff08;研究的背景、目的和意义等&#xff09; 在Internet飞速发展的今天&#xff0c;互联网成为人们快…

如何保证数据传输的安全?

要确保数据传输的安全&#xff0c;您可以采取以下措施&#xff1a; 使用加密协议&#xff1a;使用安全的传输协议&#xff0c;如HTTPS(HTTP over SSL/TLS)或其他安全协议&#xff0c;以保护数据在传输过程中的安全性。加密协议可以有效防止数据被窃听或篡改。 强化身份验证&…

【从零学习python 】28. Python中的局部变量和全局变量

文章目录 局部变量全局变量全局变量和局部变量名字相同问题修改全局变量查看所有的全局变量和局部变量函数返回值一、多个return?二、一个函数返回多个数据的方式 对返回的数据直接拆包进阶案例 局部变量 局部变量&#xff0c;就是在函数内部定义的变量其作用范围是这个函数内…

Java-java中的类,main函数和文件名的关系

java中的类&#xff0c;main函数和文件名的关系 类与文件名的关系 一个java文件中可以定义多个类&#xff0c;但是最多只有一个类被public修饰&#xff0c;并且这个类的类名与文件名必须相同&#xff0c;若这个文件中没有public的类&#xff0c;则文件名随便是一个类的名字即可…