Spring3.2.8+Mybatis3.2.6 多数据源基于BaseDAO的配置

配置数据源为:

MySQL5.5.6

H2Database 1.3.75

这个配置起来比较麻烦,本文这种方法有点麻烦,就是dao不能再用注解了,但是程序简单。还有别的方法,后续放出。

spring-core.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"><context:annotation-config/><context:property-placeholder location="classpath*:framework/jdbc.properties"/><!-- 配置系统的数据源 --><bean id="dataSourceMySQL" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/qhtf"/><property name="username" value="root"/><property name="password" value="leizm"/><property name="filters" value="stat"/><property name="maxActive" value="10"/><property name="initialSize" value="1"/><property name="maxWait" value="60000"/><property name="minIdle" value="1"/><property name="timeBetweenEvictionRunsMillis" value="60000"/><property name="minEvictableIdleTimeMillis" value="300000"/><property name="validationQuery" value="SELECT 'x'"/><property name="testWhileIdle" value="true"/><property name="testOnBorrow" value="false"/><property name="testOnReturn" value="false"/><property name="poolPreparedStatements" value="true"/><property name="maxPoolPreparedStatementPerConnectionSize" value="50"/><property name="maxOpenPreparedStatements" value="100"/></bean><bean id="sqlSessionFactoryMySQL" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:framework/mybatis-config.xml"/><property name="mapperLocations" value="classpath:/com/lavasoft/aac/entity/sqlmap/*.xml"/><property name="dataSource" ref="dataSourceMySQL"/></bean><bean id="sqlSessionTemplateMySQL" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactoryMySQL"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateMySQL"/><property name="basePackage" value="com.lavasoft.aac.dao"/></bean><!-- 事务管理器配置,单数据源事务 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSourceMySQL"/></bean><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="select*" read-only="true"/><tx:method name="get*" read-only="true"/><tx:method name="load*" read-only="true"/><tx:method name="find*" read-only="true"/><tx:method name="query*" read-only="true"/><tx:method name="count*" read-only="true"/><tx:method name="read*" read-only="true"/><tx:method name="sync*"/><tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="executeService" expression="execution(* com.lavasoft.aac.service.*SVImpl.*(..))"/><aop:advisor pointcut-ref="executeService" advice-ref="txAdvice"/></aop:config><!-- ================================H2================================== --><!--H2内存数据库配置--><bean id="dataSourceH2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="driverClassName" value="org.h2.Driver"/><property name="url" value="jdbc:h2:mem:/memdb;DB_CLOSE_DELAY=-1"/><property name="username" value="sa"/><property name="password" value=""/><property name="filters" value="stat"/><property name="maxActive" value="10"/><property name="initialSize" value="1"/><property name="maxWait" value="60000"/><property name="minIdle" value="1"/><property name="timeBetweenEvictionRunsMillis" value="60000"/><property name="minEvictableIdleTimeMillis" value="300000"/><property name="validationQuery" value="SELECT 'x'"/><property name="testWhileIdle" value="true"/><property name="testOnBorrow" value="false"/><property name="testOnReturn" value="false"/><property name="poolPreparedStatements" value="true"/><property name="maxPoolPreparedStatementPerConnectionSize" value="50"/><property name="maxOpenPreparedStatements" value="100"/></bean><bean id="sqlSessionFactoryH2" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:framework/h2SqlMapConfig.xml"/><property name="mapperLocations" value="classpath:/com/lavasoft/aac/entity/sqlmap/*.xml"/><property name="dataSource" ref="dataSourceH2"/></bean><bean id="sqlSessionTemplateH2" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactoryH2"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryH2"/><property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateH2"/><property name="basePackage" value="com.lavasoft.aac.daoh2"/></bean><bean id="baseDAO" class="com.lavasoft.framework.core.BaseMybatisDAO" abstract="true"><property name="sqlSessionFactory" ref="sqlSessionFactoryH2"/><property name="sqlSessionTemplate" ref="sqlSessionTemplateH2"/></bean><bean id="transactionManagerH2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSourceH2"/></bean><tx:advice id="h2txAdvice" transaction-manager="transactionManagerH2"><tx:attributes><tx:method name="create*" rollback-for="Exception"/><tx:method name="delete*" rollback-for="Exception"/><tx:method name="save*" rollback-for="Exception"/><tx:method name="insert*" rollback-for="Exception"/><tx:method name="update*" rollback-for="Exception"/><tx:method name="*" read-only="true" rollback-for="Exception"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="executeServiceH2" expression="execution(* com.lavasoft.ntv.service.*SVImpl.*(..))"/><aop:advisor pointcut-ref="executeServiceH2" advice-ref="h2txAdvice"/></aop:config>
</beans>

spring-back.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"xmlns:context="http://www.springframework.org/schema/context"><context:annotation-config/><!--<context:component-scan base-package="com.lavasoft.aac.daoh2" resource-pattern="Sys_userDAO.class"/>--><context:component-scan base-package="com.lavasoft.aac.service" resource-pattern="H2SV.class"/><!--<context:component-scan base-package="com.lavasoft.aac.dao" resource-pattern="*DAO.class"/>--><!--<context:component-scan base-package="com.lavasoft.aac.service" resource-pattern="*SVImpl.class"/>--><bean id="sys_userDAO" class="com.lavasoft.aac.daoh2.Sys_userDAO" parent="baseDAO"/><import resource="classpath:/framework/spring-core.xml"/>
</beans>

BaseMybatisDAO

/*** 通用DAO的Mybatis实现** @author leizhimin 11-12-12 下午10:42*/
public abstract class BaseMybatisDAO<E, PK extends Serializable> extends SqlSessionDaoSupport implements GenericDAO<E, PK> {protected String sqlmapNamespace;   //ibatis sql map的命名空间,即使用实体类的简单名称protected Class entityType;         //运行时的实体类型,也对应为SQL的命名空间。@Overridepublic void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {super.setSqlSessionFactory(sqlSessionFactory);}@Overridepublic void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {super.setSqlSessionTemplate(sqlSessionTemplate);}

BaseMybatisDAO省略具体实现代码,仅保留需要注入的两个setter方法。

测试下:

DEBUG 2014-04-16 17:33:35 org.mybatis.spring.SqlSessionUtils:104 - Creating a new SqlSession
DEBUG 2014-04-16 17:33:35 org.mybatis.spring.SqlSessionUtils:140 - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d58ce2] was not registered for synchronization because synchronization is not active
DEBUG 2014-04-16 17:33:35 org.springframework.jdbc.datasource.DataSourceUtils:110 - Fetching JDBC Connection from DataSource
DEBUG 2014-04-16 17:33:35 org.mybatis.spring.transaction.SpringManagedTransaction:86 - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@1a7789c] will not be managed by Spring
DEBUG 2014-04-16 17:33:35 Sys_user.insert:139 - ==>  Preparing: insert into sys_user( fullname, account, password, salt, isExpired, isLock, createtime, status, email, mobile, phone, sex, picture, fromtype ) values( ?, ?, ?, ?, ?, ?, now(), ?, ?, ?, ?, ?, ?, ? )
DEBUG 2014-04-16 17:33:35 Sys_user.insert:139 - ==> Parameters: leizm(String), asdfa(String), 23492399(String), null, 0(Integer), 0(Integer), 0(Integer), asdf@asdf.com(String), 139232302033(String), null, null, null, 0(Integer)
DEBUG 2014-04-16 17:33:35 Sys_user.insert:139 - <==    Updates: 1
DEBUG 2014-04-16 17:33:35 com.alibaba.druid.pool.PreparedStatementPool:123 - {conn-10002, pstmt-20003} enter cache
DEBUG 2014-04-16 17:33:35 org.mybatis.spring.SqlSessionUtils:168 - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d58ce2]
DEBUG 2014-04-16 17:33:35 org.springframework.jdbc.datasource.DataSourceUtils:327 - Returning JDBC Connection to DataSource
1
Process finished with exit code 0

