美文网首页
Springboot使用PageHelper和TkMybatis

Springboot使用PageHelper和TkMybatis

作者: ArgusK | 来源:发表于2018-12-03 20:32 被阅读0次
  • tk.mybatis框架在mybatis的基础上又做了一层封装,开发时简单的CRUD操作无需再写mapper.xml
  • pagehelper实现了数据库查询物理分页,原理是拦截器实现
1. pom文件增加以下依赖
    <dependency>
          <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
2. 自定义BaseMapper
/**
 * 定义自己的Mapper
 *注意这个Mapper不需要被扫描
 */
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {

}
3. 业务Mapper
@Mapper
public interface AccountMapper extends BaseMapper<Account> {
  //some methods
}
4. Service使用pagehelper和mapper
public PageInfo<RequirementVo> list(int pageNo, int pageSize){
        // 获取第1页,1-10条数据
        PageHelper.startPage(pageNo, pageSize);
        // 紧跟着的第一个select方法会被分页
        List<RequirementVo> list = requirementMapper.selectAll();
        PageInfo pageInfo = new PageInfo<>(list);
        //获取总记录数
        int total = pageInfo.getTotal();
        return pageInfo;
}
PageHelper原理分析
pagehelper-spring-boot-autoconfigure

pagehelper自动配置加载PageHelperAutoConfiguration
查看源码发现根据配置属性创建了拦截器PageInterceptor,在查询前对语句进行了预处理

package com.github.pagehelper.autoconfigure;

import com.github.pagehelper.PageInterceptor;
import com.github.pagehelper.autoconfigure.PageHelperProperties;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnBean({SqlSessionFactory.class})
@EnableConfigurationProperties({PageHelperProperties.class})
@AutoConfigureAfter({MybatisAutoConfiguration.class})
public class PageHelperAutoConfiguration {
    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;
    @Autowired
    private PageHelperProperties properties;

    public PageHelperAutoConfiguration() {
    }

    @Bean
    @ConfigurationProperties(
        prefix = "pagehelper"
    )
    public Properties pageHelperProperties() {
        return new Properties();
    }

    @PostConstruct
    public void addPageInterceptor() {
        PageInterceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.putAll(this.pageHelperProperties());
        properties.putAll(this.properties.getProperties());
        interceptor.setProperties(properties);
        Iterator var3 = this.sqlSessionFactoryList.iterator();

        while(var3.hasNext()) {
            SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)var3.next();
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }

    }
}

相关文章

网友评论

      本文标题:Springboot使用PageHelper和TkMybatis

      本文链接:https://www.haomeiwen.com/subject/iasucqtx.html