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

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


配置数据源为:

MySQL5.5.6

H2Database 1.3.75


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


spring-core.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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/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
            http://www.springframework.org/schema/context
            http://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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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/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"
       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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * 通用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的命名空间。
    @Override
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }
    @Override
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }


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


测试下:

1
2
3
4
5
6
7
8
9
10
11
12
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), null0(Integer), 0(Integer), 0(Integer), asdf@asdf.com(String), 139232302033(String), nullnullnull0(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





本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/1396643,如需转载请自行联系原作者


上一篇:Worksheet对象 应用示例


下一篇:NOI2021同步赛 游记