项目取名为BookManger,在Navicat中创建数据库为book_manage,包含三个表分别为学生,书籍,将学生与书籍连接起来的借阅表如下图
在IDEA创建BookManage,配置maven,导入如下坐标
4.0.0 org.example BookManger 1.0-SNAPSHOT 17 17 org.projectlombok lombok 1.18.24 provided org.junit.jupiter junit-jupiter 5.8.2 test mysql mysql-connector-java 8.0.28 org.mybatis mybatis 3.5.9
将创建时的配置文件删除,自己定义使用mapper的配置文件,连接上述所述的数据库如下:
定义接口BookMapper用于传递具体书籍学生信息(注释实现)
package book.manage.mapper; import book.manage.entity.Book; import book.manage.entity.Student; import org.apache.ibatis.annotations.Insert; public interface BookMapper { @Insert("insert into student(name,sex,grade) values(#{name},#{sex},#{grade})") int addStudent(Student student); @Insert("insert into book(title,`desc`,price) values(#{title},#{desc},#{price})") int addBook(Book book); }
定义调用配置文件的类SqlUtil.java
package book.manage.sql; import book.manage.mapper.BookMapper; 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.util.function.Consumer; public class SqlUtil { private SqlUtil(){} private static SqlSessionFactory factory; static{ try{ factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")); } catch (IOException e) { e.printStackTrace(); } } public static void doSqlWork(Consumerconsumer){ try(SqlSession sqlSession= factory.openSession(true)){ BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); consumer.accept(bookMapper); } } }
如上配置过程暂且告一段落,接下来是具体的实现:
首先定义学生类:
package book.manage.entity; import lombok.Data; @Data public class Student { int sid; final String name; final String sex; final int grade; }
以及书籍类:
package book.manage.entity; import lombok.Data; @Data public class Book { int bid; final String title; final String desc; final double price; }
实现过程在main函数如下:
package book.manage; import book.manage.entity.Book; import book.manage.entity.Student; import book.manage.sql.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 scanner = 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("输入您想要执行的操作,输入其他任意数字退出"); int input; try { input = scanner.nextInt(); }catch (Exception e){ return; } scanner.nextLine(); switch (input) { case 1: addStudent(scanner); break; case 2: addBook(scanner); break; default: return; } } } } private static void addBook(Scanner scanner){ System.out.println("请输入书籍名字"); String title =scanner.nextLine(); System.out.println("请输入书籍介绍"); String desc =scanner.nextLine(); System.out.println("请输入书籍价格"); String price =scanner.nextLine(); double p = Integer.parseInt(price); Book book =new Book(title,desc,p); SqlUtil.doSqlWork(mapper ->{ int i=mapper.addBook(book); if(i>0) { System.out.println("书籍信息录入成功"); log.info("新添加一条新书籍信息" + book); }else System.out.println("书籍信息录入失败"); }); } private static void addStudent(Scanner scanner){ System.out.println("请输入学生名字"); String name =scanner.nextLine(); System.out.println("请输入学生性别(男/女)"); String sex =scanner.nextLine(); System.out.println("请输入学生年级"); String grade =scanner.nextLine(); int g = Integer.parseInt(grade); Student student =new Student(name,sex,g); SqlUtil.doSqlWork(mapper ->{ int i=mapper.addStudent(student); if(i>0) { System.out.println("学生信息录入成功"); log.info("新添加一条新学生信息" + student); }else System.out.println("学生信息录入失败"); }); } }
测试结果:
使用的是Properties配置日志文件
定义新文件为logging.properties
handlers= java.util.logging.FileHandler .level= ALL java.util.logging.FileHandler.pattern=console.log java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
在main函数添加注释并在相应位置添加对应弹出日志提示信息:
测试结果