MyBatis-plus运用

MyBatis-plus继承了mybatis原有的特点,在不改变原有mybatis的特点、功能上,集成了分页、代码生成器……,很好的与spring无缝集成配置,不再需要spring-mybatis插件包。

配置:

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
        <!-- 自动扫描mapping.xml文件-->
        <property name="mapperLocations" value="classpath*:mapper/*.xml" /> 
        <!-- 自动为实体取别名(类名) -->
        <property name="typeAliasesPackage" value="com.*.entity" />
        <!-- 全局配置 -->
        <property name="globalConfig">
            <bean class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
                <property name="dbColumnUnderline" value="true" />
                 <property name="sqlInjector">
                     <bean class="com.baomidou.mybatisplus.mapper.AutoSqlInjector" />
                 </property>
            </bean>
        </property> 
    </bean>

 

分页插件配置:mybatis-config.xml:
 
<plugins>
        <!-- mybatisplus分页查询插件 -->
        <plugin interceptor="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
            <property name="dialectType" value="mysql" />
        </plugin>
</plugins>

 

mapper数据访问层  extends BaseMapper<Entity>

 继承BaseMapper<Enteity>实现默认的常用方法。

service 逻辑层  extends ServiceImpl<BaseMapper<Entity>, Entity>

逻辑方法:

public Page<Entity> queryPage(Page<Entity> page) {
      //自定义mapper方法查询
        //page.setRecords(icMapper.queryPage(page));
       //调用baseMapper分页查询方法   page对象,参数
        page.setRecords(baseMapper.selectPage(page, null));
        return page;
}

总结:

page.setRecords(baseMapper.selectPage(page, null));
执行了sql语句的拼接封装,完成了分页功能

 

posted @ 2017-05-14 18:40  不负未来不负卿  阅读(493)  评论(0)    收藏  举报