Mr.张小白(案例:基于Spring实现用户登录)

(102) 2024-03-28 16:01:01

基于Spring实现用户登录

一、步骤

1.引入相关依赖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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zjd</groupId>
    <artifactId>Part9</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2.导入数据库数据

1、新建数据库spring

CREATE DATABASE spring;

2、在spring数据库下创建表student

create table student(
    id int primary key auto_increment,
    username varchar not null ,
    password varchar not null ,
    course varchar
);

3、在student表中插入数据

insert into s_student(username, password, course) VALUES
('张三','123','计算机')

3.创建Spring核心配置文件applicationContext.xml

<?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:tx="http://www.springframework.org/schema/tx"
       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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/spring?useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="studentDao" class="com.zjd.StudentDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

4.创建student的实体类Student.java

package com.zjd;

public class Student { 
   
    private int id;
    private String username;
    private String password;
    private String course;

    @Override
    public String toString() { 
   
        return "Student{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", course='" + course + '\'' +
                '}';
    }

    public int getId() { 
   
        return id;
    }

    public void setId(int 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 getCourse() { 
   
        return course;
    }

    public void setCourse(String course) { 
   
        this.course = course;
    }
}

5.创建Student的Dao层接口文件StudentDao.java

package com.zjd;

import java.util.List;

public interface StudentDao { 
   
    public List<Student> findAllStudent();
}

6.创建Student的Dao层接口文件的实现类StudentDaoImpl.java

package com.zjd;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class StudentDaoImpl implements StudentDao { 
   
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 
   
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Student> findAllStudent() { 
   
        String sql="select * from student";
        RowMapper<Student> rowMapper=new BeanPropertyRowMapper<Student>(Student.class);
        return this.jdbcTemplate.query(sql,rowMapper);
    }
}

7.将Student的Dao层实现类加载到Spring的Bean对象中,并开始事务管理

<!--  上图我已加载过  -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="studentDao" class="com.zjd.StudentDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

8.创建Student实体类的Controller层测试运行结果

package com.zjd;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Scanner;

public class StudentController { 
   
    public static void main(String[] args) { 
   
        System.out.println("欢迎来到学生管理系统");
        System.out.println("输入用户名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao studentDao = (StudentDao) applicationContext.getBean("studentDao");
        List<Student> students = studentDao.findAllStudent();
        for (Student student : students
        ) { 
   
            System.out.println(name.equals(student.getUsername()));
            if (name.equals(student.getUsername())) { 
   
                System.out.println("请输入" + student.getUsername() + "的密码:");
                String password = scanner.nextLine();
                if (password.equals(student.getPassword())) { 
   
                    System.out.println("用户登录成功");
                    System.out.println(student.getUsername() + "是" + student.getCourse() + "班的");
                    return;
                } else { 
   
                    System.out.println("密码错误");
                    return;
                }
            } else { 
   
                System.out.println("账号错误");
                return;
            }
        }
    }
}

二、项目结构

Mr.张小白(案例:基于Spring实现用户登录) (https://mushiming.com/)  第1张

三、本次完成案例:基于Spring实现用户登录

通过所学的Spring数据库编程知识,实现学生管理系统的登录功能。

本案例要求:

(1)学生在控制台输入用户名和密码,如果用户名和密码正确,则显示用户所属班级;如果登陆失败则显示登陆失败。

四、重要讯息!!!!!

各位小伙伴有疑问可以私聊我,我会在每周日统一查看回复

路过的小伙伴,该篇文章如果对你有帮助,请留下你的小手(👍)再走哦,

五、下方评论区见该案例源代码

您的支持将是我一直做下去的不竭动力!!!

THE END

发表回复