// 实验存档。。。

效果图:

完整程序:https://pan.baidu.com/s/1-d1J90dkEtM0WKkABu0K0Q

提取码:hcnm

DAO层代码由MyBatis Generator生成,仅补充若干自定义代码。主要的代码只有下面这个:

package com.book;

import com.book.dao.BookMapper;
import com.book.dao.StudentMapper;
import com.book.dao.lendingRecordMapper;
import com.book.model.Book;
import com.book.model.Student;
import com.book.model.lendingRecord;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.Transactional; import java.util.List;
import java.util.Scanner; @SpringBootApplication
@MapperScan("com.book.dao")
public class MyApplication implements CommandLineRunner { @Autowired
private BookMapper bookMapper; @Autowired
private StudentMapper studentMapper; @Autowired
private lendingRecordMapper lendingRecordMapper; public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
} @Override
public void run(String... strings) throws Exception {
serve();
} private void serve() {
Scanner scan = new Scanner(System.in);
int command;
while (true) {
System.out.println("=======分===割===线================");
System.out.println("请问你要干嘛?");
System.out.println("1、管理书");
System.out.println("2、管理用户");
System.out.println("3、借书");
System.out.println("4、还书");
System.out.println("5、退出程序");
command = scan.nextInt();
if (command == 1) {
System.out.println("请问你下一步要干嘛?");
System.out.println("1、增书");
System.out.println("2、改书");
System.out.println("3、删书");
System.out.println("4、查书");
System.out.println("5、浏览书");
command = scan.nextInt();
if (command == 1) {
System.out.println("1、请你依次输入书的书名、作者、出版社、ISBN号、数量");
System.out.println("注意一行一个:");
scan.nextLine();
String name = scan.nextLine();
System.out.println("书名" + name + "已录入。");
String author = scan.nextLine();
String press = scan.nextLine();
String isbn = scan.nextLine();
int num = scan.nextInt();
Book book = new Book(name, author, press, isbn, num);
bookMapper.insert(book);
System.out.println(book + "已录入!!!");
} else if (command == 2) {
System.out.println("想改书?请填入书的id以及欲改字段");
System.out.print("书的id:");
long id = scan.nextLong();
System.out.print("书名:");
String name = scan.next();
System.out.print("作者:");
String author = scan.next();
System.out.print("出版社:");
String press = scan.next();
System.out.print("isbn:");
String isbn = scan.next();
System.out.print("数量:");
int num = scan.nextInt();
Book book = new Book(name, author, press, isbn, num);
book.setId(id);
System.out.println(book + "修改成功!!!");
// bookMapper.updateByPrimaryKeySelective();
} else if (command == 3) {
System.out.println("你想删书?请直接输入书的id:");
long id = scan.nextLong();
System.out.println("恭喜你,删除成功!!!");
} else if (command == 4) {
System.out.println("你想查书?请直接输入书的id:");
long id = scan.nextLong();
Book book = bookMapper.selectByPrimaryKey(id);
if (book == null) {
System.out.println("不好意思没有这本书");
} else {
System.out.println("找到这本书了,信息如下:");
System.out.println(book);
}
} else if (command == 5) {
List<Book> list = bookMapper.selectAll();
for (Book x : list) {
System.out.println(x);
}
}
} else if (command == 2) {
System.out.println("请问你下一步要干嘛?");
System.out.println("1、增用户");
System.out.println("2、改用户");
System.out.println("3、删用户");
System.out.println("4、查用户");
System.out.println("5、浏览用户");
command = scan.nextInt();
if (command == 1) {
System.out.println("1、请你依次输入用户的身份证号码、姓名、出生年月、性别");
System.out.println("注意一行一个:");
scan.nextLine();
String idCard = scan.nextLine();
System.out.println("idCard" + idCard + "已录入。");
String name = scan.nextLine();
String birth = scan.nextLine();
String sex = scan.nextLine();
Student student = new Student(name, sex, idCard, birth);
studentMapper.insert(student);
System.out.println(student + "已录入!!!");
} else if (command == 2) {
System.out.println("想改用户?请填入用户的id以及欲改字段");
System.out.print("用户的id:");
long id = scan.nextLong();
System.out.print("身份证号码:");
String idCard = scan.next();
System.out.print("姓名:");
String name = scan.next();
System.out.print("性别:");
String sex = scan.next();
System.out.print("出生年月:");
String birth = scan.next();
Student student = new Student(name, sex, idCard, birth);
student.setId(id);
System.out.println(student + "修改成功!!!");
// bookMapper.updateByPrimaryKeySelective();
} else if (command == 3) {
System.out.println("你想删用户?请直接输入用户的id:");
long id = scan.nextLong();
System.out.println("恭喜你,删除成功!!!");
} else if (command == 4) {
System.out.println("你想查用户?请直接输入用户的id:");
long id = scan.nextLong();
Student student = studentMapper.selectByPrimaryKey(id);
if (student == null) {
System.out.println("不好意思没有这个用户");
} else {
System.out.println("找到这个用户了,信息如下:");
System.out.println(student);
}
} else if (command == 5) {
List<Student> list = studentMapper.selectAll();
for (Student x : list) {
System.out.println(x);
}
}
} else if (command == 3) {
System.out.print("读者id:");
Long uid = scan.nextLong();
System.out.print("书的id:");
Long bookId = scan.nextLong();
System.out.print("借书本数:");
int num = scan.nextInt();
borrowBooks(uid, bookId, num);
} else if (command == 4) {
System.out.print("读者id:");
Long uid = scan.nextLong();
System.out.print("书的id:");
Long bookId = scan.nextLong();
returnBooks(uid, bookId);
} else if (command == 5) {
break;
}
}
} @Transactional
private void borrowBooks(Long uid, Long bookId, int num) {
Student student = studentMapper.selectByPrimaryKey(uid);
if (student == null) {
System.out.println("读者身份有错!!!请检查一遍再重新输入");
return;
}
Book book = bookMapper.selectByPrimaryKey(bookId);
if (book == null || book.getNum() < num) {
System.out.println("书籍不存在或者数量不够!!!");
return;
}
book.setNum(book.getNum() - num);
bookMapper.updateByPrimaryKey(book);
System.out.println("书籍数量已更新");
lendingRecord record = lendingRecordMapper.selectByUidAndBookId(uid, bookId);
if (record != null) {
// 如果借过同类的书,那就更新记录
record.setNum(record.getNum() + num);
lendingRecordMapper.updateByPrimaryKey(record);
} else {
// 如果没有,创建新记录
record = new lendingRecord(uid, bookId, num);
lendingRecordMapper.insert(record);
}
System.out.println("恭喜你!借书成功!");
} @Transactional
private void returnBooks(Long uid, Long bookId) {
// 可能出现Expected one result (or null) to be returned by selectOne(), but found: 2
// 需要保证数据干净
lendingRecord record = lendingRecordMapper.selectByUidAndBookId(uid, bookId);
if (record == null) {
System.out.println("不好意思,查不到该条借书记录~");
} else {
// 增加库存
Book book = bookMapper.selectByPrimaryKey(bookId);
book.setNum(book.getNum() + record.getNum());
bookMapper.updateByPrimaryKey(book);
// 删除记录
lendingRecordMapper.deleteByPrimaryKey(record.getId());
System.out.println("恭喜你,还书成功~~~");
System.out.println("^o^");
}
}
}

