0
点赞
收藏
分享

微信扫一扫

springboot框架学习 图书管理系统的简单实现

at小涛 2022-04-19 阅读 81
spring boot

兄弟们!jvm与线程已经差不多了,接下来的10天时间要放在中间件的学习上了,而且对于spring源码的研究也还没有开始呢!今天上午在图书馆实现了springboot框架写的图书管理系统,很简单的小东西,增删改查以及登录验证。页面没有渲染所以勉强看吧!

一、数据表设计

图书列表:

用户列表:

        

 二、application.yml文件配置

由于我使用的是Druid数据源所以配置的也是Druid,后续也可以在业务逻辑层添加Redis缓存,小项目的话配置Redis没什么作用。这里采用的是thymeleaf模板引擎加载html文件。

server:
  port: 80

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: 123456
#    提供的模板引擎设置热部署,不使用cache,部署的文件类型是HTML5
  thymeleaf:
    cache: false
    mode: HTML5
  mvc:
    static-path-pattern: /static/**

三、pojo层(使用lombok插件实现快速开发)

1、user实体类

import lombok.Data;
@Data
public class User {
    private int userId;
    private String password;
}

 2、book实体类

import lombok.Data;
@Data
public class Book {
    private int bookId;
    private String bookName;
    private String bookPrice;
    private String bookType;
}

四、数据持久层(使用mybatis)

1、bookdao

import com.hlc.pojo.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookDao {
//    利用mybatis框架写好需要使用的数据库操作方法

    /*查看全部*/
    @Select("select * from test.bootsm_book")
    List<Book> allBook();
    /*按条件查询*/
    @Select("select * from test.bootsm_book where bookId = #{bookId}")
    Book queryById(Integer bookId);
    /*修改*/
    @Update("update test.bootsm_book set bookName=#{bookName},bookPrice=#{bookPrice},bookType=#{bookType} where bookId=#{bookId}")
    Boolean update(Book book);
    /*插入*/
    @Insert("insert into test.bootsm_book values (#{bookId},#{bookName},#{bookPrice},#{bookType})")
    Boolean save(Book book);
    /*按条件删除*/
    @Delete("delete from test.bootsm_book where bookId=#{bookId}")
    Boolean delete(Integer bookId);
    /*实现登录*/
    @Select("select * from test.bootsm_book where bookId=#{bookId} and bookName=#{bookName}")
    Boolean login(Integer bookId,String bookName);

}

2、userdao

import com.hlc.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserDao {
    /*登录权限赋予用户接口*/
    @Select("select * from test.bootsm_user where userId=#{userId} and password=#{password}")
    Boolean login(int userId,String password);
    /*注册*/
    @Insert("insert into test.bootsm_user values (#{userId},#{password}) ")
    Boolean saveU(User user);
}

五、业务层

1、bookservice与它的实现类

import com.hlc.pojo.Book;

import java.util.List;

public interface BookService {
    List<Book> allBook();
    Book queryById(Integer bookId);
    Boolean update(Book book);
    Boolean save(Book book);
    Boolean delete(Integer bookId);
    Boolean login(Integer bookId,String bookName);
}

import com.hlc.dao.BookDao;
import com.hlc.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("BookService")
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
    public List<Book> allBook() {
        return bookDao.allBook();
    }

    @Override
    public Book queryById(Integer bookId) {
        return bookDao.queryById(bookId);
    }

    @Override
    public Boolean update(Book book) {
        return bookDao.update(book);
    }

    @Override
    public Boolean save(Book book) {
        return bookDao.save(book);
    }

    @Override
    public Boolean delete(Integer bookId) {
        return bookDao.delete(bookId);
    }

    @Override
    public Boolean login(Integer bookId, String bookName) {
        return bookDao.login(bookId,bookName);
    }
}

2、userservice与它的实现类

import com.hlc.pojo.User;

public interface UserService {
    Boolean login(int userId,String password);
    Boolean saveU(User user);
}


import com.hlc.dao.UserDao;
import com.hlc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("UserService")
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;

    @Override
    public Boolean login(int userId, String password) {
        return userDao.login(userId,password);
    }

    @Override
    public Boolean saveU(User user) {
        return userDao.saveU(user);
    }
}

六、控制层

1、bookcontrller

