文章目录
10. 基于Spring JDBC的事务管理(续)
当需要方法是事务性的,可以使用@Transactional
注解,此注解可以添加在:
- 接口
- 会使得此接口的实现类的所有实现方法都是事务性的
- 接口中的抽象方法上
- 会使得此接口的实现类中,重写的此方法是事务性的
- 只作用于当前方法
- 如果接口上也配置了此注解,并且接口和抽象方法的注解均配置了参数,以方法上的配置为准
- 业务实现类
- 会使得当前类中所有重写的方法都是事务性
- 自定义的方法不会是事务性的
- 会使得当前类中所有重写的方法都是事务性
- 业务实现类中的方法
- 不可以添加在自定义的(不是重写的接口的)方法上
- 语法上,可以添加,但执行时,不允许
- 不可以添加在自定义的(不是重写的接口的)方法上
Spring JDBC是通过接口代理的方式进行事务管理,所以,只对接口中声明的方法有效!
通常,应该将@Transactional
添加在接口中的抽象方法上(如果偷懒,或为了避免遗漏,也可以直接添加在接口上)。
目前,由于csmall-product-service
没有添加相关依赖,所以,并不能直接在接口中使用@Transactional
注解(因为尚不可识别),则应该在此Module中添加依赖:
<!-- Mybatis Spring Boot:Mybatis及对Spring Boot的支持 -->
<!-- 仅需要保留spring-jdbc,使得业务接口可以使用@Transactional注解 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
11. 类别管理–添加类别–业务逻辑层(续)
目前,在业务实现中,视为“错误”时始终抛出ServiceException
,且没有任何异常信息,是不合理的!
在略大规模的项目中,“错误”的种类可能较大,如果为每一种“错误”都创建一个对应的异常,则需要创建的异常类型就比较多,但是,这些异常类除了名称不一样以外,几乎没有不同,所以,存在不利于管理和维护的问题。
其实,也可以只使用1个异常类型(或者少量异常类型),但是,每次抛出时,也需要明确的表示“是哪一种错误”,则可以在异常类型中添加“业务状态码”。
则首先需要业务状态码的类型,可以从前缀项目中复制State
文件,例如:
package cn.tedu.csmall.common.web;
public enum State {
OK(20000),
ERR_CATEGORY_NAME_DUPLICATE(40100), // 客户端引起的--类别--名称冲突(被占用)
ERR_CATEGORY_NOT_FOUND(40101), // 客户端引起的--类别--数据不存在(查询参数值不正确)
ERR_INSERT(50000), // 服务端引起的--插入数据错误
ERR_UPDATE(50001); // 服务端引起的--更新数据错误
private Integer value;
State(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
}
然后,在ServiceException中,自定义构造方法,强制要求传入State state
和String message
参数,并且,为State
类型参数提供公有的获取值的方法:
public class ServiceException extends RuntimeException {
private State state;
public ServiceException(State state, String message) {
super(message);
if (state == null) {
throw new IllegalArgumentException("使用ServiceException必须指定错误时的业务状态码!");
}
this.state = state;
}
public State getState() {
return state;
}
}
在后续抛出异常时,应该传入State state
和String message
:
throw new ServiceException(State.ERR_CATEGORY_NOT_FOUND, "添加类别失败,父级类别不存在!");
完成后,应该再次执行测试,以保证修改代码后,原有代码依然能正确运行。
12. 类别管理–添加类别–控制器层
12.1. 处理跨域(一次性配置)
在csmall-product-webapi
的根包下config
包下创建SpringMvcConfiguration
类,实现WebMvcConfigururer
接口,重写其中的方法,以解决跨域问题:
package cn.tedu.csmall.product.webapi.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SpringMvcConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
提示:以上代码从此前的案例中复制过来即可。
12.2. 控制器类与处理请求的方法
先将此前项目中的JsonResult
复制到csmall-common
的根包下的web
子包中(需要在csmall-common
中补充依赖lombok
)。
在csmall-product-webapi
的根包下创建controller.CategoryController
类,在类上添加@RestController
和@RequestMapping(value = "/categories", produces = "application/json; charset=utf-8")
这2个注解:
@RestController
@RequestMapping(value = "/categories", produces = "application/json; charset=utf-8")
public class CategoryController {
}
然后,在类中添加处理请求的方法:
@Autowired
private ICategoryService categoryService;
@PostMapping("/add-new")
public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO) {
categoryService.addNew(categoryAddNewDTO);
return JsonResult.ok();
}
12.3. 控制器层测试
在csmall-product-webapi
的测试的根包下创建controller.CategoryControllerTests
测试类,编写并执行测试:
@SpringBootTest
@AutoConfigureMockMvc
public class CategoryControllerTests {
@Autowired
MockMvc mockMvc;
@Test
@Sql("classpath:truncate.sql")
public void testAddNewSuccessfully() throws Exception {
// 准备测试数据,不需要封装,应该全部声明为String类型
String name = "水果";
String parentId = "0"; // 即使目标类型是Long,参数值也不要加L
String keywords = "水果的关键字是啥";
String sort = "66";
String icon = "图标待定";
String isDisplay = "1";
// 请求路径,不需要写协议、服务器主机和端口号
String url = "/categories/add-new";
// 执行测试
// 以下代码相对比较固定
mockMvc.perform( // 执行发出请求
MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
.contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
.param("name", name) // 请求参数,有多个时,多次调用param()方法
.param("parentId", parentId)
.param("keywords", keywords)
.param("icon", icon)
.param("sort", sort)
.param("isDisplay", isDisplay)
.accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
.andExpect( // 预判结果,类似断言
MockMvcResultMatchers
.jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
.value(20000)) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
.andDo( // 需要执行某任务
MockMvcResultHandlers.print()); // 打印日志
}
}
12.4. 处理异常
在csmall-common
中添加依赖项:
<!-- Spring Boot Web:支持Spring MVC -->
<!-- 需要使用到@RestControllerAdvice等注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
在csmall-common
根包下的ex
包下创建handler.GlobalExceptionHandler
,并在此类中处理异常:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ServiceException.class)
public JsonResult<Void> handleServiceException(ServiceException ex) {
return JsonResult.fail(ex.getState(), ex.getMessage());
}
}
完成后,使用错误的测试数据时,会发现根本不会处理异常,是因为在csmall-product-webapi
中默认执行的组件扫描不会扫描到以上GlobalExceptionHandler
所在的包,为了解决此问题,应该先在csmall-common
的根包下创建config.CsmallCommonConfiguration
类,此类应该是配置类,且配置组件扫描:
package cn.tedu.csmall.common.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("cn.tedu.csmall.common.ex.handler")
public class CsmallCommonConfiguration {
}
然后,在csmall-product-webapi
的启动类引用此配置类:
package cn.tedu.csmall.product.webapi;
@SpringBootApplication
@Import({CsmallCommonConfiguration.class}) // 新增
public class CsmallProductWebapiApplication {
public static void main(String[] args) {
SpringApplication.run(CsmallProductWebapiApplication.class, args);
}
}
接下来,即可执行测试:
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testAddNewFailBecauseNameDuplicate() throws Exception {
// 准备测试数据,不需要封装,应该全部声明为String类型
String name = "类别001";
String parentId = "0"; // 即使目标类型是Long,参数值也不要加L
String keywords = "水果的关键字是啥";
String sort = "66";
String icon = "图标待定";
String isDisplay = "1";
// 请求路径,不需要写协议、服务器主机和端口号
String url = "/categories/add-new";
// 执行测试
// 以下代码相对比较固定
mockMvc.perform( // 执行发出请求
MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
.contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
.param("name", name) // 请求参数,有多个时,多次调用param()方法
.param("parentId", parentId)
.param("keywords", keywords)
.param("icon", icon)
.param("sort", sort)
.param("isDisplay", isDisplay)
.accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
.andExpect( // 预判结果,类似断言
MockMvcResultMatchers
.jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
.value(State.ERR_CATEGORY_NAME_DUPLICATE.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
.andDo( // 需要执行某任务
MockMvcResultHandlers.print()); // 打印日志
}
@Test
@Sql({"classpath:truncate.sql"})
public void testAddNewFailBecauseParentNotFound() throws Exception {
// 准备测试数据,不需要封装,应该全部声明为String类型
String name = "类别001";
String parentId = "-1"; // 即使目标类型是Long,参数值也不要加L
String keywords = "水果的关键字是啥";
String sort = "66";
String icon = "图标待定";
String isDisplay = "1";
// 请求路径,不需要写协议、服务器主机和端口号
String url = "/categories/add-new";
// 执行测试
// 以下代码相对比较固定
mockMvc.perform( // 执行发出请求
MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
.contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
.param("name", name) // 请求参数,有多个时,多次调用param()方法
.param("parentId", parentId)
.param("keywords", keywords)
.param("icon", icon)
.param("sort", sort)
.param("isDisplay", isDisplay)
.accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
.andExpect( // 预判结果,类似断言
MockMvcResultMatchers
.jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
.value(State.ERR_CATEGORY_NOT_FOUND.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
.andDo( // 需要执行某任务
MockMvcResultHandlers.print()); // 打印日志
}
12.5. 验证请求参数格式的基本有效性
关于Validation框架的基本使用:
- 添加依赖
- 在控制器类中处理请求的方法的被验证的参数(封装的对象)之前添加
@Validated
/@Valid
- 在参数的类型(封装的类型)的属性之前添加验证注解
- 在统一处理异常的类中对
BindException
进行处理
先在csmall-product-webapi
中添加依赖(如果已经添加,则不需要重复添加):
<!-- Spring Boot Validation:验证请求参数的基本格式 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
在CategoryController
处理请求的方法的参数之前添加@Validated
/ @Valid
注解:
@PostMapping("/add-new")
// ===== 在以下方法的参数前添加@Validated / @Valid注解 =====
public JsonResult<Void> addNew(@Validated CategoryAddNewDTO categoryAddNewDTO) {
categoryService.addNew(categoryAddNewDTO);
return JsonResult.ok();
}
由于CategoryAddNewDTO
等类在csmall-pojo
模块中的,要在此类中添加@NotNull
等注解,则必须在csmall-pojo
中添加依赖:
<!-- Spring Boot Validation:验证请求参数的基本格式 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
</exclusions>
</dependency>
然后,CategoryAddNewDTO
的name
属性上添加@NotNull
约束(其它的约束等到测试通过之后再补充):
@Data
public class CategoryAddNewDTO implements Serializable {
@NotNull(message = "添加类别失败,必须填写类别名称!") // 新增
private String name;
// ===== 其它原有代码 =====
}
在State
中添加对应“请求参数格式错误”的枚举值:
public enum State {
OK(20000),
// ===== 下行为新增 ======
ERR_BAD_REQUEST(40000), // 客户端引起的--请求参数格式错误
// ===== 其它原有代码 =====
}
在GlobalExceptionHandler
中添加新的处理异常的方法:
@ExceptionHandler(BindException.class)
public JsonResult<Void> handleBindException(BindException ex) {
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
StringBuilder stringBuilder = new StringBuilder();
for (FieldError fieldError : fieldErrors) {
stringBuilder.append(";");
stringBuilder.append(fieldError.getDefaultMessage());
}
String message = stringBuilder.substring(1);
return JsonResult.fail(State.ERR_BAD_REQUEST, message);
}
最后,添加测试:
@Test
@Sql({"classpath:truncate.sql"})
public void testAddNewFailBecauseBadRequest() throws Exception {
// 准备测试数据,注意:此次没有提交必要的name属性值
String parentId = "0"; // 即使目标类型是Long,参数值也不要加L
String keywords = "水果的关键字是啥";
String sort = "66";
String icon = "图标待定";
String isDisplay = "1";
// 请求路径,不需要写协议、服务器主机和端口号
String url = "/categories/add-new";
// 执行测试
// 以下代码相对比较固定
mockMvc.perform( // 执行发出请求
MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
.contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
// .param("name", name) // 注意:此处不提交必要的name属性
.param("parentId", parentId)
.param("keywords", keywords)
.param("icon", icon)
.param("sort", sort)
.param("isDisplay", isDisplay)
.accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
.andExpect( // 预判结果,类似断言
MockMvcResultMatchers
.jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
.value(State.ERR_BAD_REQUEST.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
.andDo( // 需要执行某任务
MockMvcResultHandlers.print()); // 打印日志
}
测试成功后,应该在CategoryAddNewDTO
的各属性中补充更多的、必要的注解进行约束,并且,添加更多约束后,还应该编写更多的测试。
13. 类别管理–根据父级类别查询其所有子级类别–持久层
13.1. 规划SQL语句
本次需要执行的SQL语句大致是:
select * from pms_category where parent_id=? and enable=1 and is_display=1 order by sort desc, gmt_modified desc;
关于字段列表,应该包括:
id, name, sort, icon, is_parent
13.2. 抽象方法(可能需要创建VO类)
在csmall-pojo
的根包下的vo
包下创建CategorySimpleListItemVO
类,封装以上设计的5个字段对应的属性:
package cn.tedu.csmall.pojo.vo;
import lombok.Data;
import java.io.Serializable;
@Data
public class CategorySimpleListItemVO implements Serializable {
private Long id;
private String name;
private Integer sort;
private String icon;
private Integer isParent;
}
在CategoryMapper
接口中添加:
List<CategorySimpleListItemVO> listByParentId(Long parentId);
13.3. 在XML中配置SQL
在CategoryMapper.xml
中添加配置:
<!-- List<CategorySimpleListItemVO> listByParentId(Long parentId); -->
<select id="listByParentId" resultMap="SimpleListResultMap">
select
<include refid="SimpleListQueryFields" />
from
pms_category
where
parent_id=#{id} and enable=1 and is_display=1
order by
sort desc, gmt_modified desc
</select>
<sql id="SimpleListQueryFields">
<if test="true">
id, name, sort, icon, is_parent
</if>
</sql>
<resultMap id="SimpleListResultMap" type="cn.tedu.csmall.pojo.vo.CategorySimpleListItemVO">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="sort" property="sort" />
<result column="icon" property="icon" />
<result column="is_parent" property="isParent" />
</resultMap>
13.4. 测试
本次测试推荐使用人工检查查询结果。
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testListByParentId() {
// 测试数据
Long parentId = 1L;
// 执行测试,获取查询结果
List<CategorySimpleListItemVO> list = mapper.listByParentId(parentId);
// 查看结果
System.out.println("查询结果数量:" + list.size());
for (CategorySimpleListItemVO item : list) {
System.out.println(item);
}
}
14. 类别管理–根据父级类别查询其所有子级类别–业务逻辑层
14.1. 接口和抽象方法
在ICategoryService
中添加:
List<CategorySimpleListItemVO> listByParentId(Long parentId);
14.2. 实现
在CategoryServiceImpl
中直接调用categoryMapper
执行查询并返回即可。
@Override
public List<CategorySimpleListItemVO> listByParentId(Long parentId) {
return categoryMapper.listByParentId(parentId);
}
14.3. 测试
与持久层测试类似。
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testListByParentId() {
// 测试数据
Long parentId = 1L;
// 执行测试,获取查询结果
List<CategorySimpleListItemVO> list = service.listByParentId(parentId);
// 查看结果
System.out.println("查询结果数量:" + list.size());
for (CategorySimpleListItemVO item : list) {
System.out.println(item);
}
}
15. 类别管理–根据父级类别查询其所有子级类别–控制器层
在CategoryController
中添加:
@GetMapping("/list-by-parent")
public JsonResult<List<CategorySimpleListItemVO>> listByParentId(Long parentId) {
// 调用service并将结果封装到JsonResult中
List<CategorySimpleListItemVO> list = categoryService.listByParentId(parentId);
return JsonResult.ok(list);
}
在CategoryControllerTests
中测试:
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testListByParentId() throws Exception {
// 准备测试数据,注意:此次没有提交必要的name属性值
String parentId = "0";
// 请求路径,不需要写协议、服务器主机和端口号
String url = "/categories/list-by-parent";
// 执行测试
// 以下代码相对比较固定
mockMvc.perform( // 执行发出请求
MockMvcRequestBuilders.get(url) // 根据请求方式决定调用的方法
.contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
.param("parentId", parentId)
.accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
.andExpect( // 预判结果,类似断言
MockMvcResultMatchers
.jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
.value(State.OK.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
.andDo( // 需要执行某任务
MockMvcResultHandlers.print()); // 打印日志
}