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


配置数据源为:

MySQL5.5.6

H2Database 1.3.75


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


spring-core.xml



    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
        
        
        
    
    
        
    
    
        
        
        
    
    
    
        
    
    
        
            
            
            
            
            
            
            
            
            
        
    
    
        
        
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
        
        
        
    
    
        
    
    
        
        
        
    
    
        
        
    
    
        
    
    
        
            
            
            
            
            
            
        
    
    
        
        
    


spring-back.xml



    
    
    
    
    
    
    



BaseMybatisDAO

/**
 * 通用DAO的Mybatis实现
 *
 * @author leizhimin 11-12-12 下午10:42
 */
public abstract class BaseMybatisDAO extends SqlSessionDaoSupport implements GenericDAO {
    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方法。


测试下:

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), [email protected](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