SpringBoot控制台版图书借阅程序的更多相关文章

  1. [Java] 将标准字符流写入到文件中(通过控制台写一个html程序,并保存)

    package test.stream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fi ...

  2. 转贴:修改springboot控制台输出的图案

    Post from:https://blog.csdn.net/WXN069/article/details/90667668 修改springboot控制台输出的图案1.在src\main\reso ...

  3. 清理忽略springboot控制台启动的banner和启动日志

    清理忽略springboot控制台启动的banner和启动日志 1.springboot的banner spring: main: banner-mode: off 2.mybatis-plus的ba ...

  4. springboot 控制台程序读取配置文件(原创)

    首先新建一个springboot项目,此处省略. 1.新建一个application.properties person.name=kevin person.age=6 person.sex=male ...

  5. 控制台手动编译Qt5程序

    转自:http://www.cnblogs.com/csulennon/p/4479236.html 在上一篇随笔中已经搭建好了Qt5的的开发环境,并且通过Qt Creator自动构建了一个视窗程序. ...

  6. .NET Core创建一个控制台(Console)程序

    .NET Core版本:1.0.0-rc2 Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2 开发及运行平台:Window ...

  7. Visual Studio 2012 开发环境配置+控制台工具+桌面应用程序

    一.界面布局视图设置 1.窗口的布局.控制台窗口运行恢复到开发环境的设置方法 也可以保存好设好的个性化设置,导入设置: 2.视图|服务器资源管理器(sever explorer) 可以访问数据源.服务 ...

  8. SpringBoot系列: 极简Demo程序和Tomcat war包部署

    =================================SpringBoot 标准项目创建步骤================================= 使用 Spring IDE( ...

  9. 自定义SpringBoot控制台输出的图案

    pringboot启动的时候,控制台输出的图案叫banner banner?啥玩意儿?相信有些人,一定是一脸懵逼... ——这个就不陌生了吧,这个是我们启动springboot的时候,控制台输出的.. ...

随机推荐

  1. 阿里云如何基于标准 K8s 打造边缘计算云原生基础设施

    作者 | 黄玉奇(徙远)  阿里巴巴高级技术专家 关注"阿里巴巴云原生"公众号,回复关键词 1219 即可下载本文 PPT 及实操演示视频. 导读:伴随 5G.IoT 的发展,边缘 ...

  2. C#写入(覆盖形式)数据到CSV文件 和 读取CSV文件

    /// <summary> /// 写入数据到CSV文件,覆盖形式 /// </summary> /// <param name="csvPath"& ...

  3. 《Dotnet9》系列-开源C# Winform控件库强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  4. 遇见一只黑猫,她说Python是个怪物

    导读: Python猫是一只喵星来客,它爱地球的一切,特别爱优雅而无所不能的 Python.我是它的人类朋友豌豆花下猫,被授权润色与发表它的文章.如果你是第一次看到这个系列文章,那我强烈建议,请先看看 ...

  5. go 利用chan的阻塞机制,实现协程的开始、阻塞、返回控制器

    一.使用场景 大背景是从kafka 中读取oplog进行增量处理,但是当我想发一条命令将这个增量过程阻塞,然后开始进行一次全量同步之后,在开始继续增量. 所以需要对多个协程进行控制. 二.使用知识 1 ...

  6. Maven pom.xml 全配置(二)不常用配置

    Maven pom.xml 全配置(二)不常用配置 这里贴出Maven pom.xml文件中使用率较少的配置参数,如果此篇文档中没有找到你想要的参数,移步Maven pom.xml 全配置(一)常用配 ...

  7. 爬虫(七):BeatifulSoup模块

    1. Beautiful Soup介绍 Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.能将即将要进行解析的源码加载到bs对象,调用bs对象中相关的方法或属性进 ...

  8. Javascript继承的问题

    说到Javascript的继承,相信只要是前端开发者都有所了解或应用,因为这是太基础的知识了.但不知各位有没有深入去理解其中的玄机与奥秘.今本人不才,但也想用自己的理解来说一说这其中的玄机和奥秘. 一 ...

  9. spring boot 2 + shiro 实现简单的身份验证例子

    Shiro是一个功能强大且易于使用的Java安全框架,官网:https://shiro.apache.org/. 主要功能有身份验证.授权.加密和会话管理.其它特性有Web支持.缓存.测试支持.允许一 ...

  10. Scrum Meeting - 第六周【Alpha阶段】

    每日任务内容: 本次会议为第六次Scrum Meeting会议 本次会议项目经理召开时间为20:00,在北区男生宿舍楼召开,召开时长约15分钟,探讨了本周选课网站编写的后续工作. 小组成员 本周任务 ...