高手的存在,就是让服务10亿人的时候,你感觉只是为你一个人服务......

spring ibatis多数据源

目录
  1. 1. ibatis多数据源配置
  2. 2. @Qualifier指定SqlMapClient

项目需要配置两个数据源(spring 3.0.5,ibatis2.3.4),查了一些资料,终于配置成功,在此记录一下

ibatis多数据源配置

数据源采用jndi的方式,之前一个datasource的dao.xml配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <!--DataSource -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/fds</value>
</property>
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:context/dao/sql-map-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>

按照以上的配置,增加一个datasource就copy一份改改,如下:

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
<!--DataSource -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/fds</value>
</property>
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:context/dao/sql-map-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>

<!-- fks -->
<bean id="fksDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/fks</value>
</property>
</bean>

<bean id="fksSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:context/dao/sql-map-config-fks.xml" />
<property name="dataSource" ref="fksDataSource" />
</bean>

<bean id="fksSqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="fksSqlMapClient" />
</bean>

不同的sqlMap放在对应的sql-map-config.xml中

@Qualifier指定SqlMapClient

dao层需要做简单处理,使用新的数据源需要 @Qualifier(“fksSqlMapClient”) 指定一下SqlMapClient即可

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
@Repository
public class FksAlarmlistDAOImpl extends SqlMapClientDaoSupport implements FksAlarmlistDAO {
@Autowired
@Qualifier("fksSqlMapClient")
public void setSqlMapClientTemp(SqlMapClient sqlMapClient) {
setSqlMapClient(sqlMapClient);
}

/**
* This method was generated by MyBatis Generator. This method corresponds to the database table fks_alarmlist
*
* @mbggenerated Thu Jul 28 14:43:44 CST 2016
*/
public FksAlarmlistDAOImpl() {
super();
}

/**
* This method was generated by MyBatis Generator. This method corresponds to the database table fks_alarmlist
*
* @mbggenerated Thu Jul 28 14:43:44 CST 2016
*/
public int countByExample(FksAlarmlistExample example) {
Integer count = (Integer) getSqlMapClientTemplate().queryForObject("fks_alarmlist.countByExample", example);
return count;
}