MYBATIS 拦截器实现执行的SQL语句向另一个数据库写入

本文介绍了如何使用MYBATIS拦截器(SyncInterceptor.java)来捕获执行的SQL语句,并将这些操作同步到一个不同的数据库中,以实现数据的双写策略。
摘要由CSDN通过智能技术生成

主数据库的配置

package com.lantu.platform.admin.baseConfig;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.aspectj.lang.annotation.Aspect;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
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.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import com.lantu.platform.admin.common.interceptor.SyncInterceptor;
import com.lantu.platform.admin.common.interceptor.paginate.PaginationStatementHandlerInterceptor;

import tk.mybatis.spring.annotation.MapperScan;

@Aspect
@Configuration
@PropertySource(value = {
   "classpath:database.properties"})
@MapperScan(basePackages = {
   "com.lantu.platform.admin.mapper.masterMapper"},
sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DBMasterConfig {
   
	
	private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.lantu.platform.admin.*.service.impl..*.*(..))";
	
	@Bean(name="masterDataSource")
	@Primary
	@ConfigurationProperties(prefix = "spring.datasource.master")
	public DataSource setDataSource() {
   
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name = "syncInterceptor")
	public SyncInterceptor getSyncInterceptor() {
   
		return new SyncInterceptor(); 
	}
	
	@Bean(name = "masterSqlSessionFactory")
	@Primary
	public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource")DataSource datasource,
			MybatisProperties mybatisProperties,@Qualifier("syncInterceptor")SyncInterceptor syncInterceptor)
		throws Exception{
   
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值