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

本文涉及的产品
RDSClaw,2核4GB
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介:

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 ),  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





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


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
7月前
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
7月前
|
XML Java 数据库连接
MyBatis的常见配置
MyBatis 常见配置包括数据库连接、类型别名、映射器等核心模块,合理配置可提升开发效率与系统性能。主要内容涵盖核心配置文件结构、关键配置项详解及配置优先级说明。
721 4
|
7月前
|
Java 关系型数据库 MySQL
Spring Boot自动配置:魔法背后的秘密
Spring Boot 自动配置揭秘:只需简单配置即可启动项目,背后依赖“约定大于配置”与条件化装配。核心在于 `@EnableAutoConfiguration` 注解与 `@Conditional` 系列条件判断,通过 `spring.factories` 或 `AutoConfiguration.imports` 加载配置类,实现按需自动装配 Bean。
|
7月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
2506 0
|
9月前
|
Java Spring
Spring Boot配置的优先级?
在Spring Boot项目中,配置可通过配置文件和外部配置实现。支持的配置文件包括application.properties、application.yml和application.yaml,优先级依次降低。外部配置常用方式有Java系统属性(如-Dserver.port=9001)和命令行参数(如--server.port=10010),其中命令行参数优先级高于系统属性。整体优先级顺序为:命令行参数 &gt; Java系统属性 &gt; application.properties &gt; application.yml &gt; application.yaml。
1253 0
|
6月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
8月前
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
490 18
|
7月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
1332 5
|
7月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
391 0
探索Spring Boot的@Conditional注解的上下文配置
|
8月前
|
安全 算法 Java
在Spring Boot中应用Jasypt以加密配置信息。
通过以上步骤,可以在Spring Boot应用中有效地利用Jasypt对配置信息进行加密,这样即使配置文件被泄露,其中的敏感信息也不会直接暴露给攻击者。这是一种在不牺牲操作复杂度的情况下提升应用安全性的简便方法。
1399 10
下一篇
开通oss服务