MyBatis-Plus分页插件
创建项目导入依赖(springboot项目为例)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
配置application.yml
spring:
datasource:
username: root #用户名
password: root #密码
url: #数据库连接url
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package: com.xht.entity #实体类别名
mapper-locations: classpath:mapper/*.xml #mapper.xml文件位置
使用MyBatis-Plus代码生成器生成各个模块的代码
使用这个极大的简化了开发,使用方便快捷
不需要多余设置,快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码
(可以直接复制使用,只需更改包的名字就行)
public class AutoCode {
public static void main(String[] args) {
// 代码自动生成对象
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String property = System.getProperty("user.dir");
gc.setOutputDir(property+"/src/main/java");
gc.setAuthor("Xuhaitao");//自动生成作者
gc.setOpen(false);//是否打开资源管理器
gc.setFileOverride(false);//是否覆盖原来生成的代码
gc.setServiceName("%sService");//去掉I前缀
// gc.setIdType(IdType.AUTO);//id类型
gc.setDateType(DateType.ONLY_DATE);
mpg.setGlobalConfig(gc);
//配置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("");//连接数据库url
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//3、包的配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName ("blog"); 项目的名称
pc.setParent("com.xht");
pc.setEntity("entity");
pc.setMapper( "mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("user");//要映射的表名
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setLogicDeleteFieldName("deleted");//逻辑删除
//自动填充配置
TableFill createTime = new TableFill( "create_time", FieldFill. INSERT);
// TableFill updateTime = new TableFill( "update_time",FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(createTime);
// tableFills.add(updateTime);
strategy.setTableFillList(tableFills);
//乐观锁
// strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true); // Localhost:8080/hello_ id_ .2
mpg.setStrategy(strategy);
mpg.execute();//执行
}
}
运行结果:(在application.yml中我将mapper.xml的位置配置在了resources下的mapper中所以和这里的xml需要自己手动换到resources下的mapper中)
使用MyBatis-Plus的插件功能都需要进行一个配置
编写配置类:
@Configuration //支持配置
@MapperScan("com.xht.mapper") //扫描mapper
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
开始进行分页查询
在UserController中编写:使用userService.pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
里面需要两个参数一个page
,一个Wrapper
。Wrapper
是可选参数可以先用null填充
Wrapper
是用来进行条件查询
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/userPage")
public String userPage(){
Page<User> userPage = new Page<>(1,2);
userService.pageMaps(userPage,null);
System.out.println(".hasNext()是否存在下一页======>"+userPage.hasNext());
System.out.println(".hasPrevious()是否存在上一页======>"+userPage.hasPrevious());
System.out.println(".getTotal()总数======>"+userPage.getTotal());
System.out.println(".getSize()一页显示多少数据======>"+userPage.getSize());
System.out.println(".getRecords()返回查询的结果集======>"+userPage.getRecords());
return null;
}
}
控制台打印结果:
.hasNext()是否存在下一页======>true
.hasPrevious()是否存在上一页======>false
.getTotal()总数======>43
.getSize()一页显示多少数据======>2
.getRecords()返回查询的结果集======>[{password=SYSTEM, salt=SYSTEM, create_time=2019-04-13 18:11:03.0, id=1, type=0, email=nowcoder1@sina.com, header_url=http://static.nowcoder.com/images/head/notify.png, username=SYSTEM, status=1}, {password=25ac0a2e8bd0f28928de3c56149283d6, salt=49f10, create_time=2019-04-18 01:11:27.0, id=11, type=1, email=nowcoder11@sina.com, header_url=http://images.nowcoder.com/head/11t.png, username=nowcoder11, status=1}]
MyBatis-Plus的分页中已经帮我们封装好了我们分页常用的是否有上一页、下一页、总共多少条数据等方法
希望对大家有所帮助,如果发现错误欢迎大家指正