环境:intellij15,jdk1.8,maven3
参考:http://www.jb51.net/article/104053.htm
1、 生成ssm项目,参见http://liguanshi.iteye.com/blog/2413023
2、 在jdbc.properties文件中添加数据库连接
#============================================================================ # postgresql #============================================================================ jdbc.postgresql.driver = org.postgresql.Driver jdbc.postgresql.url = jdbc:postgresql://127.0.0.1:5432/cz?characterEncoding=UTF-8 jdbc.postgresql.username = postgres jdbc.postgresql.password = ****** #============================================================================ # oracle #============================================================================ jdbc.oracle.driver=oracle.jdbc.driver.OracleDriver jdbc.oracle.url=jdbc:oracle:thin:@smart-dafeng:1521:orcl jdbc.oracle.username=df jdbc.oracle.password=******
3、 在applicationContext-dao.xml中添加数据库连接及multipleDataSource的bean
<?xml version="1.0" encoding="UTF-8"?> <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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--获取数据库配置文件--> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!--设置数据源c3p0--> <bean id="oracleDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.oracle.driver}"/> <property name="jdbcUrl" value="${jdbc.oracle.url}"/> <property name="user" value="${jdbc.oracle.username}"/> <property name="password" value="${jdbc.oracle.password}"/> <property name="maxPoolSize" value="50"/> <property name="minPoolSize" value="2"/> <property name="maxIdleTime" value="60"/> </bean> <!--设置数据源c3p0--> <bean id="postgresqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.postgresql.driver}"/> <property name="jdbcUrl" value="${jdbc.postgresql.url}"/> <property name="user" value="${jdbc.postgresql.username}"/> <property name="password" value="${jdbc.postgresql.password}"/> <property name="maxPoolSize" value="50"/> <property name="minPoolSize" value="2"/> <property name="maxIdleTime" value="60"/> </bean> <bean id="multipleDataSource" class="com.elin4it.ssm.utils.MultipleDataSource"> <property name="defaultTargetDataSource" ref="postgresqlDataSource"/> <property name="targetDataSources"> <map> <entry key="postgresqlDataSource" value-ref="postgresqlDataSource"/> <entry key="oracleDataSource" value-ref="oracleDataSource"/> </map> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="multipleDataSource"/> </bean> <!-- mybatis.spring自动映射 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.elin4it.ssm.mapper"/> </bean> <!-- 自动扫描,多个包以 逗号分隔 --> <context:component-scan base-package="com.elin4it.ssm"/> <aop:aspectj-autoproxy/> </beans>
4、 在com.elin4it.ssm.utils添加MultipleDataSource类
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * Created by Administrator on 2018/3/21. */ public class MultipleDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>(); public static void setDataSourceKey(String dataSource) { dataSourceKey.set(dataSource); } @Override protected Object determineCurrentLookupKey() { return dataSourceKey.get(); } }
5、 将applicationContext-transaction.xml内容注释掉
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.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"> <!--<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">--> <!--<property name="dataSource" ref="dataSource"/>--> <!--</bean>--> <!--<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">--> <!--<tx:attributes>--> <!--<tx:method name="find*" propagation="REQUIRED"/>--> <!--<tx:method name="update*" propagation="REQUIRED"/>--> <!--<tx:method name="delete*" propagation="REQUIRED"/>--> <!--<tx:method name="add*" propagation="REQUIRED"/>--> <!--</tx:attributes>--> <!--</tx:advice>--> <!--<aop:config>--> <!--<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.elinzhou.ixxs.service.*.*(..))"/>--> <!--</aop:config>--> </beans>
6、 在UserServiceImpl中添加数据库选择语句:MultipleDataSource.setDataSourceKey("oracleDataSource");
完成代码如下所示:
package com.elin4it.ssm.service; import com.elin4it.ssm.mapper.StudentsMapper; import com.elin4it.ssm.pojo.Students; import com.elin4it.ssm.utils.MultipleDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by Administrator on 2018/3/21. */ @Service public class UserServiceImpl implements UserService { //User接口 此处注入UserMapper时需要在UserMapper接口上添加<span style="font-family: Arial, Helvetica, sans-serif;">@Component进行依赖注入</span> @Autowired private StudentsMapper userMapper; public Students findUser() throws Exception { // 此处切换数据库,输出不同结果 // MultipleDataSource.setDataSourceKey("postgresqlDataSource"); MultipleDataSource.setDataSourceKey("oracleDataSource"); //调用mapper类中的selectByExample方法,如果传入类型为null,则表示无条件查找 Students users = userMapper.selectByPrimaryKey(2); return users; } }
7、 在pom中添加oracle数据库依赖:
<!-- 数据库驱动 --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency>
8、 运行。Oracle数据库输出结果:
修改UserServiceImpl.java代码后,Postgresql数据库输出结果:
相关推荐
springboot mybatis多数据源加事务嵌套 事务之间的调用 回滚 亲测可用 定义2个库分别建立 CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号', `user_name` varchar(25) ...
spring+mybatis 多个数据源调用示例
增加mybatis多数据源操作,引用seate1.2处理分布式事务,多数据源事务,引用shardingSphere进行分库分表处理 项目布署图 它有什么作用 本框架使用spring cloud为基本架构,结合阿里dubbo + nacos提供服务层 再结合...
mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之...
`AbstractRoutingDataSource` 是 MyBatis-Plus 提供的一个数据源路由类。它可以基于某个条件来动态地切换数据源。当调用 `selectAnyDataSources()` 方法时,它会根据当前线程的上下文信息来返回一个数据源。如果没有...
提供了查询、更新、批处理、调用、(JTA)事务、数据源管理等功能,可以取代Mybatis、Hibernate作为系统的核心ORM框架。Rexdb提供了工具类风格的接口,不需要编写映射配置,使用简便;同时,它还具备同类框架中最...
整个采用SpringBoot框架,数据库是MySQL的5.5.X版本,如果是8.0的换pom的mysql依赖包和驱动,阿里推出的Druid数据源配置,访问项目后面加/druid进入druid的界面监控,Freemarker伪静态显示数据,利用MyBatis的XML...
调用webservice,插入数据,整合druid监控数据源; webservice地址:http://localhost:8080/taixingMsg/webservice/SendMsgService?wsdl; webservice配置文件在spring-mvc.xml和web.xml中; 数据库监控地址...
dropwizard-guice-jpa-seed 这是一个用 Java 编写的示例 REST 应用程序。...使用 HTTP REST 调用访问和操作实体的模式(资源 => 服务 => DAO => 实体) 包含 ServiceErrorMessages 的 ServiceResult 对
Crabc 是低代码接口开发平台,企业级API管理系统,深度整合SpringBoot和Mybatis实现动态数据源和动态SQL。 支持接入(mysql、oracle、postgresql、sqlserver、达梦、TiDB、es)等SQL或/NoSQL数据源, 在编辑框内编写...
这是一个最基本的基于java工程的eclipse项目,支持根据ID查询用户、根据姓名查询用户、查询所有用户、模糊查询、插入数据、更新数据、删除一条数据、删除所有数据,可以使用Mysql或者oracle
springboot+mybatis+echarts+webmagic 实现过程 用webmagic爬取腾讯,百度疫情网站,获取数据 将返回的数据存储在mysql中 编写业务,在controller中调用业务 用ajax获取controller传来的数据 -------- <项目介绍> ...
使用Springboot+Mybatis搭建整个系统 使用Thymeleaf模板技术实现页面静态化 使用框架Bootstrap、JQuery开发前端界面 使用MySQL和MongoDB分别存储数据和图片 使用Redis缓存来提升数据库查询性能 主要分为四个模块: ...
java微服务spring cloud教程3_棒.zip ...7.mybatis多数据源 8.消息队列 9.Spring Cloud Bus 10.网关 11.nosql 12.分布式系统调用跟踪 13.其它服务发现组件 14.基于微服务的开源产品 15.发布boot项目 单元测试
Geek-Framework是在SpringBoot基础上搭建的一个Java基础开发框架,以Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro和Spring-Security为权限授权层,redis进行缓存。 Geek-Framework主要定位于微...
数据源的配置以及有关数据库连接的参数都在Spring的配置文件中进行配置。 2)业务层:Service层 作用:Service层主要负责业务模块的逻辑应用设计。 先设计接口然后再设计实类,然后再在Spring的配置文件中配置其...
数据源的配置以及有关数据库连接的参数都在Spring的配置文件中进行配置。 2)业务层:Service层 作用:Service层主要负责业务模块的逻辑应用设计。 先设计接口然后再设计实类,然后再在Spring的配置文件中配置其...
数据源 Druid 权限框架 Shiro 模板引擎 thymeleaf SAP访问 sapjco3 定时任务调度 Quartz 前端 描述 框架 核心框架 、 路由框架 (纯js轻量级路由框架) mvvm框架 (专为jquery编写的mvvm) 主要特色 单页面 / 响应式 ...
1、该项目整合了nacos注册中心 + openFeign远程调用 + gateway网关+ tkMybatis+druid+dynamic-datasource多数据源 + pagehelper分页插件 + MGB自动代码生成 + seata分布式事务+ sentinel服务限流降级 + sleuth-...