Java实现图书管理系统(基于数据库)

@TOC

1.概述

使用mysql、springboot、mybatis等技术,完成由数据库到线性表再到数据库的简单数据交互。

2. 实现

1. 构建springboot项目

在这里插入图片描述
在这里插入图片描述
在pom.xml中修改依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.book</groupId>
    <artifactId>BookSystem</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>BookSystem</name>
    <description>BookSystem</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2. 编写配置文件

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/book
    username: root
    password: rjj1125
logging:
  level:
    com: error

3. 构建映射类、服务类、线性表、实体类

在这里插入图片描述

  1. mapper
package com.book.mapper;

import com.book.pojo.Books;
import com.book.utils.SequenceList;
import org.apache.ibatis.annotations.*;

import java.awt.print.Book;
import java.util.List;

/**
 * @author rjj
 * @date 2022/9/6 - 20:38
 */
@Mapper
public interface BooksMapper{

    @Select("select * from books")
    List<Books> list();

    @Insert("insert into books value (#{isbn}, #{name}, #{author}, #{press}, #{price}) ")
    void updateDatabase(Books books);

    @Delete("delete from books")
    void clear();
}

  1. pojo
package com.book.pojo;

import lombok.Data;

/**
 * @author rjj
 * @date 2022/9/6 - 20:29
 */
@Data
public class Books {
    private Long isbn;
    private String name;
    private String author;
    private String press;
    private float price;
}

  1. service
package com.book.service;

import com.book.pojo.Books;

/**
 * @author rjj
 * @date 2022/9/8 - 17:50
 */
public interface BooksService{
    void init();
    String add(Books books);
    String delete(Long isbn);
    void end();
    String find(Long isbn);
    String update(Books book);
    String sort();
    String count();
    void show();
}

package com.book.service;

import com.book.mapper.BooksMapper;
import com.book.pojo.Books;
import com.book.utils.SequenceList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.Arrays;
import java.util.List;

/**
 * @author rjj
 * @date 2022/9/6 - 21:36
 */

@Service
public class BooksServiceImpl implements BooksService {

    @Resource
    private BooksMapper booksMapper;

    private SequenceList<Books> books;

    public void init() {
        //获取数据库中已有书的数据存入线性表中
        this.books = new SequenceList<Books>();
        List<Books> list = booksMapper.list();
        for (Books book : list) {
            books.insert(book);
        }
    }

    public String add(Books book) {
        //TODO 要添加的书的isbn是否已存在与表中
        //存在
        for (int i = 0; i < books.length(); i++) {
            if (book.getIsbn().equals(books.get(i).getIsbn()))
                return "该图书已存在!";
        }
        //不存在
        books.insert(book);
        return "已成功添加";
    }

    public String delete(Long isbn) {

        for (int i = 0; i < books.length(); i++) {
            if (isbn.equals(books.get(i).getIsbn())) {
                books.remove(i);
                return "删除成功!";
            }
        }
        return "不存在此书!";
    }

    public String find(Long isbn) {
        for (int i = 0; i < books.length(); i++) {
            if (isbn.equals(books.get(i).getIsbn())) {
                return books.get(i).toString();
            }
        }
        return "不存在此书!";
    }

    public String update(Books book) {
        for (int i = 0; i < books.length(); i++) {
            if (book.getIsbn().equals(books.get(i).getIsbn())) {
                books.update(i, book);
                return "修改成功!";
            }
        }
        return "不存在此书!";
    }

    public String sort() {
        for (int i = 0; i < books.length() - 1; i++) {
            for (int j = 0; j < books.length() - i - 1; j++) {
                if (books.get(j).getIsbn() > books.get(j + 1).getIsbn()) {
                    Books temp = books.get(j);
                    books.update(j, books.get(j + 1));
                    books.update(j + 1, temp);
                }
            }
        }
        return "排序成功!";
    }

    public String count(){
        return "已有图书"+books.length()+"个!";
    }

    public void show(){
        books.showAll();
    }

    public void end() {
        //先清空表中数据
        booksMapper.clear();
        //在将最新线性表加入数据库
        for (int i = 0; i < books.length(); i++) {
            booksMapper.updateDatabase(books.get(i));
        }
    }

}

  1. utils
public class SequenceList<T>{

    //创建数组
    private T[] ListArray;

    //记录当前元素个数
    private int N;

    public SequenceList(int capacity) {
        //初始化数组
        this.ListArray = (T[]) new Object[capacity];
        //初始化长度
        this.N = 0;
    }

    public SequenceList(){
        //定义默认长度
        this(10);
    }
    public void update(int i, T t){
        ListArray[i] = t;
    }

    public void clear() {
        this.N = 0;
    }

