Spring MVC框架搭建扩展--多数据源配置(一)

4 篇文章 0 订阅
3 篇文章 0 订阅

本节主要介绍spring mvc+mybatis多数据源配置

1.首先配置2个数据源
在spring-mybatis.xml里

<!-- 配置数据源1 使用的是Druid数据源 -->
    <bean id="fdataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="${dataSource.url}" />
        <property name="username" value="${dataSource.username}" />
        <property name="password" value="${dataSource.password}" />

        <!-- 初始化连接大小 -->
        <property name="initialSize" value="0" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="20" />

        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="0" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000" />
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="33" />
        <!-- 用来检测有效sql -->
        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
        <!-- 监控数据库 -->
        <property name="filters" value="mergeStat" />
    </bean>
<!-- 数据源2 -->
    <bean id="sdataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <!-- 初始化连接大小 -->
        <property name="initialSize" value="0" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="20" />

        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="0" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000" />
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="33" />
        <!-- 用来检测有效sql -->
        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
        <!-- 监控数据库 -->
        <property name="filters" value="mergeStat" />
    </bean>

2.准备一个类(DynamicDataSource)继承AbstractRoutingDataSource实现determineCurrentLookupKey方法,该方法可以实现数据库的动态切换;一个设置和获取数据源的类(DataSourceContextHolder)

public class DynamicDataSource extends AbstractRoutingDataSource{

    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDataSource();
    }

}
public class DataSourceContextHolder {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

    /**
     *设置数据源
     */
    public static void setDataSource(String dataSource){
        contextHolder.set(dataSource);
    }

    /**
     *获取数据源
     */
    public static String getDataSource(){
        return contextHolder.get();
    }

    /**
     * 清除数据源名字
     */
    public static void clearDataSource(){
        contextHolder.remove();
    }
}

3.spring-mybatis.xml里配置多数据源

<!-- 多数据源配置 -->
    <bean id="dataSource" class="com.ghca.util.db.DynamicDataSource">
        <property name="defaultTargetDataSource" ref="fdataSource"></property>
        <property name="targetDataSources">
            <map key-type ="java.lang.String">
                <entry key="fdataSource" value-ref="fdataSource" />
                <entry key="sdataSource" value-ref="sdataSource" />
            </map>
        </property>
    </bean>
<!-- myBatis文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ghca.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

<!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

4.通过aop配置来实现自动数据源切换
本文先通过xml方式配置
(1)准备一个Annotation,用于记录数据源名:

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {

    String name() default DataSource.fdataSource;

    public static String fdataSource = "fdataSource";

    public static String sdataSource = "sdataSource";
}

(2)准备一个切面:

public class AspectDataSourceExchange{

    public void afterReturning(){
        DataSourceContextHolder.clearDataSource();
    }

    public void before(JoinPoint joinPoint){
        // 从切点上获取目标方法
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        if (method.isAnnotationPresent(DataSource.class)) {
            DataSource datasource = method.getAnnotation(DataSource.class);
            if (null == datasource || (null != datasource && null == datasource.name())) {
                DataSourceContextHolder.setDataSource(DataSource.fdataSource);
                System.out.println("当前数据源为:" + DataSourceContextHolder.getDataSource());
            }
            else {
                DataSourceContextHolder.setDataSource(datasource.name());
                System.out.println("当前数据源为:" + DataSourceContextHolder.getDataSource());
            }
        }
    }

}

(3)aop配置
spring-mybatis.xml里

<!-- 注册切换数据源bean (切面) -->
    <bean id="aspectDataSourceExchange" class="com.ghca.util.db.AspectDataSourceExchange" />

    <!-- Spring aop事务管理 -->
    <aop:config>
        <aop:aspect id="aspect" ref="aspectDataSourceExchange">
            <aop:pointcut expression="execution(* com.ghca.service.impl.LoginServiceImpl.*(..))"
                id="transactionPointcut" />
            <aop:before method="before" pointcut-ref="transactionPointcut"/>
            <aop:after-returning method="afterReturning"
                pointcut-ref="transactionPointcut"/>
        </aop:aspect>
    </aop:config>

(4)激活代理
在spring-servlet.xml里

<!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy />

(ps:
1. 配置的位置不对容易导致报错!尽量放在DispatcherServlet对应的xml文件里
2. proxy-target-class默认为false,如果配成true,则需导入CGLIB包
)
5.代码测试
LoginService.java

public interface LoginService {
    //设置数据源
    @DataSource(name = DataSource.sdataSource)
    User findUser(String username);
}

LoginServiceImpl .java

@Service
public class LoginServiceImpl implements LoginService{

    @Autowired
    private UserMapper userMapper;

    public User findUser(String username) {
        User user = userMapper.findUser(username);
        return user;
    }

}

Controller

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    @ResponseBody
    @DataSource(name = DataSource.sdataSource)
    public Object login(String username){
        User user = loginService.findUser(username);
        if (user == null) {
            return "该用户未注册!";
        }
        return user;
    }
}

测试结果:
这里写图片描述
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值