Spring3.2.8+Mybatis3.2.6 多数据源基于BaseDAO的配置相关推荐

  1. springboot添加多数据源连接池并配置Mybatis

    springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018  ...

  2. mybatis配置mysql数据源_springboot+mybatis+Druid配置多数据源(mysql+postgre)

    springboot+mybatis+Druid配置多数据源(mysql+postgre) 引入pom依赖 org.mybatis.spring.boot mybatis-spring-boot-st ...

  3. springboot数据源不正确_springboot配置多数据源

    在做项目的过程中难免会遇到这种情况:一个项目需要两个数据库中的数据,希望这篇文章能给遇到这些问题的小伙伴一点帮助 第一步:将两个数据源的mapper接口和xml文件分别放入不同的文件夹下: 第二步:在 ...

  4. spring 整合 mybatis 中数据源的几种配置方式

    因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...

  5. java数据源的几种配置

    Java中的数据源就是javax.sql.DataSource.DataSource的创建可以有不同的实现. JNDI方式创建DataSource 以JNDI方式创建数据源首先要配置数据源的相关连接信 ...

  6. spring数据源、连接池配置

    Spring提供了两个这样的数据源(都位于org.springframework.jdbc.datasource程序包里): DriverManagerDataSource:在每个连接请求时都新建一个 ...

  7. Druid数据库密码加密 包含单数据源密码加密,多数据源密码加密详细配置

    发个牢骚 网上虽然有很多相关文章,但是我各种翻看文章依然是各种bug层出不穷,所幸最后终于摸索出了答案,我的第一篇博客应运而生.. 开工 别慌,看到我这篇文章,你的问题就迎刃而解了 一.获取公钥和加密 ...

  8. mybatis 配置多数据源 java,SpringBoot+MyBatisPlus配置多数据源读写分离

    首先呢,我们这里使用MySQL的数据库,可以简单配置一下主从备份来实现两个数据库的数据同步 项目的配置目录大概是这样的作为参考 第一步.定义一个枚举类声明当前的数据源类型 package com.yu ...

  9. 数据源的定义以及配置

    数据源是指数据库应用程序所使用的数据库或者数据库服务器. 数据源(Data Source)顾名思义,数据的来源,是提供某种所需要数据的器件或原始媒体.在数据源中存储了所有建立数据库连接的信息.就像通过 ...

