基于Mybatis+JUL+Lombok+Maven的图书管理系统(带单元测试)
项目需求:
- 在线录入学生信息和书籍信息
- 查询书籍信息列表
- 查询学生信息列表
- 查询借阅信息列表
- 完整的日志系统
-
package book_mange; import book_mange.entity.Book; import book_mange.entity.Student; import book_mange.util.SqlUtil; import lombok.extern.java.Log; import org.apache.ibatis.io.Resources; import java.io.IOException; import java.util.Scanner; import java.util.logging.LogManager; @Log public class Main { public static void main(String[] args) throws IOException { try(Scanner sc = new Scanner(System.in)){ LogManager manager = LogManager.getLogManager(); manager.readConfiguration(Resources.getResourceAsStream("logging.properties")); while (true){ System.out.println("===================================="); System.out.println("1.录入学生信息"); System.out.println("2.录入书籍信息"); System.out.println("3.添加借阅信息"); System.out.println("4.展示借阅信息"); System.out.println("5.展示学生信息"); System.out.println("6.展示书籍信息"); System.out.println("输入想要执行的操作,输入任意数字推出"); int input; try { input = sc.nextInt(); }catch (Exception e){ return; } switch (input){ case 1: addStudent(sc); break; case 2: addBook(sc); break; case 3: addBorrow(sc); break; case 4: showBorrow(); break; case 5: showStudentList(); break; case 6: showBookList(); break; default: return; } } } } /** * addStudent * @param sc */ private static void addStudent(Scanner sc){ System.out.println("请输入学生名字"); String name = sc.next(); System.out.println("请输入学生性别"); String sex = sc.next(); System.out.println("请输入学生年级"); int grade = sc.nextInt(); Student student = new Student(name,sex,grade); SqlUtil.doWork(bookMapper -> { int i = bookMapper.addStudent(student); if (i > 0){ System.out.println("学生信息录入成功"); log.info("插入成功" + student); }else{ System.out.println("学生信息录入失败,请重试!"); log.info("插入失败"); } }); } /** * addBook * @param sc */ private static void addBook(Scanner sc){ System.out.println("请输入书籍名称"); String title = sc.next(); System.out.println("请输入书籍简介"); String desc = sc.next(); System.out.println("请输入书籍价格"); double price = sc.nextDouble(); Book book = new Book(title,desc,price); SqlUtil.doWork(bookMapper -> { int i = bookMapper.addBook(book); if (i > 0){ System.out.println("书籍信息录入成功"); log.info("插入成功" + book); }else{ System.out.println("书籍信息录入失败,请重试!"); log.info("插入失败"); } }); } private static void addBorrow(Scanner sc){ System.out.println("请输入书籍号"); int bid = sc.nextInt(); System.out.println("请输入学生号"); int sid = sc.nextInt(); SqlUtil.doWork(bookMapper -> { int i = bookMapper.addBorrow(sid,bid); if (i > 0){ System.out.println("添加借阅信息录入成功"); log.info("添加借阅信息成功" + sid + bid); }else{ System.out.println("添加借阅信息录入失败,请重试!"); log.info("添加借阅信息录入失败"); } }); } private static void showBorrow(){ SqlUtil.doWork(bookMapper -> { bookMapper.getBorrowList().forEach(borrow -> { System.out.println(borrow.getStudent().getName() + "->" + borrow.getBook().getTitle()); log.info("展示借阅信息成功" + borrow.getStudent().getName() + "->" + borrow.getBook().getTitle()); }); }); } private static void showStudentList(){ SqlUtil.doWork(bookMapper -> { bookMapper.getStudentList().forEach(System.out::println); log.info("展示学生信息成功" ); }); } private static void showBookList(){ SqlUtil.doWork(bookMapper -> { bookMapper.getBookList().forEach(System.out::println); log.info("展示书籍信息成功" ); }); } }
- 运行方法 :在cmd界面用 java -jar 路径运行
评论