赞
踩
在spring boot项目中使用PageHelper,只需要
1、Springboot项目引入mysql和mybatis的依赖:
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.0.0</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.47</version>
- <scope>runtime</scope>
- </dependency>
2、在application.yml配置数据库连接信息,Mybatis信息,是否打印SQL等(具体看注释):
- # Springboot2.1.2默认引入的mysql版本version是8.0.13,其driver-class-name为com.mysql.cj.jdbc.Driver
- spring:
- datasource:
- url: jdbc:mysql://118.25.190.197:3306/order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
- username: root
- password: ******
- driver-class-name: com.mysql.jdbc.Driver
-
- # type-aliases-package批量设置别名作用:就是在mapper.xml文件中直接写类名,不配置就需要写类的全路径名
- mybatis:
- type-aliases-package: com.example.demo.entity
- mapper-locations: classpath:mapping/*.xml
-
- # 将mapper接口所在包的日志级别改成debug,可以在控制台打印sql
- logging:
- level:
- com.example.demo.mapper: debug
3、编写Mapper接口(普通接口,无需任何注解参与)统一放在com.example.demo.mapper包下,编写mapper.xml文件(根据上面的配置,统一放在resources/mapping下)
4、在启动类上加注解,扫描Mapper接口所在包路径:
@MapperScan("com.example.demo.mapper")
注:
也可以不在主类使用@MapperScan,而在每个Mapper接口上面添加@Mapper注解,效果一样!
Mapper接口方法有多个参数需要加@Param(“参数名”)注解,否则抛异常说找不到参数。
以上集成完成mybatis,如需集成druid数据库连接池:
5、添加druid依赖:
- <!-- alibaba的druid数据库连接池 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.10</version>
- </dependency>
6、修改application.yml,添加数据库连接池的配置,完成以后内容如下:
- # mysql version 8.0.13的driver-class-name:com.mysql.cj.jdbc.Driver
- spring:
- datasource:
- url: jdbc:mysql://118.25.190.197:3306/order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
- username: root
- password: 123456
- driver-class-name: com.mysql.jdbc.Driver
- # druid
- type: com.alibaba.druid.pool.DruidDataSource
- filters: stat
- maxActive: 20
- initialSize: 1
- maxWait: 60000
- minIdle: 1
- timeBetweenEvictionRunsMillis: 60000
- minEvictableIdleTimeMillis: 300000
- validationQuery: select 'x'
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- poolPreparedStatements: true
- maxOpenPreparedStatements: 20
-
- # type-aliases-package批量设置别名作用:就是在mapper.xml文件中直接写类名,不配置就需要写类的全路径名
- mybatis:
- type-aliases-package: com.example.demo.entity
- mapper-locations: classpath:mapping/*.xml
-
- # 将mapper接口所在包的日志级别改成debug,可以在控制台打印sql
- logging:
- level:
- com.example.demo.mapper: debug
如需集成PageHelper分页:
7、添加依赖:
- <!-- 分页插件 -->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper-spring-boot-starter</artifactId>
- <version>1.2.10</version>
- </dependency>
8、简单使用,采用默认配置即可,调用controller接口传入pageNum(即当前页码,从1开始)和pageSize(即每页条数),添加PageHepler(在开始查询之前)和PageInfo两条语句,原来的查询不需要任何修改:
- @GetMapping("/selectByPage/{pageNum}/{pageSize}")
- public PageInfo<GdUsers> selectByPage(@PathVariable int pageNum, @PathVariable int pageSize) {
- PageHelper.startPage(pageNum, pageSize);
- List<GdUsers> list = gdUsersMapper.selectByPage(); //得到从pageNum开始的pageSize条数据
- //如果直接返回list,得到了分页的数据,如果添加下面步骤,返回pageInfo,则能得到包括list在内的分页信息
- PageInfo<GdUsers> pageInfo = new PageInfo<>(list);
- return pageInfo;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。