最新文章

  1. 线程安全的ConcurrentQueueT队列
  2. Dubbo 3.0 - 开启下一代云原生微服务
  3. 开放下载!《DTS控制台入门一本通》
  4. Spring整合ibatis的配置
  5. 两个空间点直接距离投影公式_线积分与面积分(2):最初的公式
  6. 《网络规划设计师考试大纲》、《网络规划设计师教程》和《系统架构设计师教程》...
  7. 国科大高级人工智能5-RNN/LSTM/GRU/集束搜索/attention
  8. linux-修改所有者与所属组
  9. error40无法打开到sql_SQL-mysql游标与触发器
  10. 如果微软开发了 Android,会有何不同?
  11. The content of element type “resultMap“ must match “(constructor?,id*,result*,association*,collectio
  12. 打补丁更新不适用计算机,安装补丁“此更新不适用于你的计算机”解决办法
  13. redis内存碎片问题
  14. win10怎么打开无线网络服务器,win10系统打开无线网络服务的操作方法
  15. 首次使用计算机鼠标键盘不能用,电脑鼠标键盘都不能用如何解决 开机后鼠标键盘不能用怎么办...
  16. 3W字,Docker 从入门到精通
  17. linux+硬盘热插拔原理,Linux下关于热插拔硬盘的指令
  18. android注册页面开发
  19. 【VulnHub靶场】——HARRYPOTTER第一部: ARAGOG (1.0.2)
  20. 两个int类型数据交换的神级操作

热门文章

  1. linux在机房设置ip,双线机房双IP linux设置路由
  2. 怎样用c语言写高速超速罚款标准,pta高速公路超速处罚(C语言)
  3. sqlserver中能用when_sqlserver中if语句顶替when.case.语句
  4. 【每日一题】212. 单词搜索 II
  5. C++——智能指针——auto_ptr、shared_ptr、unique_ptr
  6. 五分钟看完 RocketMQ应用
  7. burp suite java_Burpsuite插件自动二手开发
  8. python中字典格式_如何在Python中使用带有字典的格式函数和整数键
  9. linux搭建markdown服务,Markdown新手快速入门基础教程及Ubuntu下的安装
  10. Android图片颜色比例,Android开发学习之路-图片颜色获取器开发(1)