广告
返回顶部
首页 > 资讯 > 后端开发 > Python >基于Spring5实现登录注册功能
  • 852
分享到

基于Spring5实现登录注册功能

2024-04-02 19:04:59 852人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

本文实例为大家分享了spring5实现登录注册功能的具体代码,供大家参考,具体内容如下 准备: 根据分析用户注册登录都需要的信息为①username(String)②userid(I

本文实例为大家分享了spring5实现登录注册功能的具体代码,供大家参考,具体内容如下

准备:

根据分析用户注册登录都需要的信息为
①username(String)
②userid(Int)
③userpassword(String)
④useremail(String)

1.生成数据库、表

2.编写实体类

import org.springframework.stereotype.Component;
@Component
public class User {
    private String UserName;
    private int UserId;
    private String UserPassWord;
    private String UserEmail;

    public User(String userName, int userId, String userPassWord, String userEmail) {
        UserName = userName;
        UserId = userId;
        UserPassWord = userPassWord;
        UserEmail = userEmail;
    }

    public User() {
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public int getUserId() {
        return UserId;
    }

    public void setUserId(int userId) {
        UserId = userId;
    }

    public String getUserPassWord() {
        return UserPassWord;
    }

    public void setUserPassWord(String userPassWord) {
        UserPassWord = userPassWord;
    }

    public String getUserEmail() {
        return UserEmail;
    }

    public void setUserEmail(String userEmail) {
        UserEmail = userEmail;
    }
}

3.配置xml文件(jdbcTemplate注入到dao层中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="Http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描包-->
    <context:component-scan base-package="MyPackage"></context:component-scan>
    <!--数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:Mysql://localhost:3306/review"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    </bean>
    <!--配置JdbcTemplate对象,把数据库dataSource注入进去-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--要把数据库注入到JdbcTemplate对象中-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--到了这一步我们就连接好了数据库了-->
</beans>

4.编写Userdao(dao层)

import MyPackage.pojo.User;
import org.springframework.stereotype.Component;
@Component
public interface UserDao {
    void ReGISter(User user);

    User login(Integer id);
}

UserDaoImpl类

ckage.dao;

import MyPackage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class UserDaoImpl implements UserDao{

    @Autowired
    JdbcTemplate jdbcTemplate;//自动注入数据库

    @Override
    public void Register(User user) {
        String sql = "insert into t_user (`username`,`userid`,`userpassword`,`useremail`)" +
                "values(?,?,?,?)";
        Object[] args = {user.getUserName(), user.getUserId(), user.getUserPassWord(), user.getUserEmail()};
        int update = jdbcTemplate.update(sql,args);
        System.out.println("注册成功:"+update);
    }

    @Override
    public User login(Integer id) {
        String sql = "select *from t_user where `userid`=?";
        User user1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user1;
    }
}

5.编写service层

import MyPackage.dao.UserDao;
import MyPackage.dao.UserDaoImpl;
import MyPackage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "userService")
public class UserService {
    @Autowired
    public UserDao userDao;
    //用户注册
    public void Register(User user){
        System.out.println("Service Register.....");
        userDao.Register(user);
    }

    //用户登录
    public void Login(User user){
        System.out.println("Service Login.....");
        User loginsuccess = userDao.login(user.getUserId());
        if (loginsuccess!=null)
            System.out.println("登录成功");
        else
            System.out.println("登录失败");
    }
}

6.测试

import MyPackage.pojo.User;
import MyPackage.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    //注册功能测试
    @org.junit.Test
    public void testJdbcTemplate1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = (UserService) context.getBean("userService", UserService.class);
//        userService.Register(new User("mary",2,"654321","654321@qq.com"));
        userService.Register(new User("lucy",1,"123456","123456@qq.com"));
    }
    //登录功能测试
    @org.junit.Test
    public void testJdbcTemplate2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = (UserService) context.getBean("userService", UserService.class);
        userService.Login(new User("lucy",1,"123456","123456@qq.com"));
    }
}

运行截图

遇到的问题:

刚开始把bean1.xml配置文件直接是在src目录下生成的,所以当运行时,就会发现找不到xml文件
解决方法:

在main目录下新建一个resources文件,且把该文件变成Resources类型,然后把bean1.xml放入在这里就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: 基于Spring5实现登录注册功能

