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