目录
13. 类别管理--根据父级类别查询其所有子级类别--持久层
14. 类别管理--根据父级类别查询其所有子级类别--业务逻辑层
15. 类别管理--根据父级类别查询其所有子级类别--控制器层
配置依赖
应该在Project下创建某个Module,用于开发商品管理相关的功能,为了便于过渡到后续将要使用的微服务架构(需要将业务逻辑层的接口声明在专门的Module中,便于被其它微服务Module依赖),商品管理的Module需要再细分为2个,所以,目前项目结构应该是:
cgb2202-csmall-server[project]
csmall-product
csmall-product-webapi
csmall-product-service
当存在Project和Module的“父子关系”或后续可能存在Module与Module的“父子关系”时,应该在父级的pom.xml
中添加:
<packaging>pom</packaging>
并且,在父级的pom.xml
中,通过<modules>
节点配置各子级Module,例如:
<modules>
<module>csmall-product</module>
</modules>
创建Module的操作步骤为:在cgb2202-csmall-server
下创建csmall-product
,再在csmall-product
下创建csmall-product-webapi
。
当Project和各Module创建出来后,调整各pom.xml
文件。
最终,cgb2202-csmall-server
根级Project的pom.xml
为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Spring Boot父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 当前项目的信息 -->
<groupId>cn.tedu</groupId>
<artifactId>csmall-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 聚合项目中的父级Project或父级Module都应该做以下配置 -->
<packaging>pom</packaging>
<!-- 当前Project的各子级Module -->
<modules>
<module>csmall-product</module>
</modules>
<!-- 属性配置,主要配置各依赖项的版本号对应的“变量” -->
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.5.9</spring-boot.version>
<mybatis-boot.version>2.2.2</mybatis-boot.version>
<mysql.version>8.0.28</mysql.version>
<lombok.version>1.18.22</lombok.version>
<druid.version>1.1.20</druid.version>
</properties>
<!-- 依赖管理,主要管理各依赖项的版本,使得子级Module添加依赖时不必指定版本 -->
<dependencyManagement>
<dependencies>
<!-- Spring Boot Validation:验证请求参数的基本格式 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Web:支持Spring MVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Mybatis Spring Boot:Mybatis及对Spring Boot的支持 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-boot.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>${mysql.version}</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>${lombok.version}</version>
</dependency>
<!-- Druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- Spring Boot Test:测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
而csmall-product
的pom.xml
为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父级项目 -->
<parent>
<groupId>cn.tedu</groupId>
<artifactId>csmall-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- 当前项目的信息 -->
<groupId>cn.tedu</groupId>
<artifactId>csmall-product</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 聚合项目中的父级Project或父级Module都应该做以下配置 -->
<packaging>pom</packaging>
<!-- 当前Project的各子级Module -->
<modules>
<module>csmall-product-webapi</module>
</modules>
</project>
最后,在csmall-product-webapi
的pom.xml
为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父级项目 -->
<parent>
<groupId>cn.tedu</groupId>
<artifactId>csmall-product</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- 当前项目的信息 -->
<groupId>cn.tedu</groupId>
<artifactId>csmall-product-webapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 当前项目需要使用的依赖项 -->
<dependencies>
<!-- Spring Boot Validation:验证请求参数的基本格式 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Spring Boot Web:支持Spring MVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mybatis Spring Boot:Mybatis及对Spring Boot的支持 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- Spring Boot Test:测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
1. 类别管理--添加类别--持久层
1.1. 配置
由于目前是本项目第1次开发持久层,必须先完成必要的配置,包括:
- 连接到数据库(配置连接数据库的参数)
- 使用
@MapperScan
指定接口所在的包 - 配置
mybatis.mapper-locations
指定XML文件的位置
应该先在csmall-product-webapi
的src\main\resources
下创建application-dev.properties
文件,在其中添加连接数据库的参数:
# 连接数据库的配置信息
spring.datasource.url=jdbc:mysql://localhost:3306/mall_pms?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
然后,在application.properties
中添加配置:
# 激活Profile配置
spring.profiles.active=dev
# 连接数据库的固定配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# Mybatis的XML文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
然后,需要在src/main/resources
下自行创建mapper
文件夹,与以上配置对应。
还需要自行创建配置类,用于配置Mapper接口文件所在的包,则在cn.tedu.csmall.product.webapi
下创建mapper
子包,并在cn.tedu.csmall.product.webapi
下创建config.MybatisConfiguration
配置类,进行配置:
package cn.tedu.csmall.product.webapi.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("cn.tedu.csmall.product.webapi.mapper")
public class MybatisConfiguration {
}
完成后,可以尝试测试连接到数据库,则在src/test/java
下找到默认即存在的测试类,编写并执行测试:
package cn.tedu.csmall.product.webapi;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
@SpringBootTest
class CsmallProductWebapiApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() {
}
@Test
void testConnection() {
Assertions.assertDoesNotThrow(() -> {
dataSource.getConnection();
});
}
}
执行整个测试,应该能够通过测试。
2.2. 规划需要执行的SQL语句
此次需要执行的是“增加类别”,其数据操作的本质是向pms_category
表中插入数据,需要执行的SQL语句大致是:
insert into pms_category (除了id以外的字段的列表) values (与字段列表匹配的各字段值);
具体表现为:
insert into pms_category (name, parent_id, depth, keywords, sort, icon, enable, is_parent, is_display, gmt_create, gmt_modified) values (值列表);
另外,还应该考虑在“添加类别”时,是否需要执行相关的检查,因为这些检查很可能是通过查询数据库来实现的,则在持久层也需要实现这些功能!
暂定规则“类别的名称不允许重复”,则后续在Service层进行处理时,应该先根据尝试添加的类别的名称进行查询,如果查询结果为null
,表示此名称对应的类别尚不存在,将允许添加,如果查询结果不为null
,表示此名称对应的类别已经存在,将不允许添加!
要实现以上检查的效果,需要执行的SQL语句可以是:
select * from pms_category where name=?;
或者:
select count(*) from pms_category where name=?;
以上2种做法,第1种做法的查询效率相对较低,或者说性能消耗略高,第2种做法的查询性能消耗更低,但是,第1种做法的查询可能具有复用性,而第2种做法的查询的复用性相对较低。
暂定可以使用以上第1种做法。
3.3. 接口与抽象方法
在插入数据之前,需要先创建“类别”对应的实体类型,而这个实体类型不能直接创建在csmall-product-webapi
模块中,而是应该创建在另一个新的模块中,以便于其它各模块都可以使用到相同的实体类型(例如商品的实体类型,在商品管理模块和订单管理模块中都将需要使用)。
在Project中创建新的子模块csmall-pojo
,创建出来后,需要:
- 修改父项目
- 在
csmall-pojo
的pom.xml
中修改 - 在根项目的
pom.xml
中添加新的<module>
- 在
- 确定所需的依赖项
- 需要依赖
lombok
,其它依赖项暂时不需要
- 需要依赖
- 删除不要的代码
- 在
pom.xml
中只需要:父项目、当前项目的信息、依赖项 - 启动类是不需要的,必须删除(因为没有依赖Spring Boot,不删除则报错)
src/main/resource
是不需要的(包含其下的application.properties
),可以删除src/test
是不需要的,必须删除(否则默认的测试类会报错)
- 在
然后,在cn.tedu.csmall.pojo.entity
包下创建Category
类:
package cn.tedu.csmall.pojo.entity;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class Category implements Serializable {
private Long id;
private String name;
private Long parentId;
private Integer depth;
private String keywords;
private Integer sort;
private String icon;
private Integer enable;
private Integer isParent;
private Integer isDisplay;
private LocalDateTime gmtCreate;
private LocalDateTime gmtModified;
}
为了实现其它Module可以依赖csmall-pojo
,应该先在根级Project的pom.xml
中对csmall-pojo
实现依赖管理:
<!-- ===== 其它原有代码 ===== -->
<!-- 属性配置,主要配置各依赖项的版本号对应的“变量” -->
<properties>
<!-- ===== 其它原有代码 ===== -->
<csmall.version>0.0.1-SNAPSHOT</csmall.version> <!-- 新增 -->
</properties>
<!-- 依赖管理,主要管理各依赖项的版本,使得子级Module添加依赖时不必指定版本 -->
<dependencyManagement>
<dependencies>
<!-- Csmall POJO -->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>csmall-pojo</artifactId>
<version>${csmall.version}</version>
</dependency>
<!-- ===== 其它原有代码 ===== -->
然后,在csmall-product-webapi
的pom.xml
中添加依赖:
<!-- ===== 其它原有代码 ===== -->
<!-- 当前项目需要使用的依赖项 -->
<dependencies>
<!-- Csmall POJO -->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>csmall-pojo</artifactId>
</dependency>
<!-- ===== 其它原有代码 ===== -->
至此,在csmall-product-webapi
中就已经成功的依赖了csmall-pojo
,可以理解为csmall-pojo
已经成为csmall-product-webapi
的一部分。
在cn.tedu.csmall.product.webapi.mapper
包下创建CategoryMapper
接口,并在接口上添加@Repository
注解(主要是避免IntelliJ IDEA在自动装配时的误判错误),并在接口中添加抽象方法:
/**
* 处理“类别”数据的持久层接口
*/
@Repository
public interface CategoryMapper {
/**
* 插入“类别”数据
* @param category 类别
* @return 受影响的行数
*/
int insert(Category category);
// 查询--待定
}
3.4. 配置SQL语句
src/main/resources/mapper
下
关于CategoryMapper.xml
文件的配置:
<?xml version="1.0" encoding="UTF-8"