网站地址栏小图标,企业做网站设计,制作灯笼作文300字,百度收录正常网站流量下降目录
1. MyBatis与MySQL配置
1.1 创建数据库及数据表 1.2 配置MyBatis与数据库
1.2.1 增加MyBatis与MySQL相关依赖
1.2.2 配置application.yml文件
1.3 增加数据表对应实体类
2. 功能1#xff1a;用户登录
2.1 约定前后端交互接口
2.2 后端接口
2.3 前端页面
2.4 单…目录
1. MyBatis与MySQL配置
1.1 创建数据库及数据表 1.2 配置MyBatis与数据库
1.2.1 增加MyBatis与MySQL相关依赖
1.2.2 配置application.yml文件
1.3 增加数据表对应实体类
2. 功能1用户登录
2.1 约定前后端交互接口
2.2 后端接口
2.3 前端页面
2.4 单元测试
3. 功能2图书列表
3.1 约定前后端交互接口
3.2 后端接口
3.3 前端页面
3.4 单元测试 1. MyBatis与MySQL配置
1.1 创建数据库及数据表
创建数据库book_test建议选择编码为utf8mb4
创建用户表user_info和图书表book_info
-- 用户表
DROP TABLE IF EXISTS user_info;
CREATE TABLE user_info (id INT NOT NULL AUTO_INCREMENT,user_name VARCHAR ( 128 ) NOT NULL,password VARCHAR ( 128 ) NOT NULL,delete_flag TINYINT ( 4 ) NULL DEFAULT 0,create_time DATETIME DEFAULT now(),update_time DATETIME DEFAULT now() ON UPDATE now(),PRIMARY KEY ( id ),
UNIQUE INDEX user_name_UNIQUE ( user_name ASC )) ENGINE INNODB DEFAULT
CHARACTER
SET utf8mb4 COMMENT 用户表;-- 图书表
DROP TABLE IF EXISTS book_info;
CREATE TABLE book_info (id INT ( 11 ) NOT NULL AUTO_INCREMENT,book_name VARCHAR ( 127 ) NOT NULL,author VARCHAR ( 127 ) NOT NULL,count INT ( 11 ) NOT NULL,price DECIMAL (7,2 ) NOT NULL,publish VARCHAR ( 256 ) NOT NULL,status TINYINT ( 4 ) DEFAULT 1 COMMENT 0-无效, 1-正常, 2-不允许借阅,create_time DATETIME DEFAULT now(),update_time DATETIME DEFAULT now() ON UPDATE now(),
PRIMARY KEY ( id )
) ENGINE INNODB DEFAULT CHARSET utf8mb4;-- 初始化数据
INSERT INTO user_info ( user_name, PASSWORD ) VALUES ( admin, admin );
INSERT INTO user_info ( user_name, PASSWORD ) VALUES ( zhangsan, 123456 );
-- 初始化图书数据
INSERT INTO book_info (book_name,author,count, price, publish) VALUES (活着,
余华, 29, 22.00, 北京文艺出版社);
INSERT INTO book_info (book_name,author,count, price, publish) VALUES (平凡的世界,
路遥, 5, 98.56, 北京十月文艺艺出版社);
INSERT INTO book_info (book_name,author,count, price, publish) VALUES (三体,
刘慈欣, 9, 102.67, 重庆出版社);
INSERT INTO book_info (book_name,author,count, price, publish) VALUES (金字塔原理,
麦肯锡, 16, 178.00, 民主与建设出版社); 1.2 配置MyBatis与数据库
1.2.1 增加MyBatis与MySQL相关依赖 1.2.2 配置application.yml文件
# 端口配置
server:port: 8080
# 数据库连接配置
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/book_test?characterEncodingutf8useSSLfalseusername: rootpassword: xxxxxxdriver-class-name: com.mysql.cj.jdbc.Driver
mybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #配置打印MyBatis日志map-underscore-to-camel-case: true #配置转换驼峰
# 设置日志文件的文件名
logging:file:name: /logger/spring-book.log
1.3 增加数据表对应实体类
创建model包并在其下创建BookInfo和UserInfo类
package com.example.bookmanagementsystem.model;
import lombok.Data;
import java.math.BigDecimal;
Data
public class BookInfo {private Integer id;private String bookName;private String author;private Integer count;private BigDecimal price;private String publish;private Integer status; //1-可借阅2-不可借阅private String statusCN; // status的中文展示
}package com.example.bookmanagementsystem.model;
import lombok.Data;
import java.util.Date;
Data
public class UserInfo {private Integer id;private String userName;private String password;private Integer deleteFlag;private Date createTime;private Date updateTime;
}
2. 功能1用户登录
2.1 约定前后端交互接口
请求/User/login
参数userNameadminpasswordadmin
响应成功返回true失败返回false
2.2 后端接口
UserController类
package com.example.bookmanagementsystem.controller;import com.example.bookmanagementsystem.model.UserInfo;
import com.example.bookmanagementsystem.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RequestMapping(/User)
RestController
public class UserController {Autowiredprivate UserService userService;RequestMapping(/login)public Boolean login(String userName, String password, HttpSession session){// 校验参数合法性if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){return false;}
// 1、根据用户名查找用户信息UserInfo userInfo userService.getUserInfoByName(userName);// 2、校验密码正确性if(userInfo null || userInfo.getId()0){return false;}if(password.equals(userInfo.getPassword())){// 存SessionuserInfo.setPassword();session.setAttribute(userName,userInfo);return true;}return false;}
}UserService类
package com.example.bookmanagementsystem.service;import com.example.bookmanagementsystem.mapper.UserInfoMapper;
import com.example.bookmanagementsystem.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class UserService {Autowiredprivate UserInfoMapper userInfoMapper;public UserInfo getUserInfoByName(String name){return userInfoMapper.selectUserByName(name);}} UserMapper接口
package com.example.bookmanagementsystem.mapper;import com.example.bookmanagementsystem.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;Mapper
public interface UserInfoMapper {
// 根据用户名查询用户信息Select(select* from user_info where user_name#{name})UserInfo selectUserByName(String name);
}
2.3 前端页面
!DOCTYPE html
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/titlelink relstylesheet hrefcss/bootstrap.min.csslink relstylesheet hrefcss/login.cssscript typetext/javascript srcjs/jquery.min.js/script
/headbodydiv classcontainer-logindiv classcontainer-picimg srcpic/computer.png width350px/divdiv classlogin-dialogh3登录/h3div classrowspan用户名/spaninput typetext nameuserName iduserName classform-control/divdiv classrowspan密码/spaninput typepassword namepassword idpassword classform-control/divdiv classrowbutton typebutton classbtn btn-info btn-lg onclicklogin()登录/button/div/div/divscript srcjs/jquery.min.js/scriptscriptfunction login() {$.ajax({url:/User/login,type:post,data:{userName:$(#userName).val(),password:$(#password).val()},success:function(result){if(result){location.href book_list.html;}else{alert(用户名或密码错误);}}});}/script
/body/html
2.4 单元测试
1、后端接口测试 可在服务器日志处查看相关信息 2、前端页面测试
输入账号admin和密码admin后即可成功登录
3. 功能2图书列表
3.1 约定前后端交互接口
1、前端发送给后端封装为对象PageRequest
1当前页currentPage
2每页显示的条数pageSize
2、后端发送给前端封装为对象PageResult
1当前页的内容records类型为ListBookInfo
2总条数total
关于SQL语句的分页查询关键字limit与offset常用方式如下
1、select* from table_name limit num2,num1;
2、select* from table_name limit num1 offset num2;
以上两句都表示从num21行开始返回num1行数据
3.2 后端接口
BookController类
package com.example.bookmanagementsystem.controller;import com.example.bookmanagementsystem.model.BookInfo;
import com.example.bookmanagementsystem.model.PageRequest;
import com.example.bookmanagementsystem.model.PageResult;
import com.example.bookmanagementsystem.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;Slf4j
RequestMapping(/Book)
RestController
public class BookController {Autowiredprivate BookService bookService;RequestMapping(/getBookListByPage)public PageResultBookInfo getBookListByPage(PageRequest pageRequest){log.info(接收到查询翻页信息pageRequest:{},pageRequest);if(pageRequest.getPageSize()0 || pageRequest.getCurrentPage()1) {return null;}PageResultBookInfo bookInfoPageResultnull;try{bookInfoPageResult bookService.selectBookInfoByPage(pageRequest);}catch (Exception e){log.error(查询翻页信息错误e{},e);}return bookInfoPageResult;}
}BookService类
package com.example.bookmanagementsystem.service;import com.example.bookmanagementsystem.mapper.BookInfoMapper;
import com.example.bookmanagementsystem.model.BookInfo;
import com.example.bookmanagementsystem.model.PageRequest;
import com.example.bookmanagementsystem.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;import java.util.List;
Service
public class BookService {Autowiredprivate BookInfoMapper bookInfoMapper;public PageResultBookInfo selectBookInfoByPage(PageRequest pageRequest){if(pageRequest null){return null;}
// 获取总记录数Integer count bookInfoMapper.count();
// 获取当前页内容ListBookInfo bookInfos bookInfoMapper.selectBookInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());return new PageResult(bookInfos,count,pageRequest);}
}BookInfoMapper接口
package com.example.bookmanagementsystem.mapper;import com.example.bookmanagementsystem.model.BookInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;import java.util.List;Mapper
public interface BookInfoMapper {
// 获取当前页信息Select(select* from book_info where status!0 order by id desc limit #{offset}, #{pageSize})ListBookInfo selectBookInfoByPage(Param(offset) Integer offset, Param(pageSize) Integer pageSize);// 获取总记录数Select(select count(1) from book_info where status !0)Integer count();
}3.3 前端页面
!DOCTYPE html
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0title图书列表展示/titlelink relstylesheet hrefcss/bootstrap.min.csslink relstylesheet hrefcss/list.cssscript typetext/javascript srcjs/jquery.min.js/scriptscript typetext/javascript srcjs/bootstrap.min.js/scriptscript srcjs/jq-paginator.js/script/headbodydiv classbookContainerh2图书列表展示/h2div classnavbar-justify-betweendivbutton classbtn btn-outline-info typebutton onclicklocation.hrefbook_add.html添加图书/buttonbutton classbtn btn-outline-info typebutton onclickbatchDelete()批量删除/button/div/divtabletheadtrtd选择/tdtd classwidth100图书ID/tdtd书名/tdtd作者/tdtd数量/tdtd定价/tdtd出版社/tdtd状态/tdtd classwidth200操作/td/tr/theadtbody/tbody/tablediv classdemoul idpageContainer classpagination justify-content-center/ul/divscriptgetBookList();function getBookList() {$.ajax({url:/Book/getBookListByPagelocation.search,type:get,success:function(result){var finalHtml;// 加载列表// 根据每一条记录拼接HTML即一个trfor(var book of result.records){finalHtmltr;finalHtmltdinput typecheckbox nameselectBook valuebook.id idselectBook classbook-select/td;finalHtmltdbook.id/td;finalHtmltdbook.bookName/td;finalHtmltdbook.author/td;finalHtmltdbook.count/td;finalHtmltdbook.price/td;finalHtmltdbook.publish/td;finalHtmltdbook.statusCN/td;finalHtmltd;finalHtmldiv classop;finalHtmla hrefbook_update.html?bookIdbook.id修改/a;finalHtmla hrefjavascript:void(0) onclickdeleteBook(book.id)删除/a;finalHtml/div;finalHtml td;finalHtml tr;}$(tbody).html(finalHtml)//翻页信息$(#pageContainer).jqPaginator({totalCounts: result.total, //总记录数pageSize: 10, //每页的个数visiblePages: 5, //可视页数currentPage: result.pageRequest.currentPage, //当前页码first: li classpage-itema classpage-link首页/a/li,prev: li classpage-itema classpage-link hrefjavascript:void(0);上一页\/a\/li,next: li classpage-itema classpage-link hrefjavascript:void(0);下一页\/a\/li,last: li classpage-itema classpage-link hrefjavascript:void(0);最后一页\/a\/li,page: li classpage-itema classpage-link hrefjavascript:void(0);{{page}}\/a\/li,//页面初始化和页码点击时都会执行onPageChange: function (page, type) {console.log(第page页, 类型:type);if(typechange){location.hrefbook_list.html?currentPagepage;}}});}});}function deleteBook(id) {var isDelete confirm(确认删除?);if (isDelete) {//删除图书alert(删除成功);}}function batchDelete() {var isDelete confirm(确认批量删除?);if (isDelete) {//获取复选框的idvar ids [];$(input:checkbox[nameselectBook]:checked).each(function () {ids.push($(this).val());});console.log(ids);alert(批量删除成功);}}/script/div
/body/html
3.4 单元测试
由于设有默认值不传参数进行测试 也可传参进行测试