目录
准备工作:
1.maven引用
2.配置文件
后端
1.Controller
Dao
Service
Book
前端
显示页面
更新页面
org.springframework
spring-core
5.3.8
org.springframework
spring-web
5.3.8
org.springframework
spring-webmvc
5.3.8
org.springframework
spring-aop
5.3.8
org.springframework
spring-context-support
5.2.12.RELEASE
org.slf4j
slf4j-log4j12
1.8.0-alpha0
test
javax.servlet
javax.servlet-api
4.0.1
provided
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
junit
junit
4.11
test
com.fasterxml.jackson.core
jackson-core
2.11.0
com.fasterxml.jackson.core
jackson-databind
2.11.0
org.springframework
spring-core
5.3.8
org.springframework
spring-beans
5.3.8
org.springframework
spring-context
5.3.8
org.springframework
spring-context-support
5.3.8
org.springframework
spring-expression
5.3.8
commons-logging
commons-logging
1.2
mysql
mysql-connector-java
8.0.20
org.springframework
spring-jdbc
5.3.8
org.springframework
spring-tx
5.3.8
org.springframework
spring-aop
5.3.8
jstl
jstl
1.2
org.jetbrains
annotations-java5
RELEASE
compile
org.junit.jupiter
junit-jupiter
RELEASE
compile
一些细节需要注意 例如:开启扫描 的包,数据库名,密码等
@Controller
public class BookController {
@Autowired
BookService bookService;
@RequestMapping("/bookStore")
public String bookStore(@org.jetbrains.annotations.NotNull Model model){
model.addAttribute("bookList",bookService.getBookList());
return "bookStore";
}
@RequestMapping("/deleteById/{id}")
public String deleteById(@PathVariable("id")Integer bookid){
bookService.deleteBook(bookid);
return "redirect:/bookStore";
}
@RequestMapping("updatebook")
public String updateById(@RequestParam("bookid") Integer bookid, @RequestParam("bookname") String bookname, @RequestParam("bookauthor") String bookauthor, @RequestParam("bookprice") BigDecimal bookprice, HttpSession session){
bookService.updateBook(bookid,bookname,bookauthor,bookprice);
return "redirect:/bookStore";
}
@RequestMapping("update/{id}")
public String update(Model model,@PathVariable("id") Integer bookid){
model.addAttribute("bookid",bookid);
return "update";
}
@RequestMapping("add")
public String addById(Book book){
bookService.addBook(book);
return "redirect:/bookStore";
}
}
@Repository
public class BookDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List query(String sql, Object[] param) {
RowMapper rowMapper=new BeanPropertyRowMapper<>(Book.class);
return jdbcTemplate.query(sql,rowMapper,param);
}
public void delete(String sql,Object[] param){
jdbcTemplate.update(sql,param);
}
public void add(Book book){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 0);
date = calendar.getTime();
jdbcTemplate.update("insert into bookinfo value (null,?,?,?,?)",book.getBookname(),book.getBookauthor(),book.getBookprice(),sdf.format(date));
}
public void update(Book book){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 0);
date = calendar.getTime();
jdbcTemplate.update("update bookinfo set bookname =?,bookauthor=?,bookprice=?,date=? where bookid=?",book.getBookname(),book.getBookauthor(),book.getBookprice(),sdf.format(date),book.getBookid());
}
}
@Service
public class BookService {
@Autowired
private BookDao bookDao;
public List getBookList(){
return bookDao.query("select * from bookinfo",null);
}
public void deleteBook(int bookid){
String sql="delete from bookinfo where bookid="+bookid;
bookDao.delete(sql,null);
}
public void addBook(Book book){
bookDao.add(book);
}
public void updateBook(int bookid, String bookname, String bookauthor, BigDecimal bookprice){
Book book=new Book();
book.setBookname(bookname);
book.setBookid(bookid);
book.setBookauthor(bookauthor);
book.setBookprice(bookprice);
bookDao.update(book);
}
}
public class Book {
private int bookid;
private String bookname;
private String bookauthor;
private BigDecimal bookprice;
private Date date;
public String getBookauthor() {
return bookauthor;
}
public void setBookauthor(String bookauthor) {
this.bookauthor = bookauthor;
}
public BigDecimal getBookprice() {
return bookprice;
}
public void setBookprice(BigDecimal bookprice) {
this.bookprice = bookprice;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
}
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
图书信息
图书信息
编号
书名
作者
价格
上架时间
操作
${book.bookid}
${book.bookname}
${book.bookauthor}
${book.bookprice}
删除
修改
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title