springboot中mybatis使用PageHelper和tk.mybatis

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

pom添加依赖

<!--持久层配置开始-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>RELEASE</version>
        </dependency>
        <!--mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>RELEASE</version>
        </dependency>
        <!--pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
        <!--持久层配置结束-->

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<T> extends Mapper<T>, MySqlMapper<T> {
}

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<Area> {
}

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<Area> queryAllByPID(String parentId) {
        Area area = new Area();
        area.setParentId(parentId);
        return areaMapper.select(area);
    }

    @Override
    public PageInfo<Area> queryPage(Integer pageNo, Integer pageSize, String parentId) {
        PageHelper.startPage(pageNo,pageSize);
        List<Area> 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<Area> areas = areaService.queryAllByPID("0");
        model.addAttribute("listSize",areas.size());
        System.out.println("areas---->"+JSON.toJSONString(areas));
        PageInfo<Area> 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
控制台数据
springboot中mybatis使用PageHelper和tk.mybatis
父id为0的全部数据
springboot中mybatis使用PageHelper和tk.mybatis
父id为0的前10条数据

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

上一篇:Centos-Mysql配置my.cnf内容


下一篇:搭建自己的个性博客平台