spring boot整合mybatis+通用mapper+pagehelper分页插件

spring boot整合mybatis+通用mapper+pagehelper分页插件

pom依赖



    4.0.0

    com.wendao
    springboot-03-mybatis
    0.0.1-SNAPSHOT
    jar

    springboot-03-mybatis
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
        
            com.github.pagehelper
            pagehelper
            4.2.1
        
        
        
            tk.mybatis
            mapper
            3.3.9
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.6
        
        
        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.scala-lang
            scala-library
            2.11.0
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

配置

在属性配置文件中配置数据库连接池信息

#jdbc配置
spring.datasource.druid.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
#配置别名
mybatis.type-aliases-package=com.wendao.demo.pojo
#连接池配置
spring.datasource.druid.initial-size=20
spring.datasource.druid.max-active=400
spring.datasource.druid.min-idle=11
spring.datasource.druid.max-wait=1

自己管理SqlSessionFactory

package com.wendao.demo.conf;
import com.github.pagehelper.PageHelper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
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.boot.autoconfigure.condition.ConditionalOnBean;
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 javax.sql.DataSource;
import java.util.Properties;
/**
 * 自己实例化sqlSessionFactory
 * 该sqlSessionFactory
 * 1:使用了druid数据库连接词
 * 2:添加了pagehelper分页插件
 */
@Configuration
public class MyBatisConfig {
    @Autowired
    private DataSource dataSource;
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean()  {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.wendao.demo.pojo");
        //分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);
        //添加插件
        bean.setPlugins(new Interceptor[]{pageHelper});
        try {
        //bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        return bean.getObject();
        } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
        }
    }
    @Bean
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}

手动管理包扫描器

package com.wendao.demo.conf;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
@Configuration
//必须在MyBatisConfig注册后再加载MapperScannerConfigurer,否则会报错
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.wendao");
        return mapperScannerConfigurer;
    }
}

你可能感兴趣的:(spring,boot)