Mybatis——实现对学生成绩表的查询及添加功能(IDEA)

Mybatis——实现对学生成绩表的查询及添加功能(IDEA),第1张

题目:

使用Mybatis,实现对学生成绩表的查询及添加功能,程序运行后,文本终端主菜单界面如下:
          学生成绩管理
          1 学生成绩查询
          2 学生成绩添加
          3 退出
          请输入选择:
   如果输入1,则提示“请输入学号”,然后根据学号查询学生表中相应信息,查到则显示学生姓名及成绩,查不到则显示“查无此人”,最后返回主菜单;如果输入2,则提示输入学号、学生姓名、成绩,如果添加成功,提示“添加成功”,否则提示“添加失败”,不能重复添加学号相同的记录。选择3,则直接退出程序。

第一种方法:传统开发方式

package com.itheima;

import com.itheima.dao.impl.StuDaoImpl;
import com.itheima.domain.Student;

import java.util.Scanner;

public class Main {

    /*
     * 遇到的问题:
     * 1. cannot find class : student    没有在mybatis的核心配置文件中给Student类配置别名
     * 2. 没有为stuMapper配置加载映射文件
     * 3. 通过select count(*) from stu_score where name=#{name}来查询同name的数据个数
     * 4. 如何使控制台进程不结束得以继续运行其他的命令程序  do-while
     *
     * */


    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.queryByNumber(number);
        if (row == 1) {
            System.out.println("添加失败");
        } else {
            stuDao.insertStu(student);
            Student record = stuDao.findByNumber(number);
            System.out.println("     学号:    " + record.getNumber() + "     学生姓名:   " + record.getName() + "     成绩:     " + record.getScore());
            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 {
            while (Integer.parseInt(command) == 1) {
                first();
                title();
                command = input.nextLine();
            }
            while (Integer.parseInt(command) == 2) {
                second();
                title();
                command = input.nextLine();
            }
            while (Integer.parseInt(command) == 3) {
                third();
                break;
            }
        } while (command == null);
    }
}
遇到的问题:
  1. cannot find class : student    没有在mybatis的核心配置文件中给Student类配置别名

  2. 没有为stuMapper配置加载映射文件

  3. 通过select count(*) from stu_score where name=#{name}来查询同name的数据个数

  4. 如何使控制台进程不结束得以继续运行其他的命令程序  do-while

  5. 如何使输出时控制台只出现输入输出的数据,没有连接数据库的日志信息
     SqlMapConfig.xml
      //在控制台展示连接数据库等日志信息(必须放在下面第一个)
      
          
      

  6. 控制台出现log4j:WARN No appenders could be found for logger                                             (org.apache.ibatis.logging.LogFactory).
      pom.xml文件中删除log4j依赖

第二种方法:代理开发方式

package com.itheima;

import com.itheima.dao.PeopleMapper;
import com.itheima.dao.UserMapper;
import com.itheima.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PeopleMapper um = sqlSession.getMapper(PeopleMapper.class); //根据类类型获取实例

        System.out.println("学生成绩管理");
        System.out.println("1 学生成绩查询");
        System.out.println("2 学生成绩添加");
        System.out.println("3 退出");

        Scanner scanner = new Scanner(System.in);
        String control = scanner.nextLine();

        do {
            while (Integer.parseInt(control) == 1){
                System.out.println("请输入学号");
                String number = scanner.nextLine();
                Student record = um.findByNumber(number);

                if (record != null) {
                    System.out.println("学生姓名:" + record.getName() + "成绩:" + record.getScore());
                } else {
                    System.out.println("查无此人");
                }
                System.out.println("学生成绩管理");
                System.out.println("1 学生成绩查询");
                System.out.println("2 学生成绩添加");
                System.out.println("3 退出");
                control = scanner.nextLine();
            }
            while (Integer.parseInt(control) == 2){
                System.out.println("学号");
                String number = scanner.nextLine();
                System.out.println("学生姓名");
                String name = scanner.nextLine();
                System.out.println("成绩");
                String score = scanner.nextLine();

                Student student = new Student();
                student.setNumber(number);
                student.setName(name);
                student.setScore(Float.parseFloat(score));

                int row = um.findNumber(number);
                if (row == 1) {
                    System.out.println("添加失败");
                } else {
                    um.insert(student);
                    sqlSession.commit();
                    Student record = um.findByNumber(number);
                    sqlSession.close();
                    System.out.println("学号:" + record.getNumber() + "学生姓名:" + record.getName() + "成绩:" + record.getScore());
                    System.out.println("添加成功");
                }
                System.out.println("学生成绩管理");
                System.out.println("1 学生成绩查询");
                System.out.println("2 学生成绩添加");
                System.out.println("3 退出");
                control = scanner.nextLine();
            }
            while (Integer.parseInt(control) == 3){
                System.out.println("退出程序");
                break;
            }
        } while (control == null);
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/924651.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存