欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot和mybatis集成分頁(yè)插件

 更新時(shí)間:2017年04月24日 17:21:14   作者:牛頭人  
這篇文章主要為大家詳細(xì)介紹了spring boot和mybatis集成分頁(yè)插件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

MyBatis提供了攔截器接口,我們可以實(shí)現(xiàn)自己的攔截器,將其作為一個(gè)plugin裝入到SqlSessionFactory中。
首先要說(shuō)的是,Spring在依賴注入bean的時(shí)候,會(huì)把所有實(shí)現(xiàn)MyBatis中Interceptor接口的所有類都注入到SqlSessionFactory中,作為plugin存在。既然如此,我們集成一個(gè)plugin便很簡(jiǎn)單了,只需要使用@Bean創(chuàng)建PageHelper對(duì)象即可。

1、添加pom依賴

<dependency>
 <groupId>com.github.pagehelper</groupId>
 <artifactId>pagehelper</artifactId>
 <version>4.1.0</version>
</dependency>

2、MyBatisConfiguration.java類配置

package com.example.mybatis;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.github.pagehelper.PageHelper;

@Configuration
//加上這個(gè)注解,使得支持事務(wù)
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {
 @Autowired
 private DataSource dataSource;
 
 @Override
 public PlatformTransactionManager annotationDrivenTransactionManager() {
  return new DataSourceTransactionManager(dataSource);
 }
 
 @Bean(name = "sqlSessionFactory")
 public SqlSessionFactory sqlSessionFactoryBean(PageHelper pageHelper) {
  SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  bean.setDataSource(dataSource);

   //自定義數(shù)據(jù)庫(kù)配置的時(shí)候,需要將pageHelper的bean注入到Plugins中,如果采用系統(tǒng)默認(rèn)的數(shù)據(jù)庫(kù)配置,則只需要定義pageHelper的bean,會(huì)自動(dòng)注入。  

   bean.setPlugins(new Interceptor[] { pageHelper });
  try {
   return bean.getObject();
  } catch (Exception e) {
   e.printStackTrace();
   throw new RuntimeException(e);
  }
 }

 @Bean
 public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
  return new SqlSessionTemplate(sqlSessionFactory);
 }
 
 @Bean
 public PageHelper pageHelper() {
  PageHelper pageHelper = new PageHelper();
  Properties p = new Properties();
  p.setProperty("offsetAsPageNum", "true");
  p.setProperty("rowBoundsWithCount", "true");
  p.setProperty("reasonable", "true");
  p.setProperty("dialect", "mysql");
  pageHelper.setProperties(p);
  return pageHelper;
 }
}

3、分頁(yè)查詢測(cè)試

 @RequestMapping("/likename")
 public List<Student> likeName(@RequestParam String name){
  PageHelper.startPage(1, 1);
  return stuMapper.likeName(name);
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論