SpringBoot:mybatisplus与多环境开发
mybatisplus对service层也进行了封装‘
- service层接口继承service顶级接口
IService<T>
,需要指定操作的泛型,即,操作User,就写User - service层的实现类继承service顶级接口
IService<T>
的实现类ServiceImpl<UserMapper, User>
public interface UserService extends IService<User> {}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{}
- MybatisPlus实现分页功能
<table cellpadding="1px" cellspacing="1px">
<tr>
<td>id</td>
<td>userName</td>
<td>password</td>
<td>name</td>
<td>age</td>
<td>email</td>
</tr>
<!--通过page的getRecords方法可以取得查询结果。-->
<tr th:each="user,stat:${page.records}">
<td th:text="${user.id}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.email}"></td>
</tr>
</table>
@RequestMapping("/select")
public String getUsers(Model model, @RequestParam(value = "pn",defaultValue = "1")Integer pn){
//每页展示2条数据,这是第pn页
List<User> list = userService.list();
Page<User> userPage = new Page(pn,2);
Page page = userService.page(userPage, null);
List records = page.getRecords();
// //获取当前页
// long current = page.getCurrent();
// //获取总记录数
// long total = page.getTotal();
// //总页数
// long pages = page.getPages();
model.addAttribute("page",page);
return "success";
}
}
- 进行分页
<p>当前第[[${page.current}]]页,总共[[${page.pages}]]页,共[[${page.total}]]条记录</p>
<!--重新调用这个方法并将pn的值-1传入-->
<a th:href="@{/select/(pn=${page.current-1})}">上一页</a>
<!--循环遍历数字,从1到最大页。每一个都会执行该方法并传入当前pn-->
<a th:href="@{/select(pn=${num})}" th:each="num:${#numbers.sequence(1,page.pages)}" th:text="${num}"></a>
<!--重新调用这个方法并将pn的值+1传入-->
<a th:href="@{/select/(pn=${page.current+1})}">下一页</a>
- 除此之外,还需要将MybatisPlus的分页组件创建。
@Configuration
public class MybatisConf {
@Bean
public MybatisPlusInterceptor paginationInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
整合Junit5
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
- 使用Junit5的测试方法,可以直接使用spring的IOC容器,而且可以使用
@Transactional
注解可以使得测试方法结束后进行回滚。
-
@Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试
-
@ParameterizedTest :表示方法是参数化测试,下方会有详细介绍
-
@RepeatedTest :表示方法可重复执行,下方会有详细介绍
-
@DisplayName :为测试类或者测试方法设置展示名称
-
@BeforeEach :表示在每个单元测试之前执行
-
@AfterEach :表示在每个单元测试之后执行
-
@BeforeAll :表示在所有单元测试之前执行
-
@AfterAll :表示在所有单元测试之后执行
-
@Tag :表示单元测试类别,类似于JUnit4中的@Categories
-
@Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore
-
@Timeout :表示测试方法运行如果超过了指定时间将会返回错误
-
@ExtendWith :为测试类或测试方法提供扩展类引用
断言机制
- 简单断言
@Test
@DisplayName("简单断言")
void test1(){
//结果和预期是否相等,不相等则输出“断言失败”
assertEquals(8,add(4,5),"断言失败");
}
int add(int i,int j){
return i+j;
}
/*
org.opentest4j.AssertionFailedError: 断言失败 ==>
Expected(预期) :8
Actual(实际) :9
*/
用来对单个值进行简单的验证。如:
方法 | 说明 |
---|---|
assertEquals | 判断两个对象或两个原始类型是否相等 |
assertNotEquals | 判断两个对象或两个原始类型是否不相等 |
assertSame | 判断两个对象引用是否指向同一个对象 |
assertNotSame | 判断两个对象引用是否指向不同的对象 |
assertTrue | 判断给定的布尔值是否为 true |
assertFalse | 判断给定的布尔值是否为 false |
assertNull | 判断给定的对象引用是否为 null |
assertNotNull | 判断给定的对象引用是否不为 null |
多环境开发
针对不同环境,一些属性可能有所不同。springboot允许多环境
- 使用不同的配置文件指定不同的环境,如:
application-prod.yml
、application-test.yml
- 然后通过配置文件告诉springboot使用哪个环境
spring.profiles.active=prod
#prod
person:
name: prod-张三
#test
person:
name: test-张三
@RestController
public class TestController {
@Value("${person.name:李四}")
private String name;
@RequestMapping("/hello")
public String hello(){
return "hello,"+name;
}
}
//输出结果为 hello,prod张三
- 同时也可以使用
@Profile("prod")
来指定某个方法或是某个类只在prod环境下才起作用。