借阅图书的操作主要是添加借阅记录:
在这张表上添加记录 , 并在 t_book这张表上扣除库存的操作
下面我们开始进行操作:
首先我们创建t_borrow表的实体类在book项目中:
package com.laosan.book.entity; import lombok. Data ; import javax.persistence.*; import java.io.Serializable; import java.util.Date; /** * 借阅图书实体类 */ @Data @Entity @Table (name= "t_borrow" ) public class Borrow implements Serializable { @Id @GeneratedValue (strategy = GenerationType. IDENTITY ) private Long brid ; private Long bid ; private Long mid ; private Date takedate ; private Date returndate ; private Date createtime ; }
然后在Dao层添加一个接口:
package com.laosan.book.repository; import com.laosan.book.entity.Borrow; import org.springframework.data.jpa.repository.JpaRepository; public interface BorrowRepository extends JpaRepository<Borrow, Long> { }
然后打开book项目的service层进行操作,写一个借阅图书的方法:
BookService
/**
* 借阅图书方法
* @param bid
* @param mobile
* @param takeDate
* @param returnDate
*/
void takeBookBookrow(Long bid, String mobile, Date takeDate, Date returnDate);
再写ServiceImpl先完成:
//库存是否为0的异常:
/**
* 库存不足自定义 异常处理
*/
public class BookLessException extends RuntimeException{
public BookLessException(String message) {
super(message);
}
}
//开始插入借书的记录 ,有个数据要先处理一下 mid
MemberDTO memberDTO=null;
所以在boot项目中也要有vo dto 和mamber的一样
引入依赖;
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
@EnableFeignClients //开启feign
vo:
package com.pb.vo;
import lombok.Data;
import java.io.Serializable;
/**
* 格式化数据的封装类
*/
@Data
public class ResultVo<T> implements Serializable {
private int code;
private String message;
private T data;
}
dto
package com.pb.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 用来存放Member对象的视图
*/
@Data
public class MemberDTO implements Serializable {
private Long mid;
private String name;
private String idno;
private String mobile;
private Date regdate;
private Date expdate;
}
clien:
package com.pb.client;
import com.pb.dto.MemberDTO;
import com.pb.vo.ResultVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
@FeignClient(name="member")
public interface MemberClient {
//调用member的服务请求为selectMemberByMobile的
@RequestMapping("/selectMemberByMobile")
ResultVo<MemberDTO> selectMemberByMobile(@RequestParam("mobile") String mobile);
}
serviceImpl
}
package com.ff.test.service.impl; import com.ff.test.client.MemberClient; import com.ff.test.dto.MemberDTO; import com.ff.test.entity.Book; import com.ff.test.entity.Borrow; import com.ff.test.exception.BookLessException; import com.ff.test.exception.BookNotFoundException; import com.ff.test.respository.BookRespository; import com.ff.test.respository.BorrowRespository; import com.ff.test.service.BookService; import com.ff.test.vo.ResultVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.List; import java.util.Optional;
@Service public class BookServiceImpl implements BookService {
@Autowired private BookRespository bookRespository;
@Autowired private MemberClient memberClient;
@Autowired private BorrowRespository borrowRespository;
@Override public List<Book> selectBookAll() { List<Book> list = bookRespository.findAll(); return list; }
/** * 根据Id查询图书信息 * @param bid * @return */ @Override public Book findById(Long bid) { Book book = null; Optional<Book> option = bookRespository.findById(bid); if(option.isPresent()) { book = option.get(); } else { throw new BookNotFoundException("图书未找到"); } return book; }
/** * 借书操作 * @param mobile * @param bid * @param takedate * @param returndate */ @Transactional @Override public void takeBookBorrow(String mobile, Long bid, Date takedate, Date returndate) { Book book = null; Optional<Book> option = bookRespository.findById(bid); if(option.isPresent()) { book = option.get(); } else { throw new BookNotFoundException("图书未找到"); }
//想想库存是否为0 if(book.getStock() <=0) { throw new BookLessException("库粗不足"); }
MemberDTO memberDTO = null; //开始插入借书的记录,有个数据先得处理下,mid ResultVO<MemberDTO> result = memberClient.selectMemberByMobile(mobile); if(result.getCode() == 200) { memberDTO = result.getData(); }
//借书记录操作插入 Borrow borrow = new Borrow(); borrow.setBid(bid); borrow.setMid(memberDTO.getMid()); borrow.setCreatetime(new Date()); borrow.setTakedate(takedate); borrow.setReturndate(returndate);
borrowRespository.save(borrow);
//修改库存 book.setStock(book.getStock()-1); bookRespository.saveAndFlush(book); } }
上面的服务我们做完了,那么现在我们来对客户端前端页面进行处理:
最后再html页面我们完成借书的操作:
//进行提交操作 $( "#btnSubmit" ). click ( function () { //获得图书的编号放到隐藏域id为bid的input标签里面 $( "#bid" ).val($( "#name" ).attr( "data-values" )); var params = $( "#frmBorrow" ).serialize(); //把表单中的所有数据记性序列化传给后台 //进行测试打印在控制台 //console.log(params); //控制台会有index.html?_ijt=sk4v02vbh5676k7vt6687cfljr:128 mobile=13834567543&name=java%E5%9F%BA%E7%A1%80&bid=1&takedate=2020-04-01&returndate=2020-04-15 //利用ajax进行访问后台的操作 $.ajax({ url : "http://localhost:9000/book/borrow" , type : "post" , data : params , dataType : "json" , success : function (json) { //进行数据的测试操作 //console.log("book:" + json); //进行判断操作 if (json. code == "0" ) { $. alert ( "借书成功" ); } else { $. alert (json. message ); } } }); })
具体的返回为:
失败信息为: