|

楼主 |
发表于 2024-11-22 22:45:47
|
显示全部楼层
3.2安全性约束
3.2.1存储过程的设计
判断并对数据进行插入
create procedure insert_book (in in_auther varchar(20),in in_name varchar(20),in in_price int,in in_leadnum int,in in_buystock int)
begin
declare exit_bid int;
select bid into exit_bid from purchase_list where name=in_name and author=in_auther;
if exit_bid is not null
then
update purchase_list set lendnum=lendnum+in_leadnum , buystock=buystock+in_buystock where name=in_name and author=in_auther;
else
insert into purchase_list values (null,in_name,in_price,in_auther,in_leadnum,in_buystock);
end if;
end;
3.2.2触发器设计
防止其归还时间和借阅时间出错
CREATE TRIGGER check_return_time
after INSERT ON lend_record
FOR EACH ROW
BEGIN
IF NEW.date_return <= NEW.date_lend THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Return date cannot be earlier than lend date';
END IF;
END;
3.3数据库访问设计
总体接口的实现:
<----DAO层---->
@Mapper
public interface AdminDAO {
//返回所有的管理员信息
public List<Admin> admins();
}
@Mapper
public interface BookDAO {
//插入图书数据
public int inertBook(Book book);
//根据书籍id查询书籍信息,是其他表通过id访问书籍信息的方法
public Book selectBookById(@Param("bid") int bid);
}
@Mapper
public interface BuyRecordDAO {
//插入购买记录
public int insertRecord(BuyRecord buyRecord);
//根据uid返回所有购买记录
public List<HashMap<String,Object>> select_AccordingUser(@Param("uid") String uid);
//返回所有购买记录
public List<HashMap<String,Object>> selectAll();
}
@Mapper
public interface LendRecordDAO {
//插入借阅记录
public int insertRecord(LendRecord lendRecord);
//删除插入记录
public int deleteRecord(@Param("uid") String uid,@Param("bid") int bid);
//根据uid返回所有借阅记录
public List<HashMap<String,Object>> select_Map_AccordingUser(@Param("uid") String uid);
//返回所有借阅记录
public List<HashMap<String,Object>> selectAll_Map();
}
@Mapper
public interface PurchaseListDAO {
//插入进货数据
public int purchaseBook(@Param("book") Book book, @Param("lendnum") int lendnum, @Param("buynum")int buynum);
//根据book信息查找bid
public Integer getBidInt(Book book);
//根据bid返回lendnum
public int getLendnum(@Param("bid") int bid);
//根据bid返回buynum
public int getBuynum(@Param("bid") int bid);
//更新purchase_list数据
public int updatedata(@Param("bid")int bid,@Param("lendnum") int lendnum,@Param("buynum") int buynum);
}
@Mapper
public interface RepositoryDAO {
//根据bid返回lendnum
public int addBook(@Param("book") Book book, @Param("lendnum") int lendnum, @Param("buynum")int buynum);
//根据purchase_list的bid更新repository
public int updateLendstock(@Param("bid") int bid)
//根据bid减少借阅书籍的数量
public int removeLendBook(@Param("bid")int id);
//根据bid减少购买书籍的数量
public int removeBuyBook(@Param("bid")int id);
//返回所有repository数据
public ArrayList<Repository> selectAll();
//更新库存
public int updateStock(@Param("bid")int bid, @Param("lendnum") int lendnum,@Param("buynum") int buynum);
}
@Mapper
public interface ReturnRecordDAO {
//根据bid减少借阅书籍的数量
public int insertRecord(ReturnRecord returnRecord);
//返回所有归还记录
public List<HashMap<String, Object>> selectAll();
//根据uid返回归还记录
public List<HashMap<String,Object>> select_AccordingUser(@Param("uid") String id);
}
@Mapper
public interface UserDAO {
//插入user数据
public int insertUser(User user);
//根据uid查询user数据
public User selectUserByUid(@Param("uid") String uid);
//返回所有的uid数据
public List<String> selectAllId();
}
其中一个功能的实现:
<---xml文件--->
Purchase_list.xml:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.library.dao.PurchaseListDAO">
<resultMap id="purchaseList" type="com.demo.library.dataobject.PurchaseList">
<id property="bid" column="bid" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="author" property="author" jdbcType="VARCHAR"/>
<result column="price" property="price" jdbcType="INTEGER"/>
<result column="lendstock" property="lendnum" jdbcType="INTEGER"/>
<result column="buystock" property="buynum" jdbcType="INTEGER"/>
</resultMap>
<insert id="purchaseBook">
insert into purchase_list (name, price, author, lendstock, buystock) VALUES
(#{book.name},#{book.price},#{book.author},#{lendnum},#{buynum});
</insert>
<update id="updatedata">
update purchase_list
set lendstock=#{lendnum},buystock=#{buynum}
where bid=#{bid};
</update>
<select id="getData" resultType="com.demo.library.dataobject.PurchaseList" >
select bid as bid, name as name, author as author, price as price, lendstock as lendnum, buystock as buynum from purchase_list
where name=#{name} and author=#{author};
</select>
<select id="getName" resultType="String">
select name from purchase_list
where author=#{author} and name=#{name};
</select>
<select id="getBidInt" resultType="java.lang.Integer">
select bid from purchase_list
where author=#{author} and name=#{name};
</select>
<select id="getLendnum" resultType="java.lang.Integer">
select lendstock from purchase_list where bid=#{bid}
</select>
<select id="getBuynum" resultType="java.lang.Integer">
select buystock from purchase_list where bid=#{bid}
</select>
</mapper>
3.4登录模块设计
HTML文件:
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>login</title>
<link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div style="text-align: center">
<h1>Welcome to the library system!</h1>
</div>
<div style="margin-top: 70px;text-align: center">
<p>tip:游客登录密码为000000,用户id为手机号</p>
</div>
<form action="/UI" method="get">
<div class="div_id">
<div>uid</div>
<input type="text" name="uid" class="id" placeholder="请输入id">
</div>
<div class="div_name">
<div>name</div>
<input type="text" name="name" class="name" placeholder="请输入姓名">
</div>
<div class="div_password">
<div>password</div>
<input type="password" name="password" class="password" placeholder="请输入密码">
</div>
<div class="button_div">
<button type="submit" class="button">login</button>
</div>
</form>
</body>
</html>
CSS文件:
body{
margin: 0;
padding: 0;
background: url("../img/background.png") no-repeat ;
background-size: cover;
}
h1{
font-weight: bold;
}
.id{
width: 200px;
height: 25px;
}
.password{
width: 200px;
height: 25px;
}
.name{
width: 200px;
height: 25px;
}
form{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.div_id{
margin-bottom: 20px;
}
.div_name{
margin-bottom: 20px;
}
.button_div{
margin-top: 10px;
width: 100%;
text-align: center;
}
.button{
color:white;
font-weight: normal;
background-color: forestgreen;
width: 80px;
height: 40px;
line-height: 30px;
border-radius: 5px;
}
3.5数据管理功能模块
管理员模块
进货模块:
public void addBook(Book book,int lendNum,int buyNum)
{
if(purchaseDAO.getBidInt(book)!=null)
{
int bid = purchaseDAO.getBidInt(book);
book.setBid(bid);
purchaseDAO.updatedata(bid, purchaseDAO.getLendnum(bid)+lendNum, purchaseDAO.getBuynum(bid)+buyNum );
repositoryDAO.updateStock(bid, purchaseDAO.getLendnum(bid), purchaseDAO.getBuynum(bid));
}
else
{
purchaseDAO.purchaseBook(book, lendNum, buyNum);
int bid = purchaseDAO.getBidInt(book);
book.setBid(bid);
repositoryDAO.addBook(book,lendNum,buyNum);
bookDAO.inertBook(book);
}
}
获取库存信息模块:
public List<RepositoryHTML> allBooks()
{
ArrayList<RepositoryHTML> arrayList = new ArrayList<>();
for (Repository repository : Repositorys()) {
Book book = bookDAO.selectBookById(repository.getBid());
RepositoryHTML repositoryHTML = new RepositoryHTML(book.getName(), book.getAuthor(), book.getPrice(), repository.getLendstock(), repository.getBuystock());
arrayList.add(repositoryHTML);
}
return arrayList;
}
查询所有用户的借阅记录模块:
public List<LendRecordHTML> LendRecordsHTML()
{
ArrayList<LendRecordHTML> arrayList = new ArrayList<>();
for (HashMap<String, Object> lendRecord : LendRecords()) {
int bid = (int)lendRecord.get("bid");
String uid = (String)lendRecord.get("uid");
LocalDateTime date_lend = (LocalDateTime) lendRecord.get("date_lend");
LocalDateTime date_return = (LocalDateTime) lendRecord.get("date_return");
User user = userDAO.selectUserByUid(uid);
Book book = bookDAO.selectBookById(bid);
LendRecordHTML lendRecordHTML=new LendRecordHTML(bid,user.getName(),book.getName(),date_lend,date_return);
arrayList.add(lendRecordHTML);
}
return arrayList;
}
查询所有用户的购买记录模块:
public List<BuyRecordsHTML> BuyRecordsHTML()
{
ArrayList<BuyRecordsHTML> arrayList = new ArrayList<>();
for (HashMap<String, Object> buyRecord : BuyRecords()) {
int bid = (int)buyRecord.get("bid");
String uid = (String)buyRecord.get("uid");
LocalDateTime date_buy = (LocalDateTime)buyRecord.get("date_buy");
User user = userDAO.selectUserByUid(uid);
Book book = bookDAO.selectBookById(bid);
BuyRecordsHTML buyRecordsHTML = new BuyRecordsHTML(bid, user.getName(), book.getName(), date_buy);
arrayList.add(buyRecordsHTML);
}
return arrayList;
}
查询所有用户的归还记录模块:
public List<ReturnRecordsHTML> ReturnRecordsHTML()
{
ArrayList<ReturnRecordsHTML> arrayList = new ArrayList<>();
for (HashMap<String, Object> returnRecord : ReturnRecords()) {
int bid = (int)returnRecord.get("bid");
String uid = (String)returnRecord.get("uid");
LocalDateTime date_return = (LocalDateTime)returnRecord.get("date_return");
User user = userDAO.selectUserByUid(uid);
Book book = bookDAO.selectBookById(bid);
ReturnRecordsHTML returnRecordsHTML = new ReturnRecordsHTML(bid, date_return, book.getName(), user.getName());
arrayList.add(returnRecordsHTML);
}
return arrayList;
}
用户
插入用户数据:
public void insertUser(User user)
{
List<String> ids = userDAO.selectAllId();
for (String id : ids) {
if(id.equals(user.getUid()))
return;
}
userDAO.insertUser(user);
}
根据用户id查询所有借阅记录:
public List<LendRecordHTML> LendRecordsHTML(String uid)
{
ArrayList<LendRecordHTML> arrayList = new ArrayList<>();
for (HashMap<String, Object> lendRecord : lendRecords(uid)) {
int bid = (int)lendRecord.get("bid");
LocalDateTime date_lend = (LocalDateTime) lendRecord.get("date_lend");
LocalDateTime date_return = (LocalDateTime) lendRecord.get("date_return");
User user = userDAO.selectUserByUid(uid);
Book book = bookDAO.selectBookById(bid);
LendRecordHTML lendRecordHTML=new LendRecordHTML(bid,user.getName(),book.getName(),date_lend,date_return);
arrayList.add(lendRecordHTML);
}
return arrayList;
根据用户id查询所有购买记录:
}
public List<BuyRecordsHTML> BuyRecordsHTML(String uid)
{
ArrayList<BuyRecordsHTML> arrayList = new ArrayList<>();
for (HashMap<String, Object> buyRecord : BuyRecords(uid)) {
int bid = (int)buyRecord.get("bid");
LocalDateTime date_buy = (LocalDateTime) buyRecord.get("date_buy");
User user = userDAO.selectUserByUid(uid);
Book book = bookDAO.selectBookById(bid);
BuyRecordsHTML buyRecordsHTML = new BuyRecordsHTML(bid, user.getName(), book.getName(), date_buy);
arrayList.add(buyRecordsHTML);
}
return arrayList;
}
根据用户id查询所有归还记录:
public List<ReturnRecordsHTML> ReturnRecordsHTML(String uid)
{
ArrayList<ReturnRecordsHTML> arrayList = new ArrayList<>();
for (HashMap<String, Object> returnRecord : ReturnRecords(uid)) {
int bid = (int)returnRecord.get("bid");
LocalDateTime date_return = (LocalDateTime) returnRecord.get("date_return");
User user = userDAO.selectUserByUid(uid);
Book book = bookDAO.selectBookById(bid);
ReturnRecordsHTML returnRecordsHTML = new ReturnRecordsHTML(bid, date_return, book.getName(), user.getName());
arrayList.add(returnRecordsHTML);
}
return arrayList;
}
查询所有能被借阅的书:
public List<Book> booksBeLend()
{
ArrayList<Book> arrayList = new ArrayList<>();
List<Repository> repositories = repositoryDAO.selectAll();
for (Repository repository : repositories) {
if(repository.getLendstock()!=0)
{
Book book = bookDAO.selectBookById(repository.getBid());
arrayList.add(book);
}
}
return arrayList;
}
查询所有能被购买的书:
public List<Book> booksBeBuy()
{
ArrayList<Book> arrayList = new ArrayList<>();
List<Repository> repositories = repositoryDAO.selectAll();
for (Repository repository : repositories) {
if(repository.getBuystock()!=0)
{
Book book = bookDAO.selectBookById(repository.getBid());
arrayList.add(book);
}
}
return arrayList;
}
查询所有能被归还的书:
public List<Book> booksBeReturn(String uid)
{
ArrayList<Book> arrayList = new ArrayList<>();
List<HashMap<String, Object>> maps = lendRecordDAO.select_Map_AccordingUser(uid);
for (HashMap<String, Object> map : maps) {
int bid = (int) map.get("bid");
Book book = bookDAO.selectBookById(bid);
arrayList.add(book);
}
return arrayList;
}
借书模块:
public void lend_A_Book(String uid,int bid, LocalDateTime lendTime,LocalDateTime returnTime)
{
LendRecord lendRecord = new LendRecord(uid, bid, lendTime, returnTime);
lendRecordDAO.insertRecord(lendRecord);
repositoryDAO.removeLendBook(bid);
}
买书模块:
public void buy_A_Book(String uid,int bid,LocalDateTime buyTime)
{
BuyRecord buyRecord = new BuyRecord(uid, bid, buyTime);
buyRecordDAO.insertRecord(buyRecord);
repositoryDAO.removeBuyBook(bid);
}
还书模块:
public void return_A_Book(String uid,int bid,LocalDateTime returnTime)
{
ReturnRecord returnRecord = new ReturnRecord(returnTime, bid, uid);
returnRecordDAO.insertRecord(returnRecord);
lendRecordDAO.deleteRecord(uid,bid);
repositoryDAO.updateLendstock(bid);
} |
|