栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

BookStore,基于Controller,Service,Dao实现数据库的增删改查

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BookStore,基于Controller,Service,Dao实现数据库的增删改查

目录

准备工作:

1.maven引用

2.配置文件

后端

1.Controller

Dao

Service

Book

前端

显示页面

更新页面


准备工作:

1.maven引用

    
    
      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
      
  

2.配置文件

一些细节需要注意 例如:开启扫描 的包,数据库名,密码等


    
    
        
        
    


    
    
        
        
        
        
        
        
        
        
    


    

    
        
    

后端

1.Controller
@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";
    }
}

Dao
@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
@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);
    }
}

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" %>


    图书信息
    
        table{
            text-align: center;
            margin-left: auto;
            margin-right: auto;
            margin-top: 120px;
            border: 1px blueviolet solid;
            cellspacing:0px;
            cellpadding:10px;
        }
        form{
            margin-top: 60px;
            text-align: center;
            margin-left: auto;
            margin-right: auto;
        }
        th,td{
            border: black 1px solid;

        }
        



    
图书信息
编号 书名 作者 价格 上架时间 操作
${book.bookid} ${book.bookname} ${book.bookauthor} ${book.bookprice} 删除 修改
书名: 作者: 价格:

更新页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    form{
        text-align: center;
        margin-left: auto;
        margin-right: auto;
        margin-top: 120px;
        border: 1px blueviolet solid;
        cellspacing:0px;
        cellpadding:10px;
    }



书名:
作者:
价格:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286914.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号