资源下载:https://download.csdn.net/download/weixin_44893902/45598347
练习点设计:模糊查询、删除、新增
一、语言和环境
- 实现语言:JAVA语言。
- 环境要求:MyEclipse/Eclipse + Tomcat + MySql。
- 使用技术:
Jsp
+Servle
t+JavaBean
或SpringMVC
+Spring
+Mybatis
。
二、实现功能
随着信息技术的高速发展,各部门对于图书管理方式不一,现需要制作图书资源管理系统,主要功能如下:
-
首页默认显示所有图书资源,如图所示。
-
鼠标悬停某行数据时,该行数据突出显示效果,如图所示。
-
用户输入“书籍名称”字段,点击搜索按钮完成模糊查询,显示查询结果,如图所示。
-
用户点击删除,则弹出提示框,用户点击确定后,删除选中数据并显示最新数据,如图所示。
5.用户点击“新增”链接,则打开新增页面,填写完相关信息后点击添加按钮,增加图书资源信息数据到数据库,且页面跳转到列表页面展示最新数据,如图6和图7所示。
三、数据库设计
- 创建数据库(
book_manage
)。 - 创建数据表(
tb_book
),结构如下。
字段名 | 说明 | 字段类型 | 长度 | 备注 |
---|---|---|---|---|
id | 编号 | int | 主键,自增,增量为1 | |
name | 书籍名称 | varchar | 50 | 不能为空 |
author | 作者 | varchar | 10 | 不能为空 |
publish_date | 出版日期 | date | 不能为空 | |
press | 出版社 | varchar | 50 | 不能为空 |
四、推荐实现步骤
- SSM版本的实现步骤如下:
(1)创建数据库和数据表,添加测试数据(至少添加5条测试数据)。
(2)创建Web工程并创建各个包,导入工程所需的jar文件。
(3)添加相关SSM框架支持。
(4)配置项目所需要的各种配置文件(mybatis配置文件、spring配置文件、springMVC配置文件)。
(5)创建实体类。
(6)创建MyBatis操作数据库所需的Mapper接口及其Xml映射数据库操作语句文件。
(7)创建业务逻辑相应的接口及其实现类,实现相应的业务,并在类中加入对DAO/Mapper的引用和注入。
(8)创建Controller控制器类,在Controller中添加对业务逻辑类的引用和注入,并配置springMVC配置文件。
(9)创建相关的操作页面,并使用CSS对页面进行美化。
(10)实现页面的各项操作功能,并在相关地方进行验证,操作要人性化。
(11)调试运行成功后导出相关的数据库文件并提交。
五、实现代码
1、MySQL数据库
book_manage.sql
/*
Date: 04/08/2021 20:51:19
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_book
-- ----------------------------
DROP TABLE IF EXISTS `tb_book`;
CREATE TABLE `tb_book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`publish_date` date NULL DEFAULT NULL,
`press` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of tb_book
-- ----------------------------
INSERT INTO `tb_book` VALUES (1, '向往的生活', '张强', '1990-01-01', '科学出版社');
INSERT INTO `tb_book` VALUES (2, '幸福的生活', '李辉', '2006-06-05', '高等教育出版社');
INSERT INTO `tb_book` VALUES (3, '这一生这么过', '郭强铭', '2021-08-10', '大百科全书出版社');
INSERT INTO `tb_book` VALUES (4, '爱的教育', '张德旭', '1992-06-20', '高等教育出版社');
SET FOREIGN_KEY_CHECKS = 1;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2、项目Java代码
目录结构
book_manage
JAR包:
src
com.controller
BookMapperController.java
package com.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.entity.TbBook;
import com.service.impl.BookManageService;
@Controller
public class BookMapperController {
@Resource
BookManageService service;
@RequestMapping("/selectAll")
public ModelAndView selectAll(String name) {
ModelAndView modelAndView = new ModelAndView();
if (name==null||name.equals("")) {
name="";
}
List<TbBook> tbBookList=service.selectAll(name);
modelAndView.setViewName("tbBook");
modelAndView.addObject("tbBookList", tbBookList);
return modelAndView;
}
//娣诲姞
@RequestMapping("/jump")
public String jump() {
return "addTbBook";
}
@RequestMapping("/insertTbBook")
public String insertTbBook(TbBook tbBook) {
int add=service.insertTbBook(tbBook);
return "redirect:/selectAll.do";
}
//鍒犻櫎
@RequestMapping("/delTbBook")
public String delTbBook(int id) {
int del=service.delTbBook(id);
return "redirect:/selectAll.do";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
com.dao
TbBookMapper.java
package com.dao;
import com.entity.TbBook;
import java.util.List;
public interface TbBookMapper {
int deleteByPrimaryKey(Integer id);
int insert(TbBook record);
TbBook selectByPrimaryKey(Integer id);
List<TbBook> selectAll();
int updateByPrimaryKey(TbBook record);
List<TbBook> likeSelect(String name);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
TbBookMapper.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.dao.TbBookMapper" >
<resultMap id="BaseResultMap" type="com.entity.TbBook" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="author" property="author" jdbcType="VARCHAR" />
<result column="publish_date" property="publishDate" jdbcType="DATE" />
<result column="press" property="press" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from tb_book
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.entity.TbBook" >
insert into tb_book (id, name, author,
publish_date, press)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR},
#{publishDate,jdbcType=DATE}, #{press,jdbcType=VARCHAR})
</insert>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select id, name, author, publish_date, press
from tb_book
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select id, name, author, publish_date, press
from tb_book
</select>
<select id="likeSelect" resultMap="BaseResultMap" >
select id, name, author, publish_date, press
from tb_book where `name` LIKE "%"#{name}"%"
</select>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
com.entity
TbBook.java
package com.entity;
import java.util.Date;
public class TbBook {
private Integer id;
private String name;
private String author;
private String publishDate;
private String press;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author == null ? null : author.trim();
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press == null ? null : press.trim();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
com.generator
Generator.java
package com.generator;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Generator {
/*
* targetRuntime="MyBatis3Simple", 不生成Example
*/
public void generateMyBatis() {
// MBG执行过程中的警告信息
List<String> warnings = new ArrayList<String>();
// 当生成的代码重复时,覆盖原代码
boolean overwrite = true;
String generatorFile = "/generator/generatorConfig.xml";
// String generatorFile = "/generator/generatorConfigExample.xml";
// 读取MBG配置文件
InputStream is = Generator.class.getResourceAsStream(generatorFile);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config;
try {
config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
// 创建MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
// 执行生成代码
myBatisGenerator.generate(null);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (String warning : warnings) {
System.out.println(warning);
}
}
public static void main(String[] args) {
Generator generator = new Generator();
generator.generateMyBatis();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32