【SSM基础】初步搭建基础服务

【SSM基础】初步搭建基础服务

其他文章: bookcase微服务实战

1、前言

最近想把之前学习的内容归纳总结下,所以写了个小项目,准备从SSM开始。

假如想快速使用SpringBoot + SSM 搭建一个可以使用的小项目,可以按照我这里流程。

2、表设计

创建书籍表,书籍和作者唯一,书号唯一。

CREATE TABLE `book`
(
    `id`               int(11)      NOT NULL AUTO_INCREMENT,
    `book_name`        varchar(255) NOT NULL COMMENT '书名',
    `book_author`      varchar(100) NOT NULL COMMENT '作者',
    `book_no`          varchar(50)  DEFAULT NULL COMMENT '书号',
    `press`            varchar(255) DEFAULT NULL COMMENT '出版社',
    `publication_time` datetime     DEFAULT NULL COMMENT '出版时间',
    `create_time`      datetime     DEFAULT NULL,
    `update_time`      datetime     DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `un_book_name_author` (`book_name`, `book_author`),
    UNIQUE KEY `un_book_no` (`book_no`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 11
  DEFAULT CHARSET = utf8mb4;

并插入基本数据10条:

insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test1', 'zhangsan', 'no123', '海淀出版社', '2020-01-01 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test2', 'zhangsan', 'no124', '海淀出版社', '2020-01-02 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test3', 'zhangsan', 'no125', '海淀出版社', '2020-01-03 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test4', 'zhangsan', 'no126', '海淀出版社', '2020-01-04 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test5', 'zhangsan', 'no127', '海淀出版社', '2020-01-05 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test6', 'zhangsan', 'no128', '海淀出版社', '2020-01-06 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test7', 'zhangsan', 'no129', '海淀出版社', '2020-01-07 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test8', 'zhangsan', 'no131', '海淀出版社', '2020-01-08 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test9', 'zhangsan', 'no132', '海淀出版社', '2020-01-09 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');
insert into book(book_name, book_author, book_no, press, publication_time, create_time, update_time)
VALUES ('test10', 'zhangsan', 'no133', '海淀出版社', '2020-01-10 00:00:00', '2020-01-01 00:00:00', '2020-01-01 00:00:00');

3、初始化项目

创建SpringBoot 项目进行初始化。

3.1、添加maven依赖

添加依赖,这里使用的是SpringBoot 2.3.7.RELEASE版本。另外依赖的包先不抽取版本,暂时直接依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--orm + 数据库-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

<!--tools-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.3</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.78</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>

添加完成之后,如果依赖有问题,可以使用reimport重新导入依赖。

3.2、添加application.yml配置

这里只使用了最简单的配置,Mybatis 也是只配置了Xml 文件地址。

server:
  port: 8080

spring:
  application:
    name: bk0
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bookcase?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
    username: root
    password: root


mybatis:
  mapper-locations: classpath:mapper/*.xml
  # 作用: 在xml 中只需要输入对象名称,不需要包名
  type-aliases-package: com.fans.bk0.entity

3.3、生成基础服务

  • 自动生成Mapper接口和Xml 文件
    这里使用了Free Mybatis plugin 自动生成基础代码,当然只是Mapper接口和Xml 文件,Mapper的接口放在src/main/resources的 mapper包下,xml 文件放在 src/main/resources 下的mapper 包下。

  • 创建service和impl

  • 创建controller

结构图如下:

在这里插入图片描述

3.4、接口编写介绍

这里填写了基本的CRUD接口:

mapper接口:
这里是自动生成的代码,只有selectByAuthor 是新增的。

注意: 按照规范应该在接口上添加注释,这里大部分是自动生成的,我这里图省事儿没有加。
Entity:

@Data
public class Book implements Serializable {
    private Integer id;

    /**
     * 书名
     */
    private String bookName;

    /**
     * 作者
     */
    private String bookAuthor;

    /**
     * 书号
     */
    private String bookNo;

    /**
     * 出版社
     */
    private String press;

    /**
     * 出版时间
     */
    private LocalDateTime publicationTime;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    private static final long serialVersionUID = 1L;
}

Mapper:

public interface BookMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    List<Book> selectByAuthor(String author);
}

service 服务:

public interface BookService {

    /**
     * 根据主键查询书籍信息
     * @param id
     * @return
     */
    Book getBookById(Integer id);

    /**
     * 更新书籍信息
     * @param book
     * @return
     */
    Integer updateBookById(Book book);

    /**
     * 更新书籍信息
     * @param id
     * @return
     */
    Integer deleteBookById(Integer id);

    /**
     * 插入书籍数据
     * @param book
     * @return
     */
    Integer insertBook(Book book);

    /**
     * 查询作者所有的作品
     * @param author
     * @return
     */
    List<Book> getBooksByAuthor(String author);

}

impl:

@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {

    private final BookMapper bookMapper;

    @Override
    public Book getBookById(Integer id) {
        return bookMapper.selectByPrimaryKey(id);
    }

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

    @Override
    public Integer deleteBookById(Integer id) {
        return bookMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer insertBook(Book book) {
        return bookMapper.insert(book);
    }

    @Override
    public List<Book> getBooksByAuthor(String author) {
        return bookMapper.selectByAuthor(author);
    }
}

Controller:

@RestController
@RequiredArgsConstructor
@RequestMapping("/book")
public class BookController {

    private final BookService bookService;

    @GetMapping("/get/{id}")
    public Book getBookById(@PathVariable Integer id) {
        return bookService.getBookById(id);
    }

    @PostMapping("/updateBookById")
    public Integer updateBookById(@RequestBody Book book) {
        return bookService.updateBookById(book);
    }

    @GetMapping("/delete/{id}")
    public Integer deleteBookById(Integer id) {
        return bookService.deleteBookById(id);
    }

    @PostMapping("/insert")
    public Integer insertBook(@RequestBody Book book) {
        return bookService.insertBook(book);
    }

    @GetMapping("/getAuthor/{author}")
    public List<Book> getBooksByAuthor(@PathVariable String author) {
        return bookService.getBooksByAuthor(author);
    }

}

3.5、接口调用

写完之后运行服务。

通过浏览器执行:

http://localhost:8080/book/get/1

响应数据:

{"id":1,"bookName":"test1","bookAuthor":"author1","bookNo":"no001","press":"海淀出版社","publicationTime":"2022-03-15T22:51:23","createTime":"2022-03-15T22:51:26","updateTime":"2022-03-15T22:51:29"}

4、总结

目前在Java Web 方面,这个算是最基础的。
后续所有的扩展都是在这个基础上修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值