编号:【BookCase-1】 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版本。另外依赖的包先不抽取版本,暂时直接依赖。
org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator mysql mysql-connector-java runtime org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 org.projectlombok lombok true cn.hutool hutool-all 5.7.3 commons-io commons-io 2.11.0 org.apache.commons commons-lang3 com.alibaba fastjson 1.2.78 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine
添加完成之后,如果依赖有问题,可以使用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
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 selectByAuthor(String author);
}
service 服务:
public interface BookService {
Book getBookById(Integer id);
Integer updateBookById(Book book);
Integer deleteBookById(Integer id);
Integer insertBook(Book book);
List 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 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("/delete/{author}")
public List 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 方面,这个算是最基础的。
后续所有的扩展都是在这个基础上修改。



