当前位置:首页 » 《随便一记》 » 正文

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

12 人参与  2022年03月02日 16:49  分类 : 《随便一记》  评论

点击全文阅读


目录

准备工作:

1.maven引用

2.配置文件

后端

1.Controller

Dao

Service

Book

前端

显示页面

更新页面


准备工作:

1.maven引用

<dependencies>
    <!--spring 核心包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>

    <!--日志-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.8.0-alpha0</version>
      <scope>test</scope>
    </dependency>

    <!--j2ee相关包 servlet、jsp、jstl-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.11.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.11.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.20</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.jetbrains</groupId>
      <artifactId>annotations-java5</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>RELEASE</version>
          <scope>compile</scope>
      </dependency>
  </dependencies>

2.配置文件

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

<context:component-scan base-package="com"></context:component-scan>
    <mvc:annotation-driven/>
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/><!--设置JSP文件的目录位置-->
        <property name="suffix" value=".jsp"/>
    </bean>


    <!--  DriverManagerDataSource 数据库打开和关闭连接  -->
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"  id="dataSource">
        <!-- 1.1.数据库驱动 -->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <!-- 1.2.连接数据库的url -->
        <property name="url" value="jdbc:mysql://localhost:3306/bookstore?characterEncoding=utf8&amp;serverTimezone=UTC"/>
        <!-- 1.3.连接数据库的用户名 -->
        <property name="username" value="root"></property>
        <!-- 1.4.连接数据库的密码 -->
        <property name="password" value="123456"></property>
    </bean>


    <!--    数据库增删改查的工具类-->

    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

后端

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<Book> query(String sql, Object[] param) {
        RowMapper<Book> 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<Book> 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" %>
<html>
<head>
    <title>图书信息</title>
    <style>
        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;

        }
        </style>
</head>
<body>
<table>
    <caption style="align:center;">图书信息</caption>
    <tr>
        <th>编号</th>
        <th>书名</th>
        <th>作者</th>
        <th>价格</th>
        <th>上架时间</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${bookList}" var="book"  varStatus="status">
    <tr>
        <td>${book.bookid}</td>
        <td>${book.bookname}</td>
        <td>${book.bookauthor}</td>
        <td>${book.bookprice}</td>
        <td ><fmt:formatDate value="${book.date}" type="date"/></td>
        <td>
            <a href="http://localhost:8080/SpringMVC_war/deleteById/${book.bookid}">删除</a>
            <a href="http://localhost:8080/SpringMVC_war/update/${book.bookid}" >修改</a>
        </td>
    </tr>
    </c:forEach>
</table>
<form  action="http://localhost:8080/SpringMVC_war/add" method="get">
    书名:<input type="text" name="bookname" >
    作者:<input type="text" name="bookauthor" >
    价格:<input type="text" name="bookprice" >
    <input type="submit" value="增加">
</form>
</body>
</html>

更新页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<style>
    form{
        text-align: center;
        margin-left: auto;
        margin-right: auto;
        margin-top: 120px;
        border: 1px blueviolet solid;
        cellspacing:0px;
        cellpadding:10px;
    }
</style>
<body>
<form action="http://localhost:8080/SpringMVC_war/updatebook" method="get">
    <input type="hidden" name="bookid" value="${bookid}"><br>
    书名:<input type="text" name="bookname"><br>
    作者:<input type="text" name="bookauthor"><br>
    价格:<input type="text" name="bookprice"><br>
    <input type="submit"><br>
</form>

</body>
</html>


点击全文阅读


本文链接:http://zhangshiyu.com/post/35662.html

页面  数据库  书名  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1