写作目的:为积极响应戴鸿君老师对项目实训内容的建设性意见,特写本系列个人纪实,记录团队在项目实训的工作中个人的贡献。
数据库使用mysql数据库,用于搭建基本的文书系统框架
数据库中共十二个实体,包括用户,评论,点赞,文件,菜单,角色,文书,公告等。分别对应每个实体的相应的功能。
后端连接数据库:
服务器配置 (server
)
port: 9090
: 这行配置了应用程序运行在9090端口上。
Spring 配置 (spring
)
datasource
: 这个部分包含了数据源的配置,用于连接数据库。driver-class-name
: 指定了 MySQL 数据库的 JDBC 驱动类。url
: 指定了数据库的 JDBC URL,包括数据库服务器地址、端口号、数据库名,以及时区设置(serverTimezone=GMT%2b8
)。username
: 数据库的用户名。password
: 数据库的密码。
servlet
: 这个部分配置了 Spring MVC 的 Servlet 参数。multipart
: 配置了文件上传的相关参数。max-file-size
: 配置了单个文件大小的最大限制为100MB。max-request-size
: 配置了请求的最大大小为100MB。
MyBatis 配置 (mybatis
)
mapper-locations
: 指定了 MyBatis 的 XML 映射文件的位置,这里使用了类路径下的mapper
目录,并且扫描所有.xml
文件。
MyBatis-Plus 配置 (mybatis-plus
)
configuration
: 这个部分配置了 MyBatis-Plus 的一些参数。log-impl
: 指定了 MyBatis-Plus 的日志实现类,这里使用了StdOutImpl
,意味着日志信息将被输出到控制台。
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bs_blog?serverTimezone=GMT%2b8
username: ****
password: ****
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
mybatis:
mapper-locations: classpath:mapper/*.xml #扫描所有mybatis的xml文件
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
数据库结构:
后端采用标准的springMVC三层架构:将实体、服务、接口分开设计,前端只于后端接口对接,由此实现前后端分离,数据库配置Spring,实现用mybatis-plus框架,对数据库进行基本的增删改查以及进阶的各种模糊查询、分类查询与返回等操作。
特别案例,文书评论树的实现:
/**
* 文书评论树结构接口
* @param foreignId 博客id
* @return
*/
@GetMapping("/tree")
public Result list(@RequestParam Integer foreignId) {
List<Comment> list = commentService.list(new LambdaQueryWrapper<Comment>().eq(Comment::getBlogId,foreignId));
// 给评论赋值用户相关属性
list.stream().forEach(comment -> {
if (null != comment.getPid()){
comment.setParentName(userService.getById(comment.getUserId()).getNickname());
}
comment.setUsername(userService.getById(comment.getUserId()).getNickname());
comment.setImg(userService.getById(comment.getUserId()).getAvatarUrl());
});
// 一级评论
List<Comment> first = list.stream().filter(comment -> comment.getPid() == null).collect(Collectors.toList());
for (Comment comment : first) {
Integer pid = comment.getId();
// 二级评论
List<Comment> second = list.stream().filter(comment1 -> Objects.equals(pid, comment1.getPid())).collect(Collectors.toList());
//将子评论放到children集合里
comment.setChildren(second);
}
return Result.success(first);
}