    public boolean isEmpty() {
        return this.N == 0;
    }

    public int length() {
        return N;
    }

    public T get(int i) {
        return ListArray[i];
    }

    public void insert(T t) {
        //位置不够进行扩容
        if (N == ListArray.length) {
            resize(ListArray.length * 2);
        }
        ListArray[N++] = t;
    }

    public void insert(int i, T t) {
        if (i<0 || i>N)
            throw new RuntimeException("插入位置不合法!");
        //位置不够进行扩容
        if (N == ListArray.length) {
            resize(ListArray.length * 2);
        }
        //先将i处元素及其后面元素依次向后移动一位
        for (int index = N - 1; index > i; index--) {
            ListArray[index] = ListArray[index - 1];
        }
        //将添加元素放入i索引处
        ListArray[i] = t;
        N++;
    }

    public T remove(int i) {
        if (i<0 || i>N)
            throw new RuntimeException("删除位置不合法!");
        T current = ListArray[i];
        //被删除元素后的每一个元素向前移动一位
        for (int index = i; index < N - 1; index++) {
            ListArray[index] = ListArray[index + 1];
        }
        N--;
        if (N > 0 && N < ListArray.length / 4) {
            resize(ListArray.length / 2);
        }
        return current;
    }

    //打印当前线性表的元素
    public void showAll() {
        for (int i = 0; i < N; i++) {
            System.out.println(ListArray[i] + " ");
        }
    }

    //改变容量
    private void resize(int newSize) {
        //记录旧数组
        T[] temp = ListArray;
        //创建新数组
        ListArray = (T[]) new Object[newSize];
        //把旧数组中的元素拷贝到新数组
        for (int i = 0; i < N; i++) {
            ListArray[i] = temp[i];
        }
    }

}
  1. 启动类
package com.book;

import com.book.pojo.Books;
import com.book.service.BooksService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Scanner;

@SpringBootApplication
public class BookSystemApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(BookSystemApplication.class, args);
        BooksService booksService = run.getBean(BooksService.class);

        System.out.println("--------------图书信息管理系统------------");
        System.out.println();
        Scanner sc = new Scanner(System.in);
        //初始化线性表,将数据库已有信息导入
        booksService.init();

        while (true){
            System.out.println("1.插入    2.删除    3.查找    4.修改    5.排序    6.计数    7.导入    8.保存    9.打印    10.退出");
            System.out.println();

            int choice = sc.nextInt();
            if (choice == 1) {
                Books book = new Books();

                System.out.println("请输入书籍isbn:");
                long isbn = sc.nextLong();
                book.setIsbn(isbn);

                System.out.println("请输入书籍名:");
                String name = sc.next();
                book.setName(name);

                System.out.println("请输入作者:");
                String author = sc.next();
                book.setAuthor(author);

                System.out.println("请输入出版社:");
                String press = sc.next();
                book.setPress(press);

                System.out.println("请输入价格");
                float price = sc.nextFloat();
                book.setPrice(price);

                System.out.println(booksService.add(book));
            }
            else if (choice == 2) {
                System.out.println("请输入删除书籍isbn:");
                Long isbns = sc.nextLong();
                System.out.println(booksService.delete(isbns));
            }
            else if (choice == 3) {
                System.out.println("请输入查找书籍的isbn:");
                long isbnss = sc.nextLong();
                System.out.println(booksService.find(isbnss));
            }
            else if (choice == 4) {
                Books subBook = new Books();
                System.out.println("请输入要修改书籍的isbn:");
                long subIsbn = sc.nextLong();
                subBook.setIsbn(subIsbn);

                System.out.println("请输入修改后书籍名:");
                String subName = sc.next();
                subBook.setName(subName);

                System.out.println("请输入修改后作者:");
                String subAuthor = sc.next();
                subBook.setAuthor(subAuthor);

                System.out.println("请输入修改后出版社:");
                String subPress = sc.next();
                subBook.setPress(subPress);

                System.out.println("请输入修改后价格:");
                float subPrice = sc.nextFloat();
                subBook.setPrice(subPrice);

                System.out.println(booksService.update(subBook));
            }
            else if (choice == 5) {
                System.out.println(booksService.sort());
            }
            else if (choice == 6) {
                System.out.println(booksService.count());
            }
            else if (choice == 7) {
                booksService.init();
                System.out.println("已成功导入!");
            }
            else if (choice == 8) {
                booksService.end();
                System.out.println("已成功保存!");
            }
            else if (choice == 9) {
                booksService.show();
            }
            else if (choice == 10){
                System.out.println("感谢使用本系统!");
                booksService.end();
                break;
            }
            else {
                System.out.println("输入不合法!");
            }

        }

    }

}

3. 效果

在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值