本文链接: https://www.lsjlt.com/news/166934.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 基于Spring5实现登录注册功能
    本文实例为大家分享了Spring5实现登录注册功能的具体代码,供大家参考,具体内容如下 准备: 根据分析用户注册登录都需要的信息为①username(String)②userid(I...
    99+
    2022-11-13
  • Android基于Sqlite实现注册和登录功能
    Android中基于Sqlite实现注册和登录功能,供大家参考,具体内容如下 前言 写这篇博客主要是为了巩固一下学的Sqlite知识以及梳理一下这个项目的逻辑 实现逻辑 项目的图片...
    99+
    2022-11-13
  • 基于struts2和hibernate实现登录和注册功能
    本文实例为大家分享了struts2和hibernate实现登录和注册功能,供大家参考,具体内容如下该项目使用MySQL数据库,数据库名为test,表名info,如图所示: 2、配置web.xml(Struts2使用) &...
    99+
    2023-05-30
    struts2 hibernate 登录
  • Java基于IO流实现登录和注册功能
    案例分析: 我们之前做过的登录注册案例是把用户信息存进集合里,要用IO流实现的话,就是要把用户信息存入文件中。登录注册两个功能的具体实现是在用户操作类中,所以我们只需要在用户操作类中...
    99+
    2022-11-13
  • 基于Java怎么实现QQ登录注册功能
    这篇文章主要介绍“基于Java怎么实现QQ登录注册功能”,在日常操作中,相信很多人在基于Java怎么实现QQ登录注册功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”基于Java怎么实现QQ登录注册功能”的疑...
    99+
    2023-06-30
  • Android基于Sqlite怎么实现注册和登录功能
    本篇内容主要讲解“Android基于Sqlite怎么实现注册和登录功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android基于Sqlite怎么实现注册和登录功能”吧!实现逻辑项目的图片结...
    99+
    2023-06-30
  • Pythontkinter库实现登录注册基本功能
    目录一、各种组件的布局二、制作过程中的理解三、制作过程中遇到的难点四、解决问题的方法tkinter库作为python的标准库之一,它的功能性十分强大,下面我将使用tkinter库制作...
    99+
    2022-12-30
    Python tkinter登录注册 Python tkinter Python登录注册
  • 基于Java实现QQ登录注册功能的示例代码
    目录前言实现代码登录页面注册页面效果展示前言 本文主要应用的技术有:GUI、JDBC、多线程 实现的功能具体如下: 1、登录功能 2、注册功能 3、是否隐藏密码的选择以及实现功能 4...
    99+
    2022-11-13
  • Python实现注册登录功能
    用Python写个注册登录功能,供大家参考,具体内容如下 本文是用Python写一个注册登录功能,难度不大,很适合练手主要就是用列表和字典,以及逻辑判断用到的第3方库模块是time模...
    99+
    2022-11-13
  • Node.js实现登录注册功能
    本文实例为大家分享了Node.js实现登录注册功能的具体代码,供大家参考,具体内容如下 目录结构 注册页面: reg.html <!DOCTYPE html> <...
    99+
    2022-11-13
  • Android实现登录注册功能
    本文实例为大家分享了Android实现登录注册功能的具体代码,供大家参考,具体内容如下 运行环境 Android Studio 总体效果图 一、 设计注册页面的布局 二、完成注册...
    99+
    2022-11-13
  • python实现登录与注册功能
    本文实例为大家分享了python实现登录与注册的具体代码,供大家参考,具体内容如下 1. 案例介绍 本例设计一个用户登录和注册模块,使用 Tkinter 框架构建界面,主要用到画布、...
    99+
    2022-11-12
  • NodeJs+MySQL实现注册登录功能
    本文实例为大家分享了NodeJs+MySQL实现注册登录功能的具体代码,供大家参考,具体内容如下 之前写过一个没有连接数据库的注册与登陆的实现,这次加上了数据库 刚刚接触后端,很多不...
    99+
    2022-11-13
  • 基于Viewpager2实现登录注册引导页面
    本文实例为大家分享了Viewpager2实现登录注册引导页面的具体代码,供大家参考,具体内容如下 介绍 屏幕滑动是两个完整屏幕之间的切换,在设置向导或幻灯片等界面中很常见 实现图(图...
    99+
    2022-11-13
  • android登录注册功能如何实现
    要实现Android的登录注册功能,你可以按照以下步骤进行操作:1. 创建一个布局文件来设计登录和注册界面。可以使用EditText...
    99+
    2023-10-20
    android
  • Spring MVC+mybatis实现注册登录功能
    本文实例为大家分享了Spring MVC mybatis实现注册登录功能的具体代码,供大家参考,具体内容如下前期准备: 如下图所示,准备好所需要的包 新建工程,导入所需要的包,在web.xml中配置好所需要的,如下<...
    99+
    2023-05-31
    spring mvc mybatis
  • java+mysql实现登录和注册功能
    初学JAVA  EE,老师留下一小作业,用JAVA实现与服务器端交互,实现登录和注册功能,初学一种专业课很多老师都会留下一种让学生实现登录和注册的作业。 下面是记录的实现步...
    99+
    2022-11-13
  • Android实现登录注册功能封装
    我们都知道Android应用软件基本上都会用到登录注册功能,那么对一个一个好的登录注册模块进行封装就势在必行了。这里给大家介绍一下我的第一个项目中所用到的登录注册功能的,已经对...
    99+
    2022-06-06
    封装 Android
  • node.js实现简单登录注册功能
    本文实例为大家分享了node.js实现简单登录注册的具体代码,供大家参考,具体内容如下 1、首先需要一个sever模块用于引入路由,引入连接数据库的模块,监听服务器2、要有model...
    99+
    2022-11-13
  • QT实现用户登录注册功能
    本文实例为大家分享了QT实现用户登录注册的具体代码,供大家参考,具体内容如下 1、login.h #ifndef LOGIN_H #define LOGIN_H #include ...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作