如何使用spring配合mybatis配置多个数据源并应用?
使用多数据源的场景应该是很多的,如操作同一台服务器上不同的数据库,或者多地机器上的相同或不相同数据库。
虽然涉及到不同数据库时,我们也许可以通过跨库操作的方式,如 other.user 使用同一数据源来操作数据库,但是,这样明显使得应用很难扩展,单数据库将无法拆离。使用多数据源操作则可以解决这个问题。
在spring中怎样使用多数据源?本文通过实践方式,让我配置多数据源有个参考。(不得不说,java中很大的一个难点就在于配置环境)
从入口处更改,web.xml中添加引用spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>c</display-name>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- URL重写 -->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping> <servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping> <display-name>c</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <filter>
<filter-name>JAMonFilter</filter-name>
<filter-class>com.xx.core.web.filter.PageMonFilter</filter-class>
<init-param>
<param-name>flag</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>JAMonFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>exceptions</servlet-name>
<jsp-file>/jamon/exceptions.jsp</jsp-file>
</servlet>
<servlet>
</web-app>
2. 在application.xml中添加多数据源配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd"> <bean class="com.xx.c.common.utils.SpringContextsUtil" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="${pool.maxPoolSize}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 超过时间限制是否回收 -->
<property name="removeAbandoned" value="true" />
<!-- 超时时间;单位为秒。180秒=3分钟 -->
<property name="removeAbandonedTimeout" value="${pool.removeAbandonedTimeout}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${pool.maxWait}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${pool.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${pool.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${pool.validationQuery} " />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
</bean>
<bean id="dataSource_c" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc2.url}" />
<property name="driverClassName" value="${jdbc2.driver}" />
<property name="maxActive" value="${pool2.maxPoolSize}" />
<property name="username" value="${jdbc2.username}" />
<property name="password" value="${jdbc2.password}" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="${pool2.removeAbandonedTimeout}" />
<property name="maxWait" value="${pool2.maxWait}" />
<property name="timeBetweenEvictionRunsMillis" value="${pool2.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${pool2.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${pool2.validationQuery} " />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
</bean>
<context:component-scan base-package="com.xx.c">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<context:component-scan base-package="com.xx.c.pojo.config" />
<bean class="com.xx.framework.web.v1_0_0.ServerController" /> <!-- spring的属性加载器,加载properties文件中的属性 -->
<bean class="com.xx.zkc.property.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:/spring/conf.properties</value>
</list>
</property>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean id="sqlSessionFactory_c" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_c" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"
scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="sqlSession_c" class="org.mybatis.spring.SqlSessionTemplate"
scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory_c" />
</bean> <mongo:mongo id="mongo" replica-set="${mongodb.url}">
<mongo:options connections-per-host="${mongo.connections.per.host}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threads.allowed.to.block.for.connection.multiplier}"
connect-timeout="${mongo.connect.timeout}" max-wait-time="${mongo.max.wait.time}"
auto-connect-retry="${mongo.auto.connect.retry}" socket-keep-alive="${mongo.socket.keep.alive}"
socket-timeout="${mongo.socket.timeout}" slave-ok="${mongo.slave.ok}"
write-number="${mongo.write.number}" write-timeout="${mongo.write.timeout}"
write-fsync="${mongo.write.fsync}" />
</mongo:mongo>
<bean id="mappingContext"
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="defaultMongoTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey">
<null />
</constructor-arg>
</bean>
<bean id="mongoDbFactory"
class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="${mongodb.databaseName}" />
</bean> <bean id="mappingMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean> <bean id="mongodbReadPreference" class="com.mongodb.ReadPreference"
factory-method="secondaryPreferred" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<property name="readPreference" ref="mongodbReadPreference" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean> <!-- 事务 控制 begin -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 事务 控制 end --> <!-- action begin -->
<bean name='requestMappingHandlerMapping'
class='com.xx.core.web.spring.annotation.PackageURLRequestMappingHandlerMapping'>
<property name='packageBase' value='com.xx.c.action'></property>
<property name="interceptors">
<list>
<bean class="com.xx.core.web.spring.interceptor.SessionInterceptor">
<property name="on" value="${system.check.permission}" />
<property name="sessionTimeout" value="${system.timeout}" />
<property name="userSessionDao" ref="userSessionDao" />
<property name="systemDao" ref="systemDao" />
</bean>
</list>
</property>
</bean> <!-- dao -->
<import resource="dao.xml" /> <!-- service -->
<import resource="service.xml" /> <!-- activemq -->
<import resource="applicationContext-activemq.xml" /> <!-- redis -->
<import resource="applicationContext-redis.xml" /> <!-- dubbo -->
<import resource="applicationContext-dubbo.xml" />
<import resource="applicationContext-dubbo-consumer.xml" /> <import resource="classpath:spring/xx-c-kafka-consumer.xml" /> <import resource="applicationContext-rabbitmq-producer.xml"/> <import resource="applicationContext-rabbitmq-consumer.xml"/> <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="com.xx.core.web.spring.convert.JSONHttpMessageConverter">
</bean>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
</bean>
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
</bean>
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter">
</bean>
<bean
class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter">
</bean>
</list>
</property>
<property name="customArgumentResolvers">
<list>
<bean class="com.xx.core.web.spring.bind.TIDArgumentResolver" />
<bean
class="com.xx.core.web.spring.bind.RequestAttributeArgumentResolver" />
<bean class="com.xx.core.web.spring.bind.ClientIPArgumentResolver" />
<bean
class="com.xx.core.web.spring.bind.SessionUserIdArgumentResolver" />
</list>
</property>
</bean> <bean id="dealException" class="com.xx.core.web.spring.interceptor.ExceptionAop"></bean>
<aop:config>
<!-- 对异常的集中处理 -->
<aop:aspect id="exceptionAop" ref="dealException">
<aop:pointcut id="exceptionPointCut"
expression="execution(* com.xx.c.action.*.*.*(..))" />
<aop:around pointcut-ref="exceptionPointCut" method="deal" />
</aop:aspect>
</aop:config> <bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>systemDictDao</value>
<value>userDao</value>
<value>infoDao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>jamonInterceptor</value>
</list>
</property>
</bean>
<bean id="jamonInterceptor"
class="org.springframework.aop.interceptor.JamonPerformanceMonitorInterceptor"></bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" p:maxUploadSize="104857600" />
<context:component-scan base-package="com.xx.c.action"
name-generator="com.xx.core.web.spring.annotation.FullPackageBeanNameGenerator" />
<!-- action end -->
<!-- Spring托管线程池 -->
<bean id="threadPoolTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 线程池活跃的线程数 -->
<property name="corePoolSize" value="50" />
<!-- 线程池最大活跃的线程数 -->
<property name="maxPoolSize" value="75" />
<!-- 队列的最大容量 <property name ="queueCapacity" value ="2000" /> -->
<!-- <property name="WaitForTasksToCompleteOnShutdown" value="true" /> -->
</bean> <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles">
<list>
<value>classpath:/dozerBeanMapping.xml</value>
<value>classpath:/dozerBeanMapping2.xml</value>
</list>
</property>
</bean> </beans>
3. 在dao.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- import this -->
<bean id="userDao" class="com.xx.c.dao.user.UserDaoImpl">
<property name="sqlSession" ref="sqlSession" />
<property name="mongoTemplate" ref ="mongoTemplate" />
</bean> <bean id="infoDao" class="com.xx.c.dao.content.ConInfoDaoImpl">
<property name="sqlSession" ref="sqlSession_c" />
</bean>
</beans>
4. 在具体的sql当中实现数据操作:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xx.c.dao.user.UserDao">
<resultMap id="userMap" type="User">
<id column="user_id" property="userId" />
<result column="username" property="userName" />
<result column="sex" property="sex" />
</resultMap>
<resultMap id="userInfoMap" type="UserInfoBean">
<id column="user_id" property="userId" />
<result column="card_id" property="cardId" />
<result column="username" property="phone" />
<result column="id_card_address" property="idCardAddress" />
<result column="reg_time" property="regTime" />
<result column="logintime" property="logintime" />
<result column="last_time" property="lastTime" />
<result column="reg_ip" property="regIp" />
<result column="up_ip" property="upIp" />
<result column="last_ip" property="lastIp" />
<result column="download_channel" property="downloadChannel" /> <result column="guarantor1" property="guarantor1" />
<result column="guarantor_phone1" property="guarantor_phone1" />
</resultMap> <sql id="getUserCond">
<where>
<if test="userId != null">
and u.user_id = #{userId}
</if>
<if test="userName != null and userName != ''">
and u.username = #{userName}
</if>
</where>
</sql> <select id="getUser" resultMap="userMap" parameterType="User">
select u.* from users_info u
<include refid="getUserCond" />
</select> <insert id="addDebitCardBindChannel" parameterType="DebitCardBindChannelBean">
insert into
nzz (
user_id,channel,
card_no,request_id,status)
values (
#{userId},#{channel},#{cardNo},#{requestId},#{status})
</insert> <update id="updateDebitCardBindChannel" parameterType="DebitCardBindChannelBean">
update
nzz
set status=#{status}
,request_id=#{requestId}
where
user_id=#{userId} and card_no=#{cardNo}
and channel=#{channel}
</update>
</mapper>
5. 完善一下,使用别名,使sql写的时候更简短:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<settings>
<setting name="logPrefix" value="dao."/>
</settings>
<!-- 配置别名,以便在引用时简写 -->
<typeAliases>
<typeAlias alias="User" type="com.xx.c.pojo.user.User" />
<typeAlias alias="CardBinBean" type="com.xx.c.pojo.user.CardBinBean"/>
<typeAlias alias="UserCreditLogBean" type="com.xx.c.pojo.user.UserCreditLogBean"/>
<typeAlias alias="InfoBean" type="com.xx.c.pojo.content.ContentInfo" />
</typeAliases> <mappers>
<mapper resource="com/xx/c/dao/impl/User.xml"/>
<mapper resource="com/xx/c/dao/impl/Info.xml"/>
</mappers> </configuration>
以上,就已经完成了一整个spring操作多数据源的mybatis了。
如何使用spring配合mybatis配置多个数据源并应用?的更多相关文章
- spring集成mybatis配置多个数据源,通过aop自动切换
spring集成mybatis,配置多个数据源并自动切换. spring-mybatis.xml如下: <?xml version="1.0" encoding=" ...
- Spring Boot + Mybatis 配置多数据源
Spring Boot + Mybatis 配置多数据源 Mybatis拦截器,字段名大写转小写 package com.sgcc.tysj.s.common.mybatis; import java ...
- spring mvc+myBatis配置详解
一.spring mvc Spring框架(框架即:编程注解+xml配置的方式)MVC是Spring框架的一大特征,Spring框架有三大特征(IOC(依赖注入),AOP(面向切面),MVC(建模M- ...
- Spring Boot MyBatis配置多种数据库
mybatis-config.xml是支持配置多种数据库的,本文将介绍在Spring Boot中使用配置类来配置. 1. 配置application.yml # mybatis配置 mybatis: ...
- Spring Boot + Mybatis多数据源和动态数据源配置
文章转自 https://blog.csdn.net/neosmith/article/details/61202084 网上的文章基本上都是只有多数据源或只有动态数据源,而最近的项目需要同时使用两种 ...
- Spring SpringMVC MyBatis配置笔记
工程大致结构: project |-src |----pojo |--------Temp.java |----dao |--------TempDao.java |--------TempDao.x ...
- spring 、Mybatis配置sql server数据库
学习了:http://www.cnblogs.com/amwicfai/p/3181003.html beans.xml中的配置写法: <!-- 数据源 --> <bean id=& ...
- maven 构建spring ssh mybatis 配置
详情参与 http://blog.csdn.net/yuguiyang1990/article/details/8811817 前面我们使用Maven构建了Struts2项目,这里我们来试一下Hibe ...
- spring+mybatis配置多个数据源
http://www.cnblogs.com/lzrabbit/p/3750803.html
随机推荐
- pip install 提示代理连接失败原因及解决办法
# pip install 提示代理连接失败原因及解决办法 1. 错误提示 在公司电脑上安装Python的虚拟环境时输入命令: pip install virtualenv 系统提示以下异常信息: R ...
- MySQL的show profile(已过时)简介以及该功能在MySQL 5.7中performance_schema中的替代
本文出处:http://www.cnblogs.com/wy123/p/6979499.html show profile 命令用于跟踪执行过的sql语句的资源消耗信息,可以帮助查看sql语句的执行情 ...
- 配置远程主机http服务器 打包资源
<1> 搭建nginx 验证nginx是否启动成功 https://blog.csdn.net/wdsdsdsds/article/details/51179780 https://ww ...
- (八)lambda、列表生成式、字典转list排序
1.函数返回多个值: 1)函数如果return多个值的话,会把返回的这几个值放在一个元组里面 def say(): num1 = 1 num2 = 2 num3 = 3 return num1,num ...
- 云笔记项目-AOP知识简单学习
在云笔记项目的过程中,需要检查各个业务层的执行快慢,如登录.注册.展示笔记本列表,展示笔记列表等,如果在每个业务层方法里都写一段代码用来检查时间并打印,不仅仅显得代码重复,而且当项目很大的时候,将大大 ...
- Java方法区(Method Area)
方法区与Java堆一样,是各个线程共享的内存区域,他在与存储已被虚拟机加载的类信息,常量,静态变量,即时编译器编译后的代码等数据,虽然Java虚拟机规范把方法区描述为堆得一个逻辑部分,但是他却有一个别 ...
- 169. Majority Element (Array)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 在IDEA中使用MyBatis Generator自动生成代码
转载自 https://blog.csdn.net/hua_faded/article/details/78900780 一.配置Maven pom.xml 文件 在pom.xml增加以下插件: ...
- ubuntu系统安装微信小程序开发工具
在ubuntu系统中安装微信小程序开发工具之前,先要安装wine与git 一.安装wine 1.如果您的系统是64位,启用32位架构(如果您还没有) sudo dpkg --add-architect ...
- java基础面试(上)
面向对象的特征 答:抽象.继承.封装.多态 short s1 = 1; s1 = s1 + 1;有错吗?short s1 = 1; s1 += 1;有错吗? 答:对于short s1 = 1; s1 ...