Springboot框架整合1:Springboot+Mybatis+mysql分包方式配置多数据源
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Springboot框架整合1:Springboot+Mybatis+mysql分包方式配置多数据源,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5534字,纯文字阅读大概需要8分钟。
内容图文
多数据源配置主要是有两种配置方式,一种是分包方式,另一种是通过AOP实现,本篇主要讲通过分包方式来实现多数据源配置。
一、添加项目依赖
<!-- mybatis start -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
二、编写application配置文件信息
本人使用的是.properties配置文件,yml的自定转换格式,这里以两个库为例,有多少个库就写多少个数据源配置信息。
#执行mapper里的接口后会打印出执行的sql语句
logging.level.com.boot.framework.mapper = debug
spring.datasource.userdb.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.userdb.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.userdb.jdbcUrl = jdbc:mysql://localhost:3306/db_user?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&serverTimezone=GMT%2B8
spring.datasource.userdb.username = root
spring.datasource.userdb.password = 123456
spring.datasource.userdb.validationQuery=SELECT 1
spring.datasource.userdb.testWhileIdle=true
spring.datasource.userdb.timeBetweenEvictionRunsMillis=300000
spring.datasource.userdb.numTestsPerEvictionRun=50
spring.datasource.userdb.minEvictableIdleTimeMillis=3600000
spring.datasource.admindb.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.admindb.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.admindb.jdbcUrl = jdbc:mysql://localhost:3306/db_admin?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&serverTimezone=GMT%2B8
spring.datasource.admindb.username = root
spring.datasource.admindb.password = 123456
spring.datasource.admindb.validationQuery=SELECT 1
spring.datasource.admindb.testWhileIdle=true
spring.datasource.admindb.timeBetweenEvictionRunsMillis=300000
spring.datasource.admindb.numTestsPerEvictionRun=50
spring.datasource.admindb.minEvictableIdleTimeMillis=3600000
三、数据源配置文件
一个数据源一个配置文件,这里列举出一个完整的配置例子,不同的数据源只需要更改bean名称以及路径即可。
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@MapperScan(basePackages = UserDatasource.PACKAGE, sqlSessionTemplateRef = "userdb")
public class UserDatasource {
// 精确到 mapper 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.boot.framework.mapper.userdb";
static final String MAPPER_LOCATION = "classpath:mapping/userdb/*.xml";
@Bean(name = "userdbDatasource")
@ConfigurationProperties(prefix = "spring.datasource.userdb")
@Primary //标记为主数据源,当数据源不确定时会默认选择主数据源,必须并只能在一个文件中使用该注解
public DataSource textDatasource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "userSqlSessionFactory")
@Primary
public SqlSessionFactory textSessionFactory(@Qualifier("userdbDatasource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(UserDatasource.MAPPER_LOCATION));
//分页拦截器-start 添加分页插件,若无该需求可不配置
Interceptor[] plugins = new Interceptor[]{pageHelper()};
bean.setPlugins(plugins);
// 分页拦截器-end
return bean.getObject();
}
//事务管理
@Bean(name = "userTransactionManager")
public DataSourceTransactionManager smartKitTransactionManager(@Qualifier("userdbDatasource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "userdb")
@Primary
public SqlSessionTemplate textSessionTemplat(@Qualifier("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
//分页插件配置
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("returnPageInfo", "check");
p.setProperty("params", "count=countSql");
pageHelper.setProperties(p);
return pageHelper;
}
}
因为使用了分页插件,所以就简单的举例来说明一下如何使用吧
是的,只需要在调用mapper接口前使用
PageHelper.startPage(“第几页”,“一页多少条数据”);
这时分页插件会自动拦截到下一次执行的sql,并动态的加上limit关键字,如:
如此配置基本就完成了,接下来可以通过 mybatis 逆向工程将model、mapper、xml文件生成出来。
内容总结
以上是互联网集市为您收集整理的Springboot框架整合1:Springboot+Mybatis+mysql分包方式配置多数据源全部内容,希望文章能够帮你解决Springboot框架整合1:Springboot+Mybatis+mysql分包方式配置多数据源所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。