文章目录
- Mybaits+maven实现xxx管理系统(图书管理系统为例)
- 1. 项目简介
- 2. 运行效果
- 3. 项目搭建
- 3.1 数据库创建
- 3.2 idea项目搭建
- 3.2.1 创建项目
- 3.2.2 Mybatis插件
- 3.2.3 文件配置
- 4. 完整项目代码
- 4.1 pojo
- 4.2 mapper接口
- 4.3 xml映射文件
- 4.4 界面搭建+功能实现
- 5. 项目完整下载
Mybaits+maven实现xxx管理系统(图书管理系统为例)
1. 项目简介
使用maven+mybatis实现xxx管理系统,在终端进行与MYSQL数据库的交互,可用作无需前端要求的课程设计(包括:数据库课程设计、java课程设计、c++课程设计、面向对象课程设计、软件工程课程设计)。对于学习mybatis的xml映射有一定的帮助
2. 运行效果
此处采用的为:图书管理系统的增删改查
3. 项目搭建
3.1 数据库创建
使用navicat连接mysql 快速搭建数据库,并创建表,添加数据
-
创建数据库
-
创建三个表:books_table (图书表 ) loans_table (借阅表) readers_table (读者表)
-
books_table
-
loans_table
-
readers_table
-
-
添加表字段
-
添加表外键关联
-
添加索引成功后
-
3.2 idea项目搭建
3.2.1 创建项目
3.2.2 Mybatis插件
设置中 安装插件,方便进行跳转,安装后需要重启
3.2.3 文件配置
-
配置pom.xml依赖文件 不需要添加依赖时不用更改
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>mybatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatis</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!--添加lombom依赖,避免代码繁多--> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> <!--添加Mybatis框架的依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!--添加mysql的依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> <!--junit单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
-
main 文件中新建resources资源包
资源包 中新建mybatis-config.xml文件
用来管理数据库信息(需要更改数据库信息,映射文件信息(直接改包扫描就不用一个个添加))
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 开启驼峰,开启后,只要数据库字段和对象属性名字母相同,无论中间加多少下划线都可以识别 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库连接信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mylibrary?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载sql映射文件-->
<!-- <mapper resource="com/example/mapper/ReaderMapper.xml"/>-->
<!--包扫描方式-->
<package name="com.example.mapper"/>
</mappers>
</configuration>
-
注意此处为自己的mysql数据库连接信息,需要修改为自己数据库的信息
数据库版本为8+ 的连接方式
注意:5版本和8版本的连接方式不同!!!!!!!!!!!!
可以先去idea中自带的数据库连接工具先进行连接,看看能不能成功
(此处成功只是为了方便操作,还要需要根据自己数据库版本进行连接)
到此项目配置已经完成,具体配置可前往步骤4进行构建完成代码,也可以先构建框架
-
com.example创建pojo文件
用来用来保存javabean,对应着数据库创建
使用lombom通过注解自动生成构造器
-
使用mapper代理开发
com.example 文件包中新建mapper用于添加mapper接口
注意:创建的是接口,不是类。接口!接口!接口!
-
添加代理开发的映射
resources文件夹中 新建同包同名文件(com.example.mapper),使得编译后xml和接口在一个文件夹中
注意创建方式
-
添加代理开发的映射
resources文件夹中 新建同包同名 文件,使得编译后xml和接口在一个文件夹中
xml映射文件中暂时添加代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.ReaderMapper">
</mapper>
其中namespace需要改为接口的全类名
右键复制路径就可以
如果有返回值类型,resultType改为javabean全类名(即pojo里面的类名字)
-
构建结果如下
4. 完整项目代码
4.1 pojo
创建javabean类,根据数据库中的表来进行创建,用作后续的返回对象
-
Book
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Book { private Integer bookID; private String title; private String author; private String publisher; }
-
Loan
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDateTime; @Data @NoArgsConstructor @AllArgsConstructor public class Loan { private Integer readerId; private Integer bookId; private LocalDateTime loanData; private LocalDateTime returnTime; }
-
Reader
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Reader { private Integer readerId; private String name; private String address; private String phone; }
-
BorrowwInf(此对象用作后续操作)
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class BorrowInf { private Integer readerId; private String name; private Integer bookId; private String title; private String author; }
4.2 mapper接口
-
BookMapper
package com.example.mapper; import com.example.pojo.Book; import java.util.ArrayList; public interface BookMapper { ArrayList<Book> selectAll(); //根据id搜寻具体书信息 Book searchByid(int bookId); //添加书籍 void addBook(Book book); //根据id更新书 void updateById(Book book); //删除书籍 void deleteById(int id); }
-
ReaderMapper
package com.example.mapper; import com.example.pojo.Reader; import java.util.ArrayList; public interface ReaderMapper { //查询所有 ArrayList<Reader> selectAll(); //搜寻单个 Reader selectById(int id); //添加读者信息 void addReader(Reader reader); //更新读者信息 void updateById(Reader reader); //删除信息 void deleteByid(int id); }
-
LoanMapper
package com.example.mapper; import com.example.pojo.Book; import com.example.pojo.Loan; import java.util.ArrayList; public interface LoanMapper { ArrayList<Loan> selectAll(); void addLoan(); }
-
BorrowInfMapper
package com.example.mapper; import com.example.pojo.BorrowInf; import org.apache.ibatis.annotations.Param; import java.util.ArrayList; public interface BorrowInfMapper { ArrayList<BorrowInf> searchAll(); }
4.3 xml映射文件
-
BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.BookMapper"> <insert id="addBook"> INSERT into books_table(book_id, title, author, publisher) VALUES (#{bookID},#{title},#{author},#{publisher}) </insert> <update id="updateById"> UPDATE books_table SET title=#{title}, author=#{author}, publisher=#{publisher} WHERE book_id = #{bookID} </update> <delete id="deleteById"> DELETE FROM books_table WHERE book_id = #{bookId} </delete> <select id="selectAll" resultType="com.example.pojo.Book"> select * from books_table </select> <select id="searchByid" resultType="com.example.pojo.Book"> SELECT *FROM books_table WHERE book_id=#{bookId} </select> </mapper>
添加成功后,可以出现蓝鸟,点击可以进行跳转
-
BorrowInfMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.BorrowInfMapper"> <select id="searchAll" resultType="com.example.pojo.BorrowInf"> SELECT loans_table.reader_id, readers_table.name, loans_table.book_id, books_table.title, books_table.author FROM readers_table, loans_table, books_table WHERE readers_table.reader_id = loans_table.reader_id AND books_table.book_id=loans_table.book_id </select> </mapper>
-
LoanMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.LoanMapper"> <insert id="addLoan"> INSERT into loans_table(reader_id, book_id, loan_data, return_time) VALUES (#{readerId},#{bookId},#{loanData},#{returnTime}) </insert> <select id="selectAll" resultType="com.example.pojo.Loan"> select * from loans_table </select> </mapper>
-
ReaderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.ReaderMapper"> <insert id="addReader"> INSERT into readers_table(reader_id,name,address,phone) VALUES(#{readerId},#{name},#{address},#{phone}) </insert> <update id="updateById"> UPDATE readers_table SET name=#{name}, address=#{address}, phone=#{phone} WHERE reader_id = #{readerId} </update> <delete id="deleteByid"> DELETE FROM readers_table WHERE reader_id=#{readerId} </delete> <select id="selectAll" resultType="com.example.pojo.Reader"> select * from readers_table </select> <select id="selectById" resultType="com.example.pojo.Reader"> SELECT *FROM readers_table WHERE reader_id=#{reader_id} </select> </mapper>
4.4 界面搭建+功能实现
com.example中创建如下文件
-
LibrarySystem.java
package com.example; import java.io.IOException; import java.util.Scanner; public class LibrarySystem { public LibrarySystem() throws IOException { 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(); Scanner sc = new Scanner(System.in); String choose = sc.next(); switch (choose) { case "1" -> queryFunction(); case "2" -> searchFunction(); case "3" -> deleteFunction(); case "4" -> updateFunction(); case "5" -> addFunction(); case "6" -> { System.out.println("退出系统"); System.exit(0); } default -> System.out.println("没有这个选项"); } } } //添加功能 private void addFunction() throws IOException { new Add(); } //更新数据功能 private void updateFunction() throws IOException { new Update(); } //删除数据功能 private void deleteFunction() throws IOException { new Delete(); } //搜索功能 private void searchFunction() throws IOException { new Search(); } //查询所有功能 private void queryFunction() throws IOException { new Query(); } }
-
Add
package com.example; import com.example.mapper.BookMapper; import com.example.mapper.LoanMapper; import com.example.mapper.ReaderMapper; import com.example.pojo.Book; import com.example.pojo.Loan; import com.example.pojo.Reader; 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.time.LocalDateTime; import java.util.Scanner; public class Add { public Add() throws IOException { while (true) { System.out.println("----------------添加功能---------------"); System.out.println("1.添加读者信息"); System.out.println("2.添加图书信息"); System.out.println("3.添加借阅信息"); System.out.println("4.返回上一级"); Scanner sc = new Scanner(System.in); String choose = sc.next(); switch (choose) { case "1" -> updateReadersInf(); case "2" -> updateBooksInf(); case "3" -> updateloansInf(); case "4" -> { System.out.println("返回上一级"); new LibrarySystem(); } default -> System.out.println("没有这个选项"); } } } private void updateloansInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); LoanMapper loanMapper = sqlSession.getMapper(LoanMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要添加的读者ID"); int readerId = sc.nextInt(); System.out.println("请输入需要添加的书本ID"); int bookId = sc.nextInt(); //获取当前时间 LocalDateTime loanData = LocalDateTime.now(); //当前时间加上一个月 LocalDateTime returnTime = loanData.plusMonths(1L); //将输入信息保存到读者对象中 Loan loan = new Loan(readerId,bookId,loanData,returnTime); //添加书本对象 //loanMapper.addLoan(loan); System.out.println("添加成功"); } private void updateBooksInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要添加的书本ID"); int ID = sc.nextInt(); System.out.println("请输入需要添加的书名"); String title = sc.next(); System.out.println("请输入需要添加的作者"); String author = sc.next(); System.out.println("请输入需要添加的出版社"); String publisher = sc.next(); //将输入信息保存到读者对象中 Book book = new Book(ID, title, author, publisher); //添加书本对象 bookMapper.addBook(book); System.out.println("添加成功"); } private void updateReadersInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要添加的读者ID"); int ID = sc.nextInt(); System.out.println("请输入需要添加的读者姓名"); String name = sc.next(); System.out.println("请输入需要添加的读者地址"); String address = sc.next(); System.out.println("请输入需要添加的读者电话"); String phone = sc.next(); //将输入信息保存到读者对象中 Reader reader = new Reader(ID, name, address, phone); //添加读者对象 readerMapper.addReader(reader); System.out.println("添加成功"); } }
-
Delete
package com.example; import com.example.mapper.BookMapper; import com.example.mapper.LoanMapper; import com.example.mapper.ReaderMapper; import com.example.pojo.Book; import com.example.pojo.Loan; import com.example.pojo.Reader; 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.time.LocalDateTime; import java.util.Scanner; public class Add { public Add() throws IOException { while (true) { System.out.println("----------------添加功能---------------"); System.out.println("1.添加读者信息"); System.out.println("2.添加图书信息"); System.out.println("3.添加借阅信息"); System.out.println("4.返回上一级"); Scanner sc = new Scanner(System.in); String choose = sc.next(); switch (choose) { case "1" -> updateReadersInf(); case "2" -> updateBooksInf(); case "3" -> updateloansInf(); case "4" -> { System.out.println("返回上一级"); new LibrarySystem(); } default -> System.out.println("没有这个选项"); } } } private void updateloansInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); LoanMapper loanMapper = sqlSession.getMapper(LoanMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要添加的读者ID"); int readerId = sc.nextInt(); System.out.println("请输入需要添加的书本ID"); int bookId = sc.nextInt(); //获取当前时间 LocalDateTime loanData = LocalDateTime.now(); //当前时间加上一个月 LocalDateTime returnTime = loanData.plusMonths(1L); //将输入信息保存到读者对象中 Loan loan = new Loan(readerId,bookId,loanData,returnTime); //添加书本对象 //loanMapper.addLoan(loan); System.out.println("添加成功"); } private void updateBooksInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要添加的书本ID"); int ID = sc.nextInt(); System.out.println("请输入需要添加的书名"); String title = sc.next(); System.out.println("请输入需要添加的作者"); String author = sc.next(); System.out.println("请输入需要添加的出版社"); String publisher = sc.next(); //将输入信息保存到读者对象中 Book book = new Book(ID, title, author, publisher); //添加书本对象 bookMapper.addBook(book); System.out.println("添加成功"); } private void updateReadersInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要添加的读者ID"); int ID = sc.nextInt(); System.out.println("请输入需要添加的读者姓名"); String name = sc.next(); System.out.println("请输入需要添加的读者地址"); String address = sc.next(); System.out.println("请输入需要添加的读者电话"); String phone = sc.next(); //将输入信息保存到读者对象中 Reader reader = new Reader(ID, name, address, phone); //添加读者对象 readerMapper.addReader(reader); System.out.println("添加成功"); } }
-
Query
package com.example; import com.example.mapper.BookMapper; import com.example.mapper.LoanMapper; import com.example.mapper.ReaderMapper; import com.example.pojo.Book; import com.example.pojo.Loan; import com.example.pojo.Reader; 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.ArrayList; import java.util.Scanner; public class Query { public Query() throws IOException { while (true) { System.out.println("----------------查询功能---------------"); System.out.println("1.查询所有借阅者信息"); System.out.println("2.查询所有图书信息"); System.out.println("3.查询所有借书信息"); System.out.println("4.返回上一级"); Scanner sc = new Scanner(System.in); String choose = sc.next(); switch (choose) { case "1" -> queryReadersInf(); case "2" -> queryBooksInf(); case "3" -> queryLoansInf(); case "4" -> { System.out.println("返回上一级"); new LibrarySystem(); } default -> System.out.println("没有这个选项"); } } } //查询所有借阅信息 private void queryLoansInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); LoanMapper loanmapper = sqlSession.getMapper(LoanMapper.class); ArrayList<Loan> list = loanmapper.selectAll(); if (list.size() == 0) { System.out.println("没有借阅信息"); } System.out.println("借阅者ID\t书ID\t借阅日期\t\t归还日期"); for (int i = 0; i < list.size(); i++) { Loan loan = list.get(i); System.out.println(loan.getReaderId() + "\t" + loan.getBookId() + "\t" + loan.getLoanData().toString()+ "\t" + loan.getReturnTime().toString()); } } //查询所有书籍信息 private void queryBooksInf() throws IOException { //1.加载核心配置文件,获取sqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.获取sqlsession对象,用他来执行sql SqlSession sqlSession = sqlSessionFactory.openSession(true); //3.获取readermapper接口代理对象 传入接口对象需要改 BookMapper bookmapper = sqlSession.getMapper(BookMapper.class); ArrayList<Book> list = bookmapper.selectAll(); //处理数据 if (list.size() == 0) { System.out.println("当前无书籍信息"); } System.out.println("书籍ID\t书名\t作者\t出版社"); for (int i = 0; i < list.size(); i++) { Book book = list.get(i); System.out.println(book.getBookID() + "\t" + book.getTitle() + "\t" + book.getAuthor() + "\t" + book.getPublisher()); } } //查询所有借阅者信息 private void queryReadersInf() throws IOException { //1.加载核心配置文件,获取sqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.获取sqlsession对象,用他来执行sql SqlSession sqlSession = sqlSessionFactory.openSession(); //3.获取readermapper接口代理对象 传入接口对象需要改 ReaderMapper readermapper = sqlSession.getMapper(ReaderMapper.class); ArrayList<Reader> list = readermapper.selectAll(); //处理数据 if (list.size() == 0) { System.out.println("当前无借阅者信息"); //结束方法 return; } System.out.println("读者ID\t姓名\t地址\t电话\t"); for (int i = 0; i < list.size(); i++) { Reader rd = list.get(i); System.out.println(rd.getReaderId() + "\t\t" + rd.getName() + "\t\t" + rd.getAddress() + "\t\t" + rd.getPhone()); } } }
-
Search
package com.example; import com.example.mapper.BookMapper; import com.example.mapper.BorrowInfMapper; import com.example.mapper.ReaderMapper; import com.example.pojo.Book; import com.example.pojo.BorrowInf; import com.example.pojo.Reader; 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.ArrayList; import java.util.Scanner; public class Search { public Search() throws IOException { while (true) { System.out.println("----------------搜寻功能---------------"); System.out.println("1.根据id搜寻读者信息"); System.out.println("2.根据id搜寻图书信息"); System.out.println("3.搜寻具体借阅信息"); System.out.println("4.返回上一级"); Scanner sc = new Scanner(System.in); String choose = sc.next(); switch (choose) { case "1" -> searchReadersInf(); case "2" -> searchBooksInf(); case "3" -> searchloansInf(); case "4" -> { System.out.println("返回上一级"); new LibrarySystem(); } default -> System.out.println("没有这个选项"); } } } //搜寻全部具体信息 private void searchloansInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); BorrowInfMapper borrowInfMapper = sqlSession.getMapper(BorrowInfMapper.class); ArrayList<BorrowInf> list = borrowInfMapper.searchAll(); System.out.println("读者ID\t姓名\t书ID\t书名\t\t作者"); for (int i = 0; i < list.size(); i++) { BorrowInf borrowInf = list.get(i); System.out.println(borrowInf.getReaderId() + "\t\t" + borrowInf.getName() + "\t\t" + borrowInf.getBookId() + "\t\t" + borrowInf.getTitle() + "\t\t" + borrowInf.getAuthor()); } } //根据id搜寻图书信息 private void searchBooksInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); System.out.println("输入需要查询的书ID"); Scanner sc = new Scanner(System.in); int bookId = sc.nextInt(); Book book = bookMapper.searchByid(bookId); if(book ==null){ System.out.println("输入的ID不存在"); return; } System.out.println("书ID\t书名\t作者\t出版社"); System.out.println(book.getBookID()+"\t"+book.getTitle()+"\t"+book.getAuthor() +"\t"+book.getPublisher()); } //根据id搜寻借阅者信息 private void searchReadersInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class); System.out.println("输入需要查询的借阅者ID"); Scanner sc = new Scanner(System.in); int readerId = sc.nextInt(); Reader reader = readerMapper.selectById(readerId); if(reader ==null){ System.out.println("输入的ID不存在"); return; } System.out.println("读者\tID\t姓名\t地址\t电话"); System.out.println(reader.getReaderId()+"\t"+reader.getName()+"\t" +reader.getAddress()+"\t"+reader.getPhone()); } }
-
Uppdate
package com.example; import com.example.mapper.BookMapper; import com.example.mapper.ReaderMapper; import com.example.pojo.Book; import com.example.pojo.Reader; 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 Update { public Update() throws IOException { while (true) { System.out.println("----------------更新功能---------------"); System.out.println("1.更新读者信息"); System.out.println("2.更新图书信息"); System.out.println("3.返回上一级"); Scanner sc = new Scanner(System.in); String choose = sc.next(); switch (choose) { case "1" -> updateReadersInf(); case "2" -> updateBooksInf(); case "3" -> { System.out.println("返回上一级"); new LibrarySystem(); } default -> System.out.println("没有这个选项"); } } } //更新书信息 private void updateBooksInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper对象 BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要更新的书ID"); int ID = sc.nextInt(); System.out.println("请输入需要更新的书名"); String name = sc.next(); System.out.println("请输入需要更新的作者"); String author = sc.next(); System.out.println("请输入需要更新的出版社"); String publisher = sc.next(); Book book = new Book(ID, name, author, publisher); bookMapper.updateById(book); System.out.println("更新成功"); } //更新读者信息 private void updateReadersInf() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper对象 ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class); Scanner sc = new Scanner(System.in); System.out.println("请输入需要更新的读者ID"); int ID = sc.nextInt(); System.out.println("请输入需要更新的读者姓名"); String name = sc.next(); System.out.println("请输入需要更新的读者地址"); String address = sc.next(); System.out.println("请输入需要更新的读者电话"); String phone = sc.next(); Reader reader = new Reader(ID, name, address, phone); readerMapper.updateById(reader); System.out.println("更新成功"); } }
-
App(运行文件)
package com.example; import java.io.IOException; /** * Hello world! * */ public class App { public static void main( String[] args ) throws IOException { new LibrarySystem(); } }
5. 项目完整下载
链接:https://pan.baidu.com/s/1h-nunPwwxqEZO87nZzkeEg
提取码:ychz