zoukankan      html  css  js  c++  java
  • Spring配置MyBatisPlus分页插件

    今天在使用Spring整合MyBaitsPlus时

    在需要使用分页插件碰到了一些小问题,百度大部分是SpringBoot配置MyBatisPlus的所以自己记录一下Spring的配置过程

    我这里使用的MyBatisPlus版本如下

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus</artifactId>
        <version>3.4.1</version>
    </dependency>

    Spring 的配置文件中完整的配置MyBatisPlus以及分页插件的配置信息如下

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <!-- 指定数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定自定义MyBatis的配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 指定Mapper XML文件的目录 -->
        <property name="mapperLocations" value="classpath*:priv/fruits/mapperxml/*.xml"/>
        <!-- 配置 MyBatisPlus 分页插件 -->
        <property name="plugins">
            <array>
                <ref bean="mybatisPlusInterceptor"/>
            </array>
        </property>
    </bean>
    
    <!-- 配置MapperScanner -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定DAO层映射目录 -->
        <property name="basePackage" value="priv.fruits.mapper"/>
    </bean>
    
    <!-- 配置MyBatisPlus分页插件 -->
    <bean id="mybatisPlusInterceptor" class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
        <property name="interceptors">
            <list>
                <ref bean="paginationInnerInterceptor"/>
            </list>
        </property>
    </bean>
    
    <bean id="paginationInnerInterceptor"
          class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor">
        <!-- 指定数据库为MySQL -->
        <constructor-arg name="dbType" value="MYSQL"/>
    </bean>

    这时配置就基本完成,我们就可以使用MyBatisPlus的分页插件进行查询

    注意MyBatisPlus自己已经帮我们实现了分页查询方法,如果你的Service层继承了ServiceImpl类,就可以直接使用

    想要使用MyBatisPlus封装的CRUD,Mapper就需要继承BaseMapper,Service需要继承ServiceImpl

    测试代码如下:

    Mapper类:

    package priv.fruits.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import priv.fruits.domain.TAdmin;
    
    public interface TAdminMapper extends BaseMapper<TAdmin> {
    }

    Service类:

    package priv.fruits.service;
    
    import org.springframework.stereotype.Service;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import priv.fruits.domain.TAdmin;
    import priv.fruits.mapper.TAdminMapper;
    @Service
    public class TAdminService extends ServiceImpl<TAdminMapper, TAdmin> { }

    测试代码:

    package priv.fruits.test;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import priv.fruits.domain.TAdmin;
    import priv.fruits.service.TAdminService;
    
    import java.util.List;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:applicationContext.xml")
    public class TestProject {
    
        @Autowired
        private TAdminService adminService;
    
        @Test
        public void testAddress() {
            // 创建MyBatisPlus的Page类,该类用于封装分页信息
            // 这里使用了构造器来创建Page,传入两个参数  第一个表示当前页,第二个表示每页展示的数量
            IPage<TAdmin> page = new Page<>(1, 1);
            // 调用Service层中MyBatisPlus已经实现好的查询方法(page方法就是分页查询的方法)
            // 会返回一个Page对象
            IPage<TAdmin> page1 = adminService.page(page);
            // Page对象中会有一个records字段,该字段存放的就是查询的数据
            List<TAdmin> records = page1.getRecords();
            // 输出打印结果
            System.out.println(records.toString());
        }
    
    }

    我另一篇博客有解释MyBatisPlus的Page对象中的所有参数介绍:https://www.cnblogs.com/wanguncle/p/14127788.html

    打印结果如下:

  • 相关阅读:
    【POJ 2987】Firing (最小割-最大权闭合子图)
    -网络流经典模型
    【bzoj 3299】 [USACO2011 Open]Corn Maze玉米迷宫(最短路)
    【POJ 3623】 Best Cow Line, Gold (后缀数组)
    题表-各种生成树
    yield 生成器的运行机制
    numpy.random.shuffle(x)的用法
    Softmax回归(Softmax Regression
    softmax 函数
    极大似然估计的朴素理解
  • 原文地址:https://www.cnblogs.com/wanguncle/p/14127779.html
Copyright © 2011-2022 走看看