通过所学的Spring数据库编程知识,实现学生管理系统的登录及增删改查的功能。要求学生在控制台输入用户名密码,如果用户账号密码正确则显示登录成功,如果登录失败则显示登录失败。登录成功后,可以进行增删改的功能。
思路分析:
根据学生管理系统及其登录要求,可以分析案例的实现步骤如下。
(1)为了存储学生信息,需要创建一个数据库。
(2)为了程序连接数据库并完成对数据的增删改查操作,需要在XML配置文件中配置数据库连接和事务等信息。
(3)在Dao层实现查询用户信息的方法及增删改查的方法。
(4)在Controller层(测试类)处理业务逻辑,如判断用户输入的用户名与密码是否正确。
下面来进入本次实验的编写:
首先进行Navicat数据库建立:
下图为工程的结构
首先打开pom文件,进行依赖的配置
如果依赖配置爆红,
出现下面爆红:
<artifactId>junit</artifactId>
<version>4.12</version>
可以点击右侧上方的Maven进行刷新,
如果只有version报错,此时需要把版本改为你已经下载的版本号
如果还是不行,很有可能是外部库重复下载导致版本不兼容,这个时候就需要到你配置maven的本地仓库:
把本地仓库的相关文件全部删除,之后再对maven进行刷新,重新下载外部库就OK了
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>org.example</groupId>
<artifactId>exper-6-test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
</dependencies>
</project>
之后进行application-student的配置:
在这个里面我也遇到了很多问题:
1.下面这个mybatis链接爆红,此时选择更多操作,应用外部资源,就OK了
2.数据库驱动出现问题:此时一定要记得cj,因为目前的版本,驱动Driver的路径为com.mysql.cj.jdbc.Driver
<!-- 数据库驱动 -->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
3.name="JdbcTemplate"爆红,原因是在后面的StudentDaoImpl中没有构造setJdbcTemplate方法
<?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"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!-- 连接数据库url -->
<property name="url" value="jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="StudentDao" class="cn.edu.ldu.dao.impl.StudentDaoImpl"> <property name="JdbcTemplate" ref="JdbcTemplate"/></bean>
<context:component-scan base-package="cn.edu.ldu.dao"></context:component-scan>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
编写Student类:
package cn.edu.ldu.entity;
public class Student {
private int id;
private String username;
private String password;
private String 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;
}
}
**编写StudentDao:**实现增删改查
package cn.edu.ldu.dao;
import cn.edu.ldu.entity.Student;
import java.util.List;
public interface StudentDao {
public List<Student> findAllStudent();
void insert(int id,String username,String password,String course);
void update(int id,String course);
void delete(int id);
}
**编写StudentDaoImpl:**对StudentDao的全部方法进行重写
package cn.edu.ldu.dao.impl;
import cn.edu.ldu.dao.StudentDao;
import cn.edu.ldu.entity.Student;
import org.springframework.dao.EmptyResultDataAccessException;
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);
}
@Override
public void delete(int id){
String sql = "delete from student where id=?";
jdbcTemplate.update(sql,id);
}
@Override
public void insert(int id, String username, String password, String course){
String sql = "insert into student(id,username,password,course) values(?,?,?,?)";
jdbcTemplate.update(sql,id,username,password,course);
}
@Override
public void update(int id,String course){
String sql="update student set course=? where id=?";
jdbcTemplate.update(sql,course,id);
}
}
StudentController的编写:
其中有个需要注意的点:
在输入id之后不要点击回车,直接空格写下一项内容,不然数据库的student表中会缺少一项内容,被回车字符替代
package cn.edu.ldu.controller;
import cn.edu.ldu.dao.StudentDao;
import cn.edu.ldu.dao.impl.StudentDaoImpl;
import cn.edu.ldu.entity.Student;
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("Welcome to student management system");
System.out.println("Please input the username:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Please input the password:");
String pwd = sc.nextLine();
ApplicationContext ac = new ClassPathXmlApplicationContext("application-student.xml");
//获取StudentDao实例
StudentDao studentDao = (StudentDao) ac.getBean("StudentDao");
List<Student> student = studentDao.findAllStudent();
Student stu = new Student();
int flag=0;
for(Student i:student){
if(i.getUsername().equals(name) && i.getPassword().equals(pwd)){
flag=1;
}
}
if(flag==1){
System.out.println("登陆成功!");
}
else{
System.out.println("用户名或密码错误,请重新登陆!");
}
if(flag==1){
String option;
System.out.println("请选择是否进入管理员模式?(Y/N)");
option = sc.nextLine();
while(option.equals("Y")){
System.out.println("请输入您想执行的操作:");
String op;
int id;
String user_name;
String pass_word;
String course_;
op = sc.nextLine();
if(op.equals("insert")){
System.out.println("请输入id,username,password,course:");
id = sc.nextInt();
user_name = sc.nextLine();
pass_word = sc.nextLine();
course_ = sc.nextLine();
studentDao.insert(id,user_name,pass_word,course_);
System.out.println("插入成功!");
}
else if(op.equals("delete")){
System.out.println("请输入你想删除的id:");
id = sc.nextInt();
user_name=sc.nextLine();
studentDao.delete(id);
System.out.println("删除成功!");
}
else if(op.equals("update")){
System.out.println("请输入你想更新的id和course:");
id = sc.nextInt();
course_ = sc.nextLine();
studentDao.update(id,course_);
System.out.println("更新成功!");
}
System.out.println("请选择是否继续执行管理员操作?(Y/N)");
option = sc.nextLine();
}
}
}
}
结果展示:
后记:
本次实验还是觉得自己的实际操作能力不是很好,过程也是十分艰辛,报了无数种奇奇怪怪的错,最终还好弄出来了,其中pom.xml和application-student.xml这两个很容易出现错误,一般一个很小的错误,就能花费好几个小时才能解决,总之,这次实验完成的很不容易,虽然在上交之前没有很好的完成,但是最后还是自己弄出来,就觉得自己已经很棒了,inin,继续加油!