JavaEE——声明式事务管理案例:实现用户登录

一、案例要求

本案例要求在控制台输入用户名密码,如果用户账号密码正确则显示用户所属班级,如果登录失败则显示登录失败。实现用户登录项目运行成功后控制台效果如下所示。

java 复制代码
欢迎来到学生管理系统
请输入用户名:
zhangsan
请输入zhangsan的密码:
123456
用户登录成功!
zhangsan是Java班的

二、思路分析

根据学生管理系统及其登录要求,可以分析案例的实现步骤如下。

(1)为了存储学生信息,需要创建一个数据库。

(2)为了程序连接数据库并完成对数据的增删改查操作,需要在XML配置文件中配置数据库连接和事务等信息。

(3)在Dao层实现查询用户信息的方法。

(4)在Controller层处理业务逻辑,如判断用户输入的用户名与密码是否正确 。

1、创建数据库

在MySQL中的spring数据库中创建一个名为student的表。

|----------|---------|-----|------|------|
| 字段名 | 类型 | 长度 | 是否主键 | 说明 |
| id | int | 11 | 是 | 学生编号 |
| username | varchar | 255 | 否 | 学生姓名 |
| password | varchar | 255 | 否 | 学生密码 |
| course | varchar | 255 | 否 | 学生班级 |

2、编写实体类

创建Student类,在该类中定义id、username、password和course属性,以及属性对应的getter/setter方法。

java 复制代码
public class Student {
    //学生ID
    private Integer id;
    //学生姓名
    private String username;
    //学生密码
    private String password;
    //学生班级
    private String course;

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

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

3、编写配置文件

创建配置文件applicationContext-student.xml,在该文件中配置id为dataSource的数据源Bean和id为jdbcTemplate的JDBC模板Bean,并将数据源注入到JDBC模板中。

java 复制代码
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       	    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       	    http://www.springframework.org/schema/tx
       	    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- 1.配置数据源 -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!--数据库驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <!--连接数据库的url -->
        <property name="url"
                  value="jdbc:mysql://localhost/spring?useSSL=false"/>
        <!--连接数据库的用户名 -->
        <property name="username" value="root"/>
        <!--连接数据库的密码 -->
        <property name="password" value="root"/>
    </bean>
    <!-- 2.配置JDBC模板 -->
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 默认必须使用数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 3.定义id为accountDao的Bean -->
    <bean id="studentDao" class="com.itheima.dao.impl.StudentDaoImpl">
        <!-- 将jdbcTemplate注入到AccountDao实例中 -->
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <!-- 4.事务管理器,依赖于数据源 -->
    <bean id="transactionManager" class=
            "org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 5.注册事务管理器驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

4、编写Dao层方法

创建StudentDao接口,在StudentDao接口中声明查询所有用户信息的方法。

java 复制代码
public interface StudentDao {
    //查询所有账户
    public List<Student> findAllStudent();

5、实现Dao层方法

创建StudentDaoImpl实现类,在StudentDaoImpl类中实现StudentDao接口中的findAllStudent()方法。

java 复制代码
public class StudentDaoImpl implements StudentDao {
    // 声明JdbcTemplate属性,省略了setter方法
    private JdbcTemplate jdbcTemplate;
    public List<Student> findAllStudent() {
        String sql = "select * from student";
        RowMapper<Student> rowMapper =
                new BeanPropertyRowMapper<Student>(Student.class);
        // 执行静态的SQL查询,并通过RowMapper返回结果
        return this.jdbcTemplate.query(sql, rowMapper);
    }}

6、编写Controller层

创建StudentController类,用于实现用户登录操作。

java 复制代码
public class StudentController {
    public static void main(String[] args) {
        System.out.println("欢迎来到学生管理系统");
        System.out.println("请输入用户名:");
        Scanner sca = new Scanner(System.in);
        String name = sca.nextLine();
        // 加载配置文件
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext-student.xml");
        // 获取AccountDao实例
        StudentDao studentDao =
                (StudentDao) applicationContext.getBean("studentDao");
        // 执行findAllAccount()方法,获取Account对象的集合
        List<Student> student = studentDao.findAllStudent();
        // 循环输出集合中的对象
        for (Student stu : student) {
            if (name.equals(stu.getUsername())) {
                System.out.println("请输入" + stu.getUsername() + "的密码:");
                String mima = sca.nextLine();
                if (mima.equals(stu.getPassword())) {
                    System.out.println("用户登录成功!");
                    System.out.println(stu.getUsername() + "是" + stu.getCourse() + "班的");
                    return;
                }
            } else {
                System.out.println("账号密码错误!");
                return;
            }
        }
    }
}

7、 查看运行结果

在IDEA中启动StudentController类,在控制台按照提示输入账号密码进行登录。

java 复制代码
欢迎来到学生管理系统
请输入用户名:
zhangsan
请输入zhangsan的密码:
123456
用户登录成功!
zhangsan是Java班的
相关推荐
Cyanto36 分钟前
Spring注解IoC与JUnit整合实战
java·开发语言·spring·mybatis
qq_4338889337 分钟前
Junit多线程的坑
java·spring·junit
gadiaola41 分钟前
【SSM面试篇】Spring、SpringMVC、SpringBoot、Mybatis高频八股汇总
java·spring boot·spring·面试·mybatis
写不出来就跑路43 分钟前
WebClient与HTTPInterface远程调用对比
java·开发语言·后端·spring·springboot
Cyanto1 小时前
深入MyBatis:CRUD操作与高级查询实战
java·数据库·mybatis
datascome1 小时前
文章发布易优CMS(Eyoucms)网站技巧
数据库·经验分享·爬虫·数据采集·eyoucms·易优cms
麦兜*1 小时前
Spring Boot 集成Reactive Web 性能优化全栈技术方案,包含底层原理、压测方法论、参数调优
java·前端·spring boot·spring·spring cloud·性能优化·maven
天上掉下来个程小白2 小时前
MybatisPlus-06.核心功能-自定义SQL
java·spring boot·后端·sql·微服务·mybatisplus
知了一笑2 小时前
独立开发第二周:构建、执行、规划
java·前端·后端
有想法的py工程师2 小时前
PostgreSQL 锁等待监控,查找等待中的锁
数据库