SpringBoot整合Mybatis
1:pom文件
<!--mybatis分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.6</version> </dependency> <!--mybatis与SpringBoot整合的起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2:yaml文件配置
server: port: 8081 spring: application: name: provider datasource: type: com.zaxxer.hikari.HikariDataSource #使用hikariDataSource数据库连接池(默认) driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://localhost:3306/cloudorder?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 mybatis: mapper-locations: [classpath:/mapper/*.xml] # 映射classpath:内路径里面mapper包下面的所有xml文件 configuration: map-underscore-to-camel-case: true # 下划线转换为驼峰命名 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql日志 type-aliases-package: com.tom.dome.entity #省略实体类参数的省略包名 #pagehelper分页插件的配置 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql logging: level: com.tom.demo.mapper: trace pattern: dateformat: MM-dd HH:mm:ss:SS #日志打印时时间输入格式
3:主启动类上面,添加dao层的包扫描
@SpringBootApplication @MapperScan(basePackages = {"com.tom.dome.mapper"})-->扫描这个mapper包下的接口加入容器 public class SpringBootTest03Application { public static void main(String[] args) { SpringApplication.run(SpringBootTest03Application.class, args); } }
4:分页插件pageHelper的使用
1:方式一
@RequestMapping("/stu/{page}/{pageSize}")
@ResponseBody
public List<Student> selectStudent(@PathVariable("page")int page,//当前页码
@PathVariable("pageSize")int pageSize//每页显示的条数){
PageHelper.startPage(page,pageSize);
List<Student> studentList = userMapper.listStudent();
//现在去查询查出来就是分页之后的数据
//注意:只有紧跟着PageHelper.startPage(pageNum,pageSize)的sql语句才被pagehelper起作用,再次注意:经过再次验证,这句话不是扯淡
PageInfo<Student> pageInfo = new PageInfo<>(studentList);
List<Student> list = pageInfo.getList();//当前页的数据
int pageNum = pageInfo.getPageNum();//当前第几页
System.out.println("当前第几页"+pageNum);
int size1 = pageInfo.getSize();//每页显示条数
System.out.println("size1每页显示条数"+size1);
long total = pageInfo.getTotal();//总条数
System.out.println("total一共条数:"+total);
int prePage = pageInfo.getPrePage();//上一页是第几页
System.out.println("上一页是第几页:"+prePage);
int nextPage = pageInfo.getNextPage();//下一页是第几页
System.out.println("下一页是第几页:"+nextPage);
boolean isFirstPage = pageInfo.isIsFirstPage();//是否是第一页
System.out.println("是否是第一页"+isFirstPage);
boolean isLastPage = pageInfo.isIsLastPage();//是否是最后一页
System.out.println("是否是最后一页"+isFirstPage);
return list;
}
2:方式二
/** * 检查项的分页查询 * @param queryPageBean * @return */ @Override public Result findPage(QueryPageBean queryPageBean) { Integer page = queryPageBean.getPage();//当前页码 Integer pageSize = queryPageBean.getPageSize();//每页显示的条数 String queryString = queryPageBean.getQueryString();//查询条件 //完成分页查询,使用mybatis的分页插件完成 //select * from t_checkitem limit 0,10 PageHelper.startPage(page,pageSize); //返回一个分页对象 Page<CheckItem> checkItemPage checkItemMapper.selectByCondition(queryString); //根据返回的分页对象,获取总条数total,和每页的数据 long total = checkItemPage.getTotal(); List<CheckItem> rows = checkItemPage.getResult(); //将分页对象返回给前端,这个对象中有前端需要的所有数据 return Result。success(checkItemPage); } /** * Dao层的写法进行分页查询 * @param queryString * @return */ Page<CheckItem> selectByCondition(@Param("queryString") String queryString);
浙公网安备 33010602011771号