springboot中mybatis使用PageHelper和tk.mybatis

公司要做前后端分离,后端决定采用springboot提供接口程序,持久层采用mybatis,为了方便,需要对mapper进一步封装,继续整合PageHelper和tk.mybatis。

pom添加依赖


        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            RELEASE
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            RELEASE
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            RELEASE
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.0
        
        
            com.alibaba
            druid
            ${druid.version}
        
        
            mysql
            mysql-connector-java
            ${mysql-connector-java.version}
        
        

application.properties配置

# 数据源基础配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#数据库
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis 配置
mybatis.type-aliases-package=com.mos.quote.model
mybatis.mapper-locations=classpath:mapper/*.xml
# 通用 Mapper 配置
mapper.mappers=com.mos.quote.common.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
# 分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

PS:此处的坑,pagehelper.reasonable,启用合理化时候,如果pageNo<1,则会返回第一页数据,如果pageNo>pages会查询最后一页,作为接口程序,如果传入的pageNo一直大于pages,则一直会有数据返回,前端还需要校验页码问题。
建议:如果普通的分页查询,建议开启该功能,如果作为前后端分离或者提供接口之类的,建议禁用该功能

MyMapper

package com.mos.quote.common;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * 该接口不能被扫描到,否则会出错
 * @author Administrator
 */
public interface MyMapper extends Mapper, MySqlMapper {
}

MyMapper的使用

package com.mos.quote.mapper;

import com.mos.quote.common.MyMapper;
import com.mos.quote.model.Area;

/**
 * @author Administrator
 */
public interface AreaMapper extends MyMapper {
}

Service中使用

package com.mos.quote.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mos.quote.mapper.AreaMapper;
import com.mos.quote.model.Area;
import com.mos.quote.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 */
@Service
public class AreaServiceImpl implements IAreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List queryAllByPID(String parentId) {
        Area area = new Area();
        area.setParentId(parentId);
        return areaMapper.select(area);
    }

    @Override
    public PageInfo queryPage(Integer pageNo, Integer pageSize, String parentId) {
        PageHelper.startPage(pageNo,pageSize);
        List list = this.queryAllByPID(parentId);
        return new PageInfo<>(list);
    }
}

启动添加mapper扫描

package com.mos.quote;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author Administrator
 */
@SpringBootApplication
@MapperScan(basePackages = "com.mos.quote.mapper")
public class QuoteApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(QuoteApplication.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        System.out.println("starter");
    }
}

测试controller

package com.mos.quote.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.mos.quote.model.Area;
import com.mos.quote.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("demo")
public class DemoController {

    @Autowired
    private IAreaService areaService;

    @RequestMapping("/test")
    public String test(Integer pageNo,Integer pageSize, Model model){
        List areas = areaService.queryAllByPID("0");
        model.addAttribute("listSize",areas.size());
        System.out.println("areas---->"+JSON.toJSONString(areas));
        PageInfo page = areaService.queryPage(pageNo,pageSize,"0");
        System.out.println(JSON.toJSONString(page));
        model.addAttribute("page---->", JSON.toJSONString(page));
        return "test";
    }
}

输出结果(格式化Json后)

浏览器输入http://localhost:8080/demo/test?pageNo=1&pageSize=10查看控制台

控制台数据
springboot中mybatis使用PageHelper和tk.mybatis_第1张图片
父id为0的全部数据
springboot中mybatis使用PageHelper和tk.mybatis_第2张图片
父id为0的前10条数据

其实和spring mvc中使用差不多,注意jar包引用即可,大部分springboot都有自己对应的jar,使用springmvc的会报错。

你可能感兴趣的:(springboot中mybatis使用PageHelper和tk.mybatis)