当前位置: 首页 > news >正文

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

一、案例要求

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

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

二、思路分析

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

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

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

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

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

1、创建数据库

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

字段名类型长度是否主键说明
idint11学生编号
usernamevarchar255学生姓名
passwordvarchar255学生密码
coursevarchar255学生班级

2、编写实体类

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

public class Student {//学生IDprivate 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模板中。

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/txhttp://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接口中声明查询所有用户信息的方法。

public interface StudentDao {//查询所有账户public List<Student> findAllStudent();

5、实现Dao层方法

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

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类,用于实现用户登录操作。

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 = newClassPathXmlApplicationContext("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类,在控制台按照提示输入账号密码进行登录。

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

相关文章:

  • 打开C# 大门:Hallo, World!
  • 【Pycharm】功能介绍
  • 【全网最简单的解决办法】vscode中点击运行出现仅当从 VS 开发人员命令提示符处运行 VS Code 时,cl.exe 生成和调试才可用
  • TypeScript基础教程学习
  • 算法训练营day52
  • Chapter 6 Frequency Response of Amplifiers
  • 代码随想录算法训练营第五十四 | ● 392.判断子序列 ● 115.不同的子序列
  • SpringBoot引入WebSocket依赖报ServerContainer no avaliable
  • centos官方yum源不可用 解决方案(随手记)
  • OBS 录屏软件:录制圆形头像画中画,设置卡通人像(保姆级教程,有步骤图,建议收藏)
  • 首届IEEE RAS峰会,为什么大厂阿里、字节、腾讯都参加了?
  • 让GNSSRTK不再难【第一天】
  • **《Linux/Unix系统编程手册》读书笔记24章**
  • L2-002 链表去重(C++)
  • PyTorch tutorials:快速学会使用PyTorch
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • bootstrap创建登录注册页面
  • CSS 专业技巧
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • JAVA 学习IO流
  • js面向对象
  • Leetcode 27 Remove Element
  • Magento 1.x 中文订单打印乱码
  • python学习笔记-类对象的信息
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • Sequelize 中文文档 v4 - Getting started - 入门
  • 程序员该如何有效的找工作?
  • 初识MongoDB分片
  • 从重复到重用
  • 第十八天-企业应用架构模式-基本模式
  • 来,膜拜下android roadmap,强大的执行力
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 批量截取pdf文件
  • 使用权重正则化较少模型过拟合
  • 通过几道题目学习二叉搜索树
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • Java数据解析之JSON
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • 我们雇佣了一只大猴子...
  • #Linux(Source Insight安装及工程建立)
  • #LLM入门|Prompt#3.3_存储_Memory
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (十六)Flask之蓝图
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .cfg\.dat\.mak(持续补充)
  • .gitattributes 文件
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .net 发送邮件
  • .NetCore 如何动态路由
  • .NET简谈设计模式之(单件模式)
  • .Net下C#针对Excel开发控件汇总(ClosedXML,EPPlus,NPOI)