Spring MVC框架搭建扩展--多数据源配置(一)
来源:互联网 发布:中文分词最大匹配算法 编辑:程序博客网 时间:2024/05/18 16:13
本节主要介绍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)@Documentedpublic @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
@Servicepublic class LoginServiceImpl implements LoginService{ @Autowired private UserMapper userMapper; public User findUser(String username) { User user = userMapper.findUser(username); return user; }}
Controller
@Controllerpublic 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
- Spring MVC框架搭建扩展--多数据源配置(一)
- Spring MVC框架搭建扩展--多数据源配置(二)
- spring MVC+MyBatis 多数据源配置
- spring mvc 多数据源的配置
- spring mvc 多数据源配置
- Spring MVC 配置多数据源
- Spring MVC +MyBatis 多数据源配置
- Spring mvc 配置mongodb多数据源
- spring框架学习【多数据源配置】
- spring框架学习【多数据源配置】
- Spring多数据源配置方式一
- Spring多数据源配置系列(一)——多数据源配置
- Spring mvc mybaties druid 搭建多数据源
- 搭建一个spring mvc框架(一)
- Spring MVC学习(一) 框架搭建
- Spring MVC框架搭建(一)
- spring mvc+mybatis+多数据源切换(一)
- Spring MVC多数据源动态切换配置
- java web 实现把文件夹下的所有文件压缩成zip并实现下载(原文链接)http://blog.csdn.net/sxdtzhaoxinguo/article/details/20550537)
- 【MyBatis学习09】高级映射之一对多查询
- HTML body顶格,边框0
- 10个快速提升技术水平的方法
- 浅析Javascript匿名函数与自执行函数 (闭包)
- Spring MVC框架搭建扩展--多数据源配置(一)
- 判断链表是是否是回文
- POJ 2955 Brackets 【区间DP】
- js事件冒泡和事件捕获详解
- 回溯线搜索 Backtracking line search
- 定时器
- Laravel修改器取出原始值
- Hyperledger Fabric SDK 示例fabric-samples-《balance-transfer》之三《加入到Channel》
- string,stringBuilder,字符串的常用方法,键盘录入字符串,string的构造方法,string类的各种判断功能,string类的各种获取功能,string类的转换功能,切割方法