题目:
使用Mybatis,实现对学生成绩表的查询及添加功能,程序运行后,文本终端主菜单界面如下:
学生成绩管理
1 学生成绩查询
2 学生成绩添加
3 退出
请输入选择:
如果输入1,则提示“请输入学号”,然后根据学号查询学生表中相应信息,查到则显示学生姓名及成绩,查不到则显示“查无此人”,最后返回主菜单;如果输入2,则提示输入学号、学生姓名、成绩,如果添加成功,提示“添加成功”,否则提示“添加失败”,不能重复添加学号相同的记录。选择3,则直接退出程序。
import com.itheima.dao.impl.StuDaoImpl;
import com.itheima.domain.Student;
import java.util.Scanner;
public class Main {
static void title() {
System.out.println("学生成绩管理");
System.out.println("1 学生成绩查询");
System.out.println("2 学生成绩添加");
System.out.println("3 退出");
}
static void first() {
System.out.println("请输入学号");
StuDaoImpl stuDao = new StuDaoImpl();
Scanner input = new Scanner(System.in);
String number = input.nextLine();
Student record = stuDao.findByNumber(number);
if (record != null) {
System.out.println("学生姓名:" + record.getName() + "成绩:" + record.getScore());
} else {
System.out.println("查无此人");
}
}
static void second() {
StuDaoImpl stuDao = new StuDaoImpl();
Scanner input = new Scanner(System.in);
System.out.println("学号");
String number = input.nextLine();
System.out.println("学生姓名");
String name = input.nextLine();
System.out.println("成绩");
String score = input.nextLine();
Student student = new Student();
student.setNumber(number);
student.setName(name);
student.setScore(Float.parseFloat(score));
int row = stuDao.findByName(name);
if (row == 0) {
stuDao.insertStu(student);
Student record = stuDao.findByNumber(number);
System.out.println("学号:" + record.getNumber() + "学生姓名:" + record.getName() + "成绩:" + record.getScore());
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
static void third() {
System.out.println("退出程序");
}
public static void main(String[] args) {
title();
Scanner input = new Scanner(System.in);
String command = input.nextLine();
do {
if (Integer.parseInt(command) != 3){
while (Integer.parseInt(command) == 0){
title();
continue;
}
while (Integer.parseInt(command) == 1){
first();
title();
command = input.nextLine();
}
while (Integer.parseInt(command) == 2){
second();
title();
command = input.nextLine();
}
}else if (Integer.parseInt(command) == 3){
third();
break;
}
} while (Integer.parseInt(input.nextLine()) == 4);
}
}
遇到的问题:
1. cannot find class : student 没有在mybatis的核心配置文件中给Student类配置别名
2. 没有为stuMapper配置加载映射文件
3. 通过select count(*) from stu_score where name=#{name}来查询同name的数据个数
4. 如何使控制台进程不结束得以继续运行其他的命令程序 do-while
5. 直接退出可以,但是在运行一个循环之后再退出不行了(未解决)



