DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id bigint ( 20 ) NOT NULL COMMENT '主键ID' ,
name VARCHAR ( 30 ) NULL DEFAULT NULL COMMENT '姓名' ,
PRIMARY KEY ( id)
) ;
DELETE
FROM user;
INSERT INTO user ( id, name)
VALUES ( 1 , 'Jone' ) ,
( 2 , 'Jack' ) ,
( 3 , 'Tom' ) ,
( 4 , 'Sandy' ) ,
( 5 , 'Billie' ) ;
package com. zs. testmybatisplus ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import com. baomidou. mybatisplus. annotation. DbType ;
import com. baomidou. mybatisplus. extension. plugins. MybatisPlusInterceptor ;
import com. baomidou. mybatisplus. extension. plugins. inner. PaginationInnerInterceptor ;
import com. github. pagehelper. PageInterceptor ;
@Configuration
public class MybatisPlusPageConfig {
@Bean
public PageInterceptor pageInterceptor ( ) {
return new PageInterceptor ( ) ;
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor ( ) {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor ( ) ;
interceptor. addInnerInterceptor ( new PaginationInnerInterceptor ( DbType . H2) ) ;
return interceptor;
}
}
1实体类
package com. zs. testmybatisplus ;
import lombok. Data ;
@Data
public class User {
private Long id;
private String name;
}
2测试类
package com. zs. testmybatisplus ;
import static org. assertj. core. api. Assertions . assertThat;
import java. util. Arrays ;
import java. util. List ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
import com. baomidou. mybatisplus. core. toolkit. Wrappers ;
import com. baomidou. mybatisplus. extension. plugins. pagination. Page ;
import com. github. pagehelper. PageHelper ;
import com. github. pagehelper. PageInfo ;
@SpringBootTest
class DefaultTest {
@Autowired
private UserMapper mapper;
@Test
void test ( ) {
Page < User > mpPage = mapper. selectPage ( new Page < > ( 1 , 2 ) , Wrappers . < User > query ( )
. eq ( "id" , 1 ) ) ;
assertThat ( mpPage. getTotal ( ) ) . isEqualTo ( 1L ) ;
List < User > records = mpPage. getRecords ( ) ;
assertThat ( records) . isNotEmpty ( ) ;
assertThat ( records. size ( ) ) . isEqualTo ( 1 ) ;
PageInfo < User > info = PageHelper . startPage ( 1 , 2 )
. doSelectPageInfo (
( ) -> mapper. selectById ( 1 ) ) ;
assertThat ( info. getTotal ( ) ) . isEqualTo ( 1L ) ;
List < User > list = info. getList ( ) ;
assertThat ( list) . isNotEmpty ( ) ;
assertThat ( list. size ( ) ) . isEqualTo ( 1 ) ;
}
@Test
void testIn ( ) {
List < Long > ids = Arrays . asList ( 1L , 2L ) ;
Page < User > mpPage = mapper. selectPage ( new Page < > ( 1 , 5 ) , Wrappers . < User > query ( )
. in ( "id" , ids) ) ;
assertThat ( mpPage. getTotal ( ) ) . isEqualTo ( 2L ) ;
List < User > records = mpPage. getRecords ( ) ;
assertThat ( records) . isNotEmpty ( ) ;
assertThat ( records. size ( ) ) . isEqualTo ( 2 ) ;
PageInfo < User > info = PageHelper . startPage ( 1 , 5 )
. doSelectPageInfo (
( ) -> mapper. selectList ( Wrappers . < User > query ( )
. in ( "id" , ids) ) ) ;
assertThat ( info. getTotal ( ) ) . isEqualTo ( 2L ) ;
List < User > list = info. getList ( ) ;
assertThat ( list) . isNotEmpty ( ) ;
assertThat ( list. size ( ) ) . isEqualTo ( 2 ) ;
}
}
3mapper
package com. zs. testmybatisplus ;
import org. apache. ibatis. annotations. Mapper ;
import com. baomidou. mybatisplus. core. mapper. BaseMapper ;
@Mapper
public interface UserMapper extends BaseMapper < User > {
}