SpringBoot + Mybatis + Druid + PageHelper 实现多数据源并分页(附源码)

spring.datasource.initialSize=5

spring.datasource.minIdle=5

spring.datasource.maxActive=20

# 配置获取连接等待超时的时间

spring.datasource.maxWait=60000

# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

spring.datasource.timeBetweenEvictionRunsMillis=60000

# 配置一个连接在池中最小生存的时间,单位是毫秒

spring.datasource.minEvictableIdleTimeMillis=300000

spring.datasource.validationQuery=SELECT 1 FROM DUAL

spring.datasource.testWhileIdle=true

spring.datasource.testOnBorrow=false

spring.datasource.testOnReturn=false

# 打开PSCache,并且指定每个连接上PSCache的大小

spring.datasource.poolPreparedStatements=true

spring.datasource.maxPoolPreparedStatementPerConnectionSize=20

# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall’用于防火墙

spring.datasource.filters=stat,wall,log4j

# 通过connectProperties属性来打开mergeSql功能;慢SQL记录

spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

成功添加了配置文件之后,我们再来编写Druid相关的类。

首先是MasterDataSourceConfig.java这个类,这个是默认的数据源配置类。

@Configuration

@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = “masterSqlSessionFactory”)

public class MasterDataSourceConfig {

static final String PACKAGE = “com.pancm.dao.master”;

static final String MAPPER_LOCATION = “classpath:mapper/master/*.xml”;

@Value(“${master.datasource.url}”)

private String url;

@Value(“${master.datasource.username}”)

private String username;

@Value(“${master.datasource.password}”)

private String password;

@Value(“${master.datasource.driverClassName}”)

private String driverClassName;

@Value(“${spring.datasource.initialSize}”)

private int initialSize;

@Value(“${spring.datasource.minIdle}”)

private int minIdle;

@Value(“${spring.datasource.maxActive}”)

private int maxActive;

@Value(“${spring.datasource.maxWait}”)

private int maxWait;

@Value(“${spring.datasource.timeBetweenEvictionRunsMillis}”)

private int timeBetweenEvictionRunsMillis;

@Value(“${spring.datasource.minEvictableIdleTimeMillis}”)

private int minEvictableIdleTimeMillis;

@Value(“${spring.datasource.validationQuery}”)

private String validationQuery;

@Value(“${spring.datasource.testWhileIdle}”)

private boolean testWhileIdle;

@Value(“${spring.datasource.testOnBorrow}”)

private boolean testOnBorrow;

@Value(“${spring.datasource.testOnReturn}”)

private boolean testOnReturn;

@Value(“${spring.datasource.poolPreparedStatements}”)

private boolean poolPreparedStatements;

@Value(“${spring.datasource.maxPoolPreparedStatementPerConnectionSize}”)

private int maxPoolPreparedStatementPerConnectionSize;

@Value(“${spring.datasource.filters}”)

private String filters;

@Value(“{spring.datasource.connectionProperties}”)

private String connectionProperties;

@Bean(name = “masterDataSource”)

@Primary

public DataSource masterDataSource() {

DruidDataSource dataSource = new DruidDataSource();

dataSource.setUrl(url);

dataSource.setUsername(username);

dataSource.setPassword(password);

dataSource.setDriverClassName(driverClassName);

//具体配置

dataSource.setInitialSize(initialSize);

dataSource.setMinIdle(minIdle);

dataSource.setMaxActive(maxActive);

dataSource.setMaxWait(maxWait);

dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

dataSource.setValidationQuery(validationQuery);

dataSource.setTestWhileIdle(testWhileIdle);

dataSource.setTestOnBorrow(testOnBorrow);

dataSource.setTestOnReturn(testOnReturn);

dataSource.setPoolPreparedStatements(poolPreparedStatements);

dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

try {

dataSource.setFilters(filters);

} catch (SQLException e) {

e.printStackTrace();

}

dataSource.setConnectionProperties(connectionProperties);

return dataSource;

}

@Bean(name = “masterTransactionManager”)

@Primary

public DataSourceTransactionManager masterTransactionManager() {

return new DataSourceTransactionManager(masterDataSource());

}

@Bean(name = “masterSqlSessionFactory”)

@Primary

public SqlSessionFactory masterSqlSessionFactory(@Qualifier(“masterDataSource”) DataSource masterDataSource)

throws Exception {

final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

sessionFactory.setDataSource(masterDataSource);

sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()

.getResources(MasterDataSourceConfig.MAPPER_LOCATION));

return sessionFactory.getObject();

}

}

其中这两个注解说明下:

  • @Primary :标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。多数据源配置的时候注意,必须要有一个主数据源,用 @Primary 标志该 Bean。

  • @MapperScan:扫描 Mapper 接口并容器管理。

需要注意的是sqlSessionFactoryRef 表示定义一个唯一 SqlSessionFactory 实例。

上面的配置完之后,就可以将Druid作为连接池使用了。但是Druid并不简简单单的是个连接池,它也可以说是一个监控应用,它自带了web监控界面,可以很清晰的看到SQL相关信息。

在SpringBoot中运用Druid的监控作用,只需要编写StatViewServlet和WebStatFilter类,实现注册服务和过滤规则。这里我们可以将这两个写在一起,使用@Configuration和@Bean。

为了方便理解,相关的配置说明也写在代码中了,这里就不再过多赘述了。

代码如下:

@Configuration

public class DruidConfiguration {

@Bean

public ServletRegistrationBean druidStatViewServle() {

//注册服务

ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(

new StatViewServlet(), “/druid/*”);

// 白名单(为空表示,所有的都可以访问,多个IP的时候用逗号隔开)

servletRegistrationBean.addInitParameter(“allow”, “127.0.0.1”);

// IP黑名单 (存在共同时,deny优先于allow)

servletRegistrationBean.addInitParameter(“deny”, “127.0.0.2”);

// 设置登录的用户名和密码

servletRegistrationBean.addInitParameter(“loginUsername”, “pancm”);

servletRegistrationBean.addInitParameter(“loginPassword”, “123456”);

// 是否能够重置数据.

servletRegistrationBean.addInitParameter(“resetEnable”, “false”);

return servletRegistrationBean;

}

@Bean

public FilterRegistrationBean druidStatFilter() {

FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(

new WebStatFilter());

// 添加过滤规则

filterRegistrationBean.addUrlPatterns(“/*”);

// 添加不需要忽略的格式信息

filterRegistrationBean.addInitParameter(“exclusions”,

.js,.gif,.jpg,.png,.css,.ico,/druid/*”);

System.out.println(“druid初始化成功!”);

return filterRegistrationBean;

}

}

编写完之后,启动程序,在浏览器输入:http://127.0.0.1:8084/druid/index.html ,然后输入设置的用户名和密码,便可以访问Web界面了。

多数据源配置

======

在进行多数据源配置之前,先分别在springBoot和springBoot_test的mysql数据库中执行如下脚本。

– springBoot库的脚本

CREATE TABLE t_user (

id int(11) NOT NULL AUTO_INCREMENT COMMENT ‘自增id’,

name varchar(10) DEFAULT NULL COMMENT ‘姓名’,

age int(2) DEFAULT NULL COMMENT ‘年龄’,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8

– springBoot_test库的脚本

CREATE TABLE t_student (

id int(11) NOT NULL AUTO_INCREMENT,

name varchar(16) DEFAULT NULL,

age int(11) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

注:为了偷懒,将两张表的结构弄成一样了!不过不影响测试!

在application.properties中已经配置这两个数据源的信息,上面已经贴出了一次配置,这里就不再贴了。

这里重点说下 第二个数据源的配置。和上面的MasterDataSourceConfig.java差不多,区别在与没有使用@Primary 注解和名称不同而已。需要注意的是MasterDataSourceConfig.java对package和mapper的扫描是精确到目录的,这里的第二个数据源也是如此。

那么代码如下:

@Configuration

@MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = “clusterSqlSessionFactory”)

public class ClusterDataSourceConfig {

static final String PACKAGE = “com.pancm.dao.cluster”;

static final String MAPPER_LOCATION = “classpath:mapper/cluster/*.xml”;

@Value(“${cluster.datasource.url}”)

private String url;

@Value(“${cluster.datasource.username}”)

private String username;

@Value(“${cluster.datasource.password}”)

private String password;

@Value(“${cluster.datasource.driverClassName}”)

private String driverClass;

// 和MasterDataSourceConfig一样,这里略

@Bean(name = “clusterDataSource”)

public DataSource clusterDataSource() {

DruidDataSource dataSource = new DruidDataSource();

dataSource.setUrl(url);

dataSource.setUsername(username);

dataSource.setPassword(password);

dataSource.setDriverClassName(driverClass);

// 和MasterDataSourceConfig一样,这里略 …

return dataSource;

}

@Bean(name = “clusterTransactionManager”)

public DataSourceTransactionManager clusterTransactionManager() {

return new DataSourceTransactionManager(clusterDataSource());

}

@Bean(name = “clusterSqlSessionFactory”)

public SqlSessionFactory clusterSqlSessionFactory(@Qualifier(“clusterDataSource”) DataSource clusterDataSource)

throws Exception {

final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

sessionFactory.setDataSource(clusterDataSource);

sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ClusterDataSourceConfig.MAPPER_LOCATION));

return sessionFactory.getObject();

}

}

成功写完配置之后,启动程序,进行测试。

分别在spring Boot和springBoot_test库中使用接口进行添加数据。

t_user

POST http://localhost:8084/api/user

{“name”:“张三”,“age”:25}

{“name”:“李四”,“age”:25}

{“name”:“王五”,“age”:25}

t_student

POST http://localhost:8084/api/student

{“name”:“学生A”,“age”:16}

{“name”:“学生B”,“age”:17}

{“name”:“学生C”,“age”:18}

成功添加数据之后,然后进行调用不同的接口进行查询。

请求:

GET http://localhost:8084/api/user?name=李四

返回:

{

“id”: 2,

“name”: “李四”,

“age”: 25

}

请求:

GET http://localhost:8084/api/student?name=学生C

返回:

{

“id”: 1,

“name”: “学生C”,

“age”: 16

}

通过数据可以看出,成功配置了多数据源了。

PageHelper 分页实现

===============

PageHelper是Mybatis的一个分页插件,非常的好用!这里强烈推荐!!!

PageHelper的使用很简单,只需要在Maven中添加pagehelper这个依赖就可以了。

Maven的依赖如下:

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.3

注:这里我是用springBoot版的!也可以使用其它版本的。

添加依赖之后,只需要添加如下配置或代码就可以了。

第一种,在application.properties或application.yml添加

pagehelper:

helperDialect: mysql

offsetAsPageNum: true

rowBoundsWithCount: true

reasonable: false

第二种,在mybatis.xml配置中添加

helperDialect=mysql

offsetAsPageNum=true

rowBoundsWithCount=true

reasonable=false

第三种,在代码中添加,使用@Bean注解在启动程序的时候初始化。

@Bean

public PageHelper pageHelper(){

PageHelper pageHelper = new PageHelper();

Properties properties = new Properties();

//数据库

properties.setProperty(“helperDialect”, “mysql”);

//是否将参数offset作为PageNum使用

properties.setProperty(“offsetAsPageNum”, “true”);

//是否进行count查询

properties.setProperty(“rowBoundsWithCount”, “true”);

//是否分页合理化

properties.setProperty(“reasonable”, “false”);

pageHelper.setProperties(properties);

}

因为这里我们使用的是多数据源,所以这里的配置稍微有些不同。我们需要在sessionFactory这里配置。这里就对MasterDataSourceConfig.java进行相应的修改。

在masterSqlSessionFactory方法中,添加如下代码。

@Bean(name = “masterSqlSessionFactory”)

@Primary

public SqlSessionFactory masterSqlSessionFactory(@Qualifier(“masterDataSource”) DataSource masterDataSource)

throws Exception {

final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

sessionFactory.setDataSource(masterDataSource);

sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()

.getResources(MasterDataSourceConfig.MAPPER_LOCATION));

//分页插件

Interceptor interceptor = new PageInterceptor();

Properties properties = new Properties();

//数据库

properties.setProperty(“helperDialect”, “mysql”);

//是否将参数offset作为PageNum使用

properties.setProperty(“offsetAsPageNum”, “true”);

//是否进行count查询

properties.setProperty(“rowBoundsWithCount”, “true”);

//是否分页合理化

properties.setProperty(“reasonable”, “false”);

interceptor.setProperties(properties);

sessionFactory.setPlugins(new Interceptor[] {interceptor});

return sessionFactory.getObject();

}

注:其它的数据源也想进行分页的时候,参照上面的代码即可。

这里需要注意的是reasonable参数,表示分页合理化,默认值为false。如果该参数设置为 true 时,pageNum<=0 时会查询第一页,pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。

设置完PageHelper 之后,使用的话,只需要在查询的sql前面添加PageHelper.startPage(pageNum,pageSize);,如果是想知道总数的话,在查询的sql语句后买呢添加 page.getTotal()就可以了。

代码示例:

public List findByListEntity(T entity) {

List list = null;

try {

Page<?> page =PageHelper.startPage(1,2);

System.out.println(getClassName(entity)+“设置第一页两条数据!”);

list = getMapper().findByListEntity(entity);

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
的sql前面添加PageHelper.startPage(pageNum,pageSize);,如果是想知道总数的话,在查询的sql语句后买呢添加 page.getTotal()就可以了。

代码示例:

public List findByListEntity(T entity) {

List list = null;

try {

Page<?> page =PageHelper.startPage(1,2);

System.out.println(getClassName(entity)+“设置第一页两条数据!”);

list = getMapper().findByListEntity(entity);

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-1PaBEdfl-1715066831327)]

[外链图片转存中…(img-kOyIIwI1-1715066831328)]

[外链图片转存中…(img-1Iv4fben-1715066831328)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目中使用MyBatis Plus和Druid多数据源的步骤如下: 1. 添加依赖 在`pom.xml`文件中添加以下依赖: ```xml <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 2. 配置Druid数据源 在`application.yml`中添加Druid数据源的配置: ```yaml spring: datasource: # 主数据源 druid: url: jdbc:mysql://localhost:3306/main_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # Druid配置 initialSize: 5 minIdle: 5 maxActive: 20 testOnBorrow: false testOnReturn: false testWhileIdle: true timeBetweenEvictionRunsMillis: 60000 validationQuery: SELECT 1 FROM DUAL # 从数据源 druid2: url: jdbc:mysql://localhost:3306/sub_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # Druid配置 initialSize: 5 minIdle: 5 maxActive: 20 testOnBorrow: false testOnReturn: false testWhileIdle: true timeBetweenEvictionRunsMillis: 60000 validationQuery: SELECT 1 FROM DUAL ``` 3. 配置MyBatis Plus 在`application.yml`中添加MyBatis Plus的配置: ```yaml mybatis-plus: # 主数据源配置 mapper-locations: classpath:mapper/main/*.xml type-aliases-package: com.example.main.entity global-config: db-config: id-type: auto field-strategy: not_empty logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 从数据源配置 multi-datasource: main: mapper-locations: classpath:mapper/main/*.xml type-aliases-package: com.example.main.entity sub: mapper-locations: classpath:mapper/sub/*.xml type-aliases-package: com.example.sub.entity ``` 4. 配置数据源路由 在`com.example.config`包下创建`DynamicDataSourceConfig`类,用于配置数据源路由: ```java @Configuration public class DynamicDataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.druid") public DataSource mainDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.druid2") public DataSource subDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean public DataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); Map<Object, Object> dataSourceMap = new HashMap<>(2); dataSourceMap.put("main", mainDataSource()); dataSourceMap.put("sub", subDataSource()); // 将主数据源作为默认数据源 dynamicDataSource.setDefaultTargetDataSource(mainDataSource()); dynamicDataSource.setTargetDataSources(dataSourceMap); return dynamicDataSource; } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dynamicDataSource()); sqlSessionFactoryBean.setTypeAliasesPackage("com.example.main.entity"); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/main/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate() throws Exception { return new SqlSessionTemplate(sqlSessionFactory()); } } ``` 5. 配置数据源切换 在`com.example.config`包下创建`DynamicDataSource`类,用于实现数据源切换: ```java public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDataSource(); } } ``` 在`com.example.config`包下创建`DataSourceContextHolder`类,用于存储当前数据源: ```java public class DataSourceContextHolder { private static final ThreadLocal<String> DATASOURCE_CONTEXT_HOLDER = new ThreadLocal<>(); public static void setDataSource(String dataSource) { DATASOURCE_CONTEXT_HOLDER.set(dataSource); } public static String getDataSource() { return DATASOURCE_CONTEXT_HOLDER.get(); } public static void clearDataSource() { DATASOURCE_CONTEXT_HOLDER.remove(); } } ``` 在`com.example.aop`包下创建`DataSourceAspect`类,用于切换数据源: ```java @Aspect @Component public class DataSourceAspect { @Pointcut("@annotation(com.example.annotation.DataSource)") public void dataSourcePointCut() { } @Before("dataSourcePointCut()") public void before(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); DataSource dataSource = signature.getMethod().getAnnotation(DataSource.class); if (dataSource != null) { String value = dataSource.value(); DataSourceContextHolder.setDataSource(value); } } @After("dataSourcePointCut()") public void after(JoinPoint joinPoint) { DataSourceContextHolder.clearDataSource(); } } ``` 6. 使用多数据源 在需要使用从数据源的方法上加上`@DataSource("sub")`注解,如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> listUsers() { DataSourceContextHolder.setDataSource("sub"); List<User> users = userMapper.selectList(null); DataSourceContextHolder.clearDataSource(); return users; } } ``` 这样就完成了Spring Boot项目中使用MyBatis Plus和Druid多数据源的配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值