- 浏览: 142299 次
- 性别:
- 来自: 合肥
文章分类
最新评论
-
hzyhush:
Java模拟登录新浪微博 -
meaemz:
大神,这是完整的代码吗?怎么没有没有main(),我要怎么调用 ...
Java模拟登录新浪微博 -
meaemz:
大神,这是完整的代码吗?怎么没有没有main(),是要自己写一 ...
Java模拟登录新浪微博 -
c_he_n:
最后执行结果怎么获取到url
Java模拟登录新浪微博 -
hvang1988:
18.JEgg Java多线程开发包 ,这个到底怎么样哦,资料 ...
Java 常用的第三方组件
spring mybatis多数据源切换(1)
- 博客分类:
- SSH+i/mbatis
项目中既要连接mysql数据库又要连接sever sql数据库,做了如下设置,实现数据库的连接。
1、在resource.properties中配置不同的数据库连接信息。
2、在mybaits.xml文件中进行多数据源的配置。
3、写一个继承AbstractRoutingDataSource的类DynamicDataSource,和DataSourceContextHolder处理类。
DynamicDataSource代码如下:
DataSourceContextHolder代码如下:
4、在实现代码中加入切换数据源的控制,在调用service之前。
1、在resource.properties中配置不同的数据库连接信息。
#连接mysql数据库 db.driverClass=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:3306/qyjx?useUnicode=true&characterEncoding=utf-8&useSSL=false db.user=root db.password=sa db.validationQuery=select 1 #连接seversql数据库 msdb.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver msdb.url=jdbc:sqlserver://127.0.0.1:1433; DatabaseName=zjys1024 msdb.user=sa msdb.password=Zgpt-svr msdb.validationQuery=select GETDATE()
2、在mybaits.xml文件中进行多数据源的配置。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性driverClassName、 url、user、password --> <property name="driverClassName" value="${db.driverClass}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.user}"/> <property name="password" value="${db.password}"/> <!-- 配置初始化大小、最小、最大 --> <!-- 通常来说,只需要修改initialSize、minIdle、maxActive --> <!-- 初始化时建立物理连接的个数,缺省值为0 --> <property name="initialSize" value="2"/> <!-- 最小连接池数量 --> <property name="minIdle" value="2"/> <!-- 最大连接池数量,缺省值为8 --> <property name="maxActive" value="10"/> <!-- 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 --> <property name="maxWait" value="60000"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒(3600000:为1小时) --> <property name="timeBetweenEvictionRunsMillis" value="3600000"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒(300000:为5分钟) --> <property name="minEvictableIdleTimeMillis" value="300000"/> <!-- 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。 --> <property name="validationQuery" value="${db.validationQuery}"/> <!-- 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。建议配置为true,不影响性能,并且保证安全性。--> <property name="testWhileIdle" value="true"/> <!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。缺省值:true --> <property name="testOnBorrow" value="false"/> <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。缺省值:false --> <property name="testOnReturn" value="false"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <!-- 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。5.5及以上版本有PSCache,建议开启。缺省值:false --> <property name="poolPreparedStatements" value="true"/> <!-- 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100。 --> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> </bean> <!--sqlserver配置数据源--> <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${msdb.driverClass}"/> <property name="url" value="${msdb.url}"/> <property name="username" value="${msdb.user}"/> <property name="password" value="${msdb.password}"/> <!-- 初始化连接大小 --> <property name="initialSize" value="2"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="10"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="2"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000"></property> </bean> <bean id="dynamicDataSource" class="com.winning.common.mybatis.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="dataSource" key="dataSource"></entry> <entry value-ref="dataSource2" key="dataSource2"></entry> </map> </property> <!-- 默认使用dataSource的数据源 --> <property name="defaultTargetDataSource" ref="dataSource"></property> </bean> <!-- spring jdbcTemplate DAO层模版 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- 将数据源映射到sqlSessionFactory中 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:MyBatis/mybatis-config.xml"/> <property name="dataSource" ref="dynamicDataSource"/> <property name="mapperLocations" value="classpath*:MyBatis/mapper/**/*.xml"></property> </bean> <!-- SqlSession模板类实例 --> <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!-- 事务管理器(由Spring管理MyBatis的事务) --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dynamicDataSource"></property> </bean> <!-- 打开事务注解功能--> <tx:annotation-driven transaction-manager="transactionManager"/> <!--声明式事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true" rollback-for="java.lang.Exception"/> <tx:method name="search*" propagation="REQUIRED" read-only="true" rollback-for="java.lang.Exception"/> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="import*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.winning..*.*Service.*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> </aop:config> </beans>
3、写一个继承AbstractRoutingDataSource的类DynamicDataSource,和DataSourceContextHolder处理类。
DynamicDataSource代码如下:
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * Created by TonyJ on 2019/6/5. */ public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDbType(); } }
DataSourceContextHolder代码如下:
/** * Created by TonyJ on 2019/6/5. */ public class DataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static void setDbType(String dbType) { contextHolder.set(dbType); } public static String getDbType() { return ((String) contextHolder.get()); } public static void clearDbType() { contextHolder.remove(); } }
4、在实现代码中加入切换数据源的控制,在调用service之前。
/** * Created by TonyJ on 2019/6/5. */ @Controller @RequestMapping("/test") public class TestController extends AbstractController { @Autowired private SysLogService sysLogService; @RequestMapping(value = "list") public @ResponseBody Map list(@RequestMap MapAdapter mapAdapter, HttpServletRequest request, HttpServletResponse response) throws Exception { Map param= new HashMap(); //注意这里在调用service前切换到dataSource2的数据源 DataSourceContextHolder.setDbType("dataSource2"); List<Map> dataList = sysLogService.querySyhjAllDatasByParams(param); return dataList.get(0); } }
发表评论
-
项目操作日志记录(方法级别)
2019-06-12 16:45 930在项目中要记录操作日志,可以通过spring的Aop技术去实现 ... -
spring mybatis多数据源切换(2)
2019-06-12 16:35 414在上一篇文章中写到,多数据源的切换是使用Java代码调用的,现 ... -
Spring定时器的功能开发
2016-12-05 16:59 21611、使用quartz.jar包实现定时器功能,首先在项目中引入 ... -
Maven多模块SSM框架搭建
2016-12-05 15:07 1542整合SSM和Maven多模块,搭建一个简单的开发架构,具体的做 ... -
Hibernate缓存机制分析
2015-04-09 23:18 645复制代码 您可以通过点击 右下角 的按钮 来对文章内容作出评价 ... -
Spring管理事务的几种方式
2015-04-09 22:33 956把Hibernate用到的数据源Datasource,Hibe ... -
Hibernate注解说明
2015-04-03 18:03 795/** * * @author liuguang ... -
struts2请求过程源码分析
2015-02-04 21:08 621struts2请求过程源码分析 这篇文章写的很好 分析很透彻 ... -
spring和mybatis泛型接口的整合(一)
2015-02-03 20:42 203871、这次使用spring和mybatis进行整合,使用spri ... -
spring和ibatis综合应用入门示例
2012-03-20 20:00 12411、最近几天在学习ibatis ... -
spring开发所需jar包
2012-03-14 09:17 1104spring2.5开发所需的jar包,上传到这里,方便下载使用 ... -
ibatis自动生成主键
2012-03-13 10:53 1045很多数据库支持自动生成主键的数据类型如(mysql)。不 ... -
ibatis入门教程
2012-03-12 20:41 183891、本文通过几个简单的示例,实现ibatis的增删改查的功能。 ... -
ibatis一对多示例
2012-03-12 15:20 15381、在实际开发中,iBATIS框架已经很普遍的使用,然而我们常 ... -
hibernate入门示例展示
2011-06-30 17:00 1229有很长一段时间没有使用hibernate进行开发了,最近闲来无 ...
相关推荐
spring mybatis 多数据源动态切换
spring整合mybatis多数据源切换 可以运行的demo实例
mybatis+spring实现动态切换数据源,修改数据源配置信息之后,直接运行test可进行测试
Mybatis+Spring+SpringMVC+quartz多数据源切换 定时任务 多数据库之间同步
springboot+druid+mybatis多数据源动态切换,基于sql类型完成读写分离操作
SpringBoot整合mybatis-plus实现多数据源的动态切换且支持分页查询,案例以postgresql和oracle数据库为数据源,分别使用mybatis-plus分页插件和pagehelper分页插件实现分页查询。
Spring-boot+mybatis多数据源整合(动态切换数据源),亲测可用
使用springboot + JPA / MyBatis 实现多数据源动态切换
springboot实现数据源动态切换 注意事项: 1. 该demo采用yml配置数据库信息,注意url标签为jdbc-url 2.项目中加了日志输出,可看到完整执行过程 3.在Service中应用事务时,自定义的注解将失效,解决办法:可将注解...
mybatis+spring实现动态切换数据源,修改数据源配置信息之后,直接运行test可进行测试 maven项目 导入即可 jar包都不用 适合学习参考 出自Java自学网 (Java自学网专供 就是免积分下载)
spring-boot集成mybatis+druid实现 hive/mysql多数据源切换,用mysql数据库作为用户验证库以及用户信息库,hive作为数据可视化源库。
Spring+SpringMvc+MybatisPlus实现多数据源切换、利用自定义Aop注解,只需要在需要切换数据库的方法上加上注解即可实现、极大避免了代码冗余。
SpringMVC+Mybatis(多数据源)+Mongo(多数据源),框架适合同时访问多种DB,且支持每种DB配置多个数据源。正如我代码中的demo,配置了两个mysql数据源+2个mongo数据源,可以在service实现中随意访问。除此之外还通过...
springboot+mybatis结合,多数据源配置,并实现注解aop动态切换,可实现读写分离,maven结构。
使用aop进行多数据源切换 springMVC+spring+mybatis增删改查的使用。dk8+tomcat8+mysql+Eclipse+maven。spring+spring mvc+mybatis+bootstrap+jquery
spring boot mybatis plus 多数据源切换的demo解压运行
SpringBoot+Druid+MyBatis实现多数据源切换
主要介绍了Spring + Mybatis 项目实现动态切换数据源的相关资料,需要的朋友参考下吧
Spring Boot整合Mybatis使用druid实现多数据源自动切换
下面小编就为大家带来一篇spring boot+mybatis 多数据源切换(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