@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("books")
    public ModelAndView allBook(){
        ModelAndView modelAndView = new ModelAndView();
        List<Book> bookList = bookService.allBook();
        modelAndView.addObject("bookList",bookList);
        modelAndView.setViewName("list");
        return modelAndView;
    }

    @RequestMapping("query")
    /*这里的查询结果就是一条没有页面的json数据*/
    @ResponseBody
    public Book query(Integer bookId){
        return bookService.queryById(bookId);
    }

    /*先到add页面提交保存数据,再由真正的save方法接收数据并操作数据库*/
    @RequestMapping("add")
    public ModelAndView add(){
        ModelAndView modelAndView =  new ModelAndView("add");
        return modelAndView;
    }
    @RequestMapping("save")
    public ModelAndView save(Book book){
        ModelAndView modelAndView = new ModelAndView();
        bookService.save(book);
        List<Book> bookList = bookService.allBook();
        modelAndView.addObject("bookList",bookList);
        modelAndView.setViewName("list");
        return modelAndView;
    }
/*删除的思路就是前端携带参数传递给后端,直接进行数据库操作后再进行加载列表显示!*/
    @RequestMapping("delete/{bookId}")
    public ModelAndView del(@PathVariable("id") Integer bookId){
        ModelAndView modelAndView = new ModelAndView();
        bookService.delete(bookId);
        List<Book> bookList = bookService.allBook();
        modelAndView.addObject("bookList",bookList);
        modelAndView.setViewName("list");
        return modelAndView;
    }
    /*修改操作的实现也是一样,先跳转到修改的表单页面,再通过访问路由调用修改方法*/
    @RequestMapping("uForm")
    public ModelAndView uForm(ModelAndView modelAndView ){
        modelAndView.setViewName("uForm");
        return modelAndView;
    }
    @RequestMapping("update")
    public ModelAndView update(Book book){
        ModelAndView modelAndView = new ModelAndView();
        bookService.update(book);
        List<Book> bookList = bookService.allBook();
        modelAndView.addObject("bookList",bookList);
        modelAndView.setViewName("list");
        return modelAndView;
    }

}

2、usercontroller

@RestController
public class UserController {
    @Autowired
    private UserService userService;
/*------------------------------------------------------------------------------------*/
    /*这个复写就是为了实现登录成功返回书籍列表的操作,待优化!*/
    @Autowired
    private BookService bookService;
    public ModelAndView allBook(){
        ModelAndView modelAndView = new ModelAndView();
        List<Book> bookList = bookService.allBook();
        modelAndView.addObject("bookList",bookList);
        modelAndView.setViewName("list");
        return modelAndView;
    }
/*----------------------------------------------------------------------------------*/

    /*实现登录功能,原理很简单,登录成功就可以访问,不成功就不能进入其他页面,跳转错误提示页面*/
    @RequestMapping("login")
    public ModelAndView login(ModelAndView modelAndView){
        modelAndView.setViewName("login");
        return modelAndView;
    }
    @RequestMapping("index")
    public ModelAndView index(int userId,String password){
        Boolean login = userService.login(userId,password);
        if (login) {
            ModelAndView modelAndView = allBook();
            return modelAndView;
        }
        return new ModelAndView("error");
    }

    /*实现注册功能*/
    @RequestMapping("addU")
    public ModelAndView add(ModelAndView modelAndView){
        modelAndView.setViewName("addU");
        return modelAndView;
    }
    @RequestMapping("saveU")
    public ModelAndView saveU(User user){
        ModelAndView modelAndView =new ModelAndView();
        Boolean save = userService.saveU(user);
        if(save) {
            modelAndView.setViewName("login");
            return modelAndView;
        }
        return new ModelAndView("addU");
    }
}

七、web层(篇幅太大,只展示利用th标签遍历BookList的列表页面)

list.html

<!DOCTYPE html>
<html lang="zh" xmlns:th="Thymeleaf 官网网址">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>书目列表</h2>
<div style="align-content: center">
    <form action="/query" method="post">
        搜索书籍<input type="text" name="bookId"><input type="submit">
    </form>

</div>

<table>
    <thead>
        <tr>
            <th>书号</th>
            <th>书名</th>
            <th>图书类型</th>
            <th>图书价格</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="book:${bookList}" >
            <td th:text="${book.bookId}"></td>
            <td th:text="${book.bookName}"></td>
            <td th:text="${book.bookType}"></td>
            <td th:text="${book.bookPrice}"></td>
            <!--这里的删除需要携带参数,要利用jq函数-->
            <td><button type="button" id="del">删除</button>||<button><a href="/uForm">修改</a></button></td>
        </tr>
    </tbody>
</table>
<button><a href="/add">新增图书</a></button>
<button><a href="/login">返回</a> </button>
</body>
</html>

八、导入的依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--导入静态加载的模板引擎坐标-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
举报

相关推荐

0 条评论