相关 知识 >>>

相关 练习 >>>

实现要求:

使用所学Spring知识,实现简易的图书查询系统功能。

要求如下:

  • 查询全部图书。
  • 根据书籍编号查询信息。
  • 根据书名查询书籍信息。
  • 根据状态查询书籍信息。

运行结果:

实现思路:

具体实现步骤:

(1)在MySQL数据库中创建t00_book表,代码如下:

-- ----------------------------
-- Table structure for t00_book
-- ----------------------------
DROP TABLE IF EXISTS `t00_book`;
CREATE TABLE `t00_book` (
`bookId` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '图书编号',
`bookName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '图书名字',
`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '作者',
`pages` int(11) NULL DEFAULT NULL COMMENT '页数',
`flag` char(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态(0-借出,1-在库)'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

(2)在t00_book表中插入初始化数据,代码如下:

-- ----------------------------
-- Records of t00_book
-- ----------------------------
INSERT INTO `t00_book` VALUES ('1000', '《西游记》', '吴承恩', 324, '1');
INSERT INTO `t00_book` VALUES ('1002', '《红楼梦》', '曹雪芹', 425, '1');
INSERT INTO `t00_book` VALUES ('1003', '《三国演义》', '罗贯中', 346, '1');
INSERT INTO `t00_book` VALUES ('1004', '《水浒传》', '施耐庵', 298, '1');
INSERT INTO `t00_book` VALUES ('1005', '《儒林外史》', '吴敬梓', 234, '1');
INSERT INTO `t00_book` VALUES ('1006', '《离骚》', '屈原', 186, '1');
INSERT INTO `t00_book` VALUES ('1007', '《牡丹亭》', '汤显祖', 168, '1');
INSERT INTO `t00_book` VALUES ('1007', 'Python高级编程', '张大大', 322, '1');

(3)引入jar包:

(4)在applicationContext.xml配置文件中打开注解扫描功能,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!-- 开启注解扫描功能 -->
<context:component-scan base-package="com.mhys.book"></context:component-scan> </beans>

(5)在com.mhys.book.pojo包下创建DataSource类,添加driverClassName,url,username,password4个属性,然后又注入属性值,代码如下:

package com.mhys.book.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class DataSource {
@Value("com.mysql.jdbc.Driver")
private String driverClassName;
@Value("jdbc:mysql://127.0.0.1:3306/t00_book")
private String url;
@Value("root")
private String username;
@Value("123456")
private String password; public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

(6)在com.mhys.book.pojo包下创建Book类,添加bookId,bookName,author,pages,flag5个属性,然后注入属性值,代码如下

package com.mhys.book.pojo;

public class Book {
private String bookId;
private String bookName;
private String author;
private int pages;
private String flag; public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
} }

(7)在com.mhys.book.dao包下创建BaseDao类,添加dataSource属性并注入属性值,然后声明getCounection()方法,代码如下:

package com.mhys.book.dao;

import java.sql.Connection;
import java.sql.DriverManager; import java.sql.PreparedStatement;
import java.sql.ResultSet; import javax.annotation.PostConstruct; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.mhys.book.pojo.DataSource; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BaseDao {
@Autowired
private DataSource dataSource; @PostConstruct
public Connection getConnection() throws Exception{
Class.forName(dataSource.getDriverClassName());
String url = dataSource.getUrl();
String user = dataSource.getUsername();
String password = dataSource.getPassword();
Connection connection = DriverManager.getConnection(url, user, password); return connection;
} public void close(ResultSet rs,PreparedStatement ps,Connection conn) throws Exception{
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
} }

(8)在com.mhys.book.dao包下创建BookDao类,声明getAllBooksInfo()方法、getBooksInfoByid()方法,添加getBooksInfoByBookName()方法和getBooksInfoByflag()方法,代码如下:

package com.mhys.book.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import org.junit.Test;
import org.springframework.stereotype.Component; @Component
public class BookDao extends BaseDao { //查询全部
public ResultSet getAllBooksInfo() throws Exception{
String sql = "select bookId,bookName,author,pages,flag from t00_book";
Connection connection = this.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
return rs;
} //根据书籍编号查询信息
public ResultSet getBooksInfoById(String bookId) throws Exception{
String sql = "select bookId,bookName,author,pages,flag from t00_book where bookId=?";
Connection connection = this.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, bookId);
ResultSet rs = ps.executeQuery();
return rs;
} //根据书名查询书籍信息
public ResultSet getBooksInfoByBookName(String bookName) throws Exception{
String sql = "select bookId,bookName,author,pages,flag from t00_book where bookName like ?";
Connection connection = this.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, "%"+bookName+"%");
ResultSet rs = ps.executeQuery();
return rs;
} //根据状态查询书籍信息
public ResultSet getBooksInfoByflag(String flag) throws Exception{
String sql = "select bookId,bookName,author,pages,flag from t00_book where flag=?";
Connection connection = this.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, flag);
ResultSet rs = ps.executeQuery();
return rs;
} }

