[从零开始的账本项目04] 账本模块

225 阅读2分钟

在之前写完了账户模块后, 进行账本模块的构思的时候. 越想越有问题. 发现自己数据库关系图画的有问题. 少了一个账单模块. 因此需要新增一个"账单模块". 账本模块的设计几乎与账户模块一致. 因此本次基本只是进行一个代码的贴.

新的数据库关系图如下:

image.png

因此应该是一个用户可以拥有多个账本和多个账户.
而一个账本或一个账户中可以包含多个账单.

因此本次Book账本模块的设计也就与Account账户模块趋同.

  • name: 账本名
  • userId: 该账本的拥有者, 在数据库中设置外键对应为User表中的id. 使其能够做到级联删除, 即某个用户注销时, 该账本自动删除.
  • type: 账本类型, 由用户或客户端定义.

以下的内容则与Account账户模块基本一致.
[[从零开始的账本项目03] 账户模块](juejin.cn/post/702338…

Book

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Book {
    private Long id;
    private String name;
    private Long userId;
    private String type;
}

BookDao


@Mapper
public interface BookDao {

    Integer saveBook(@Param("book") Book book);

    Integer deleteBookById(@Param("id") Long id);

    Integer updateBookById(@Param("book") Book book);

    Book getBookById(@Param("id") Long id);

    List<Book> getBooksByUserId(@Param("userId") Long userId);
}
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.bngel.bngelbookbookprovider8002.dao.BookDao">

    <insert id="saveBook">
        insert into bngel_book(`name`, `user_id`, `type`) values (#{book.name}, #{book.userId}, #{book.type});
    </insert>

    <delete id="deleteBookById">
        delete from bngel_book where `id` = #{id};
    </delete>

    <update id="updateBookById">
        update bngel_book
        <set>
            <if test="book.name != null">`name`=#{book.name}</if>
            <if test="book.type != null">`type`=#{book.type}</if>
        </set>
        where `id`=#{book.id};
    </update>

    <select id="getBookById" resultType="cn.bngel.bngelbookbookprovider8002.bean.Book">
        select * from bngel_book where `id`=#{id};
    </select>

    <select id="getBooksByUserId" resultType="cn.bngel.bngelbookbookprovider8002.bean.Book">
        select * from bngel_book where `user_id`=#{userId};
    </select>
</mapper>

BookService

public interface BookService {

    Integer saveBook(Book book);

    Integer deleteBookById(Long id);

    Integer updateBookById(Book book);

    Book getBookById(Long id);

    List<Book> getBooksByUserId(Long userId);
}
@Service
public class BookServiceImpl implements BookService{

    @Autowired
    private BookDao bookDao;


    @Override
    public Integer saveBook(Book book) {
        return bookDao.saveBook(book);
    }

    @Override
    public Integer deleteBookById(Long id) {
        return bookDao.deleteBookById(id);
    }

    @Override
    public Integer updateBookById(Book book) {
        return bookDao.updateBookById(book);
    }

    @Override
    public Book getBookById(Long id) {
        return bookDao.getBookById(id);
    }

    @Override
    public List<Book> getBooksByUserId(Long userId) {
        return bookDao.getBooksByUserId(userId);
    }
}

BookController

@RestController
@Slf4j
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping("/book")
    public CommonResult saveBook(@RequestBody Book book) {
        Integer result = bookService.saveBook(book);
        if (result == 1) {
            return CommonResult.commonSuccessResult();
        }
        else {
            return CommonResult.commonFailureResult();
        }
    }

    @DeleteMapping("/book")
    public CommonResult deleteBookById(@RequestParam("id") Long id) {
        Integer result = bookService.deleteBookById(id);
        if (result >= 1) {
            return CommonResult.commonSuccessResult();
        }
        else {
            return CommonResult.commonFailureResult();
        }
    }

    @PutMapping("/book")
    public CommonResult updateBookById(@RequestBody Book book) {
        Integer result = bookService.updateBookById(book);
        if (result == 1) {
            return CommonResult.commonSuccessResult();
        }
        else {
            return CommonResult.commonFailureResult();
        }
    }

    @GetMapping("/book")
    public CommonResult getBookById(@RequestParam("id") Long id) {
        Book result = bookService.getBookById(id);
        if (result != null) {
            return new CommonResult(CommonResult.SUCCESS_CODE, result, CommonResult.SUCCESS_MESSAGE);
        }
        else {
            return CommonResult.commonFailureResult();
        }
    }

    @GetMapping("/book/{userId}")
    public CommonResult getBooksByUserId(@PathVariable("userId") Long userId) {
        List<Book> results = bookService.getBooksByUserId(userId);
        if (results != null) {
            return new CommonResult(CommonResult.SUCCESS_CODE, results, CommonResult.SUCCESS_MESSAGE);
        }
        else {
            return CommonResult.commonFailureResult();
        }
    }
}

接下来的话应该就是算是核心模块的最后一块拼图.
账单模块.


欢迎各位来对我的小项目提出各种宝贵的意见, 这对我的进步非常重要, 谢谢大家.
GitHub地址: Bngel/bngelbook