springboot+mybatis多数据源+分页PageHelper

springboot+mybatis多数据源+分页PageHelper

步骤1

整个项目写下来踩过的坑无数,反反复复的需求也可以说有很多
在springboot+mybatis配置多数据源的问题上,总会出现读取不到mapper的xml文件,通过强大的csdn查找方法。总算是解决这个问题。
首先entity实体类mapper的配置文件及接口要进行拆分。放在不同的路径下,这一步尤为重要。如下图所示:
在这里插入图片描述

步骤2

下面就是数据源的配置,因为是springboot项目,我直接配置到yml文件,由于项目问题,我会打上马赛克,如下图
在这里插入图片描述

步骤3

下面再就是配置数据源,因为要使用多个数据源,所以需要配置config类
配置主要的数据源连接可以使用@Primary注解
MainDataSourceConfig.java是我主要使用的配置类。
SecondDataSourceConfig.java只需要访问数据库,并往数据库写入数据

package com.xx.xx.config;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Author: 
 * @date: 
 * @desc: 
 */
@Configuration
@MapperScan(basePackages = "com.xx.xx.mapper.wechat",sqlSessionFactoryRef = "mainSqlSessionFactory")
public class MainDataSourceConfig {

    @Bean(name = "mainData_Source")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource1")
    public DataSource mainDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "mainSqlSessionFactory")
    @Primary
    public SqlSessionFactory mainSqlSessionFactory(@Qualifier("mainData_Source") DataSource dataSource)
            throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        //设置分页参数
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("dialect", "postgresql");
        pageHelper.setProperties(properties);
        bean.setPlugins(new Interceptor[]{pageHelper});

        //设置读取的xml文件位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources
                ("classpath*:com/xx/xx/mapper/wechat/*.xml"));
        //设置实体类配置
        bean.setTypeAliasesPackage("com.xx.xx.entity.wechat");
        //设置加载mybatis配置文件
        bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        return bean.getObject();
    }

    @Bean(name = "mainTransactionManager")
    @Primary
    public DataSourceTransactionManager mainTransactionManager(@Qualifier("mainData_Source") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean("mainSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate mainSqlSessionTemplate(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

package com.xx.xx.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Author:
 * @date: 
 * @desc: 这个配置只用于数据存储功能
 */
@Configuration
@MapperScan(basePackages = "com.xx.xx.mapper.second",sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {

    @Bean(name = "second_dataSource")
    @ConfigurationProperties(prefix = "spring.datasource2")
    public DataSource secondDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("second_dataSource")DataSource dataSource) throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/xx/xx/mapper/second/*.xml"));
        bean.setTypeAliasesPackage("com.xx.xx.entity.second");
        return bean.getObject();
    }

    @Bean(name = "secondTransactionManager")
    public DataSourceTransactionManager secondTransactionManager(@Qualifier("second_dataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "secondSqlSessionTemplate")
    public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

步骤4

关于分页,如果细心的同学可能已经发现了,要实现分页该怎么做,但是我这里还是进行解释一下,帮助一些小白同学。
如果需要达到分页效果,及其他配置,可以在SqlSessionFactory方法中进行配置,按照自己所需要的来进行配置,如下图示:
在这里插入图片描述

总结

需要注意的是:所有的mapper.xml文件里面如果type、parameterType等的属性。如果是写的简写类名,这里就必须改为全类路径;比如user类,就必须写为com.xx.xx.entity.wechat.User

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值