(9)在com.mhys.book.service包下创建BookService类,添加bookDao属性并注入属性值,然后声明 getAllBooksInfo() 方法和 getBooksInfoById()方法、getBooksInfoByBookName()方法、getBooksInfoByflag()方法、和print()方法,代码如下:

package com.mhys.book.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.mhys.book.dao.BookDao; @Component
public class BookService {
@Autowired
private BookDao bookDao; //查询全部
public void getAllBooksInfo() throws Exception{
ResultSet rs = bookDao.getAllBooksInfo();
print(rs);
} //根据书籍编号查询信息
public void getBooksInfoById(String bookId) throws Exception{
ResultSet rs = bookDao.getBooksInfoById(bookId);
print(rs);
} //根据书名查询书籍信息
public void getBooksInfoByBookName(String bookName) throws Exception{
ResultSet rs = bookDao.getBooksInfoByBookName(bookName);
print(rs);
} //根据状态查询书籍信息
public void getBooksInfoByflag(String flag) throws Exception{
ResultSet rs = bookDao.getBooksInfoByflag(flag);
print(rs);
} //释放资源
public void close(String flag) throws Exception{
ResultSet rs = bookDao.getBooksInfoByflag(flag);
print(rs);
} //打印结果
public void print(ResultSet rs) throws SQLException{
if(rs==null){
System.out.println("查询无结果!");
}else{
System.out.println("编号\t\t书名\t\t作者\t\t总页数\t\t状态");
while (rs.next()) {
for (int i = 1; i <= 5; i++) {
String value = "";
if(i==5){
value = rs.getString(i).equals("0")?"借出":"在库";
}else{
value = rs.getString(i);
}
System.out.print(value+"\t\t");
}
System.out.println("");
}
} }
}

(10)在com.mhys.book.main包下创建MainClass类,添加bookService属性并注入属性值,然后声明main()方法、start()方法、printMenu()方法、getAllBooksInfo()方法、getBooksInfoById()方法、getBooksInfoByBookName()方法、getBooksInfoByflag()方法,代码如下:

package com.mhys.book.main;

import java.util.Scanner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; import com.mhys.book.service.BookService; @Component
public class MainClass {
@Autowired
private BookService bookService; public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MainClass main = (MainClass) context.getBean("mainClass");
main.start();
} public void start() throws Exception{
Scanner scan = new Scanner(System.in);
System.out.println("----------欢迎使用图书管理查询系统----------");
printMenu(); while(true)
{
//读取用户输入
int choice = scan.nextInt(); if(choice == 5)
{
System.out.println("成功退出系统,欢迎再次光临!");
break;
}
switch(choice)//switch形式
{
case 1: getAllBooksInfo(); break;
case 2: getBooksInfoById(); break;
case 3: getBooksInfoByBookName(); break;
case 4: getBooksInfoByflag(); break;
default: System.out.println("输入非法");
continue;
}
printMenu();
} } public void printMenu(){
System.out.println("请选择查询条件:1、查询全部\t2、根据书籍编号查询\t3、根据书籍名称查询\t4、根据状态查询\t5、退出系统");
} public void getAllBooksInfo() throws Exception{
bookService.getAllBooksInfo();
}
public void getBooksInfoById() throws Exception{
Scanner scan = new Scanner(System.in);
System.out.println("请输入书籍编号:");
String bookId = scan.next();
bookService.getBooksInfoById(bookId);
}
public void getBooksInfoByBookName() throws Exception{
Scanner scan = new Scanner(System.in);
System.out.println("请输入书名:");
String bookName = scan.next();
bookService.getBooksInfoByBookName(bookName);
}
public void getBooksInfoByflag() throws Exception{
Scanner scan = new Scanner(System.in);
System.out.println("请输入状态编号:0、借出\t1、在库");
String flag = scan.next();
bookService.getBooksInfoByflag(flag);
}
}

使用所学Spring知识,实现简易的图书查询系统功能。实现查询全部图书。 根据书籍编号查询信息。 根据书名查询书籍信息。 根据状态查询书籍信息。的更多相关文章

  1. 57. Spring 自定义properties升级篇【从零开始学Spring Boot】

    之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...

  2. 学习spring2--跟我一起学Spring 3(3)–使用Spring开发第一个HelloWorld应用

    http://www.importnew.com/13246.html     首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 ...

  3. 学习spring1--跟我一起学Spring 3(2)–开发环境配置

    http://www.importnew.com/13185.html#spring     首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所 ...

  4. 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...

  5. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  6. 49. spring boot日志升级篇—理论【从零开始学Spring Boot】

    我们之前在其中的一篇文章介绍过如何在spring boot中使用日志记录SLF4J. Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如 ...

  7. (41)Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

    已经好久没有讲一些基础的知识了,这一小节来点简单的,这也是为下节的在Spring Boot中使用多数据源做准备. 从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这 ...

  8. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  9. 从原理层面掌握@SessionAttribute的使用【一起学Spring MVC】

    每篇一句 不是你当上了火影大家就认可你,而是大家都认可你才能当上火影 前言 该注解顾名思义,作用是将Model中的属性同步到session会话当中,方便在下一次请求中使用(比如重定向场景~). 虽然说 ...

随机推荐

  1. 【leetcode】565. Array Nesting

    You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...

  2. 通信方案软件设计(环形动态申请内存,支持USART+IIC+SPI+CAN协议

    1 <STM32进阶之串口环形缓冲区实现>中讲得比较清楚(链接) 2 amobbs中讲的方法有点复杂,以下是链接和参考源码: 通信方案软件设计(环形动态申请内存,支持USART+IIC+S ...

  3. ebs 初始化登陆

    BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...

  4. 【编程思想】【设计模式】【其他模式】blackboard

    Python版 https://github.com/faif/python-patterns/blob/master/other/blackboard.py #!/usr/bin/env pytho ...

  5. Playing with Destructors in C++

    Predict the output of the below code snippet. 1 #include <iostream> 2 using namespace std; 3 4 ...

  6. Enumeration遍历http请求参数的一个例子

    Enumeration<String> paraNames=request.getParameterNames(); for(Enumeration e=paraNames;e.hasMo ...

  7. nexus 私服 拉不了 jar 包,报 Not authorized

    问题: 无法下载导入jar包,idea reload 时 报: Could not transfer artifact com.xxx:parent:pom:1.0-SNAPSHOT from/to ...

  8. JSP 文字乱码、${}引用无效

    问题: 代码:<form action="/test/requestPost.do" method="post"> <input type=& ...

  9. 如何为Dash/Zeal生成c++ 文档: 以abseil文档为例

    目录 1. 软件安装 2 Sample源文件下载: 3. 生成步骤 3.1 使用doxygen生成html文件 3.2 使用docsetutil 生成 dash/Zeal 格式 1. 软件安装: 1. ...

  10. 删除其他列Table.SelectColumns(Power Query 之 M 语言)

    数据源: "姓名""基数""个人比例""个人缴纳""公司比例""公司缴纳"&qu ...