By custom annotations differentiate data source

There is a case, only a packet mybatis persistence layer, different bag mapper required to interface the data source, thus requiring different interface injection of different data sources mapper. It can be distinguished by custom annotation.

This problem comes from the company's Oracle database using the project, two projects in the data table TPME user, it will use part of the data table under the user portion of a TPM, and mapper interface, and configuration files from a table also on the mybatis package under Phase II.

What follows is a presentation by the MySQL database.

Database as follows:

There ssm and test database under MySQL, ssm library has the book table, test library has uesr table

     

 

FIG mybaits packet structure:

Wherein BookMapper data source ssm library book table, UserMapper data sources for user table test library

 

 To be able to distinguish between the two data sources Mapper, we need to scan the specified package. Scanning can be specified by custom annotation packet.

Custom two notes as follows:

  

mapper interfaces and configuration files as follows:

mysql.properties configuration file as follows:

 

applicationContext-dao profile as follows:

 Which mapper scan when you need to specify a custom annotation

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6        http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9     <!-- 引入数据源配置文件 -->
10     <context:property-placeholder location="classpath:mysql.properties" />
11     <!-- 配置数据源 -->
12     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
13         <property name="driverClass" value="${jdbc.driver}" />
14         <property name="jdbcUrl" value="${jdbc.url}" />
15         <property name="user" value="${jdbc.username}" />
16         <property name="password" value="${jdbc.password}" />
17     </bean>
18 
19     <bean id="dataSource-test" class="com.mchange.v2.c3p0.ComboPooledDataSource">
20         <property name="driverClass" value="${test.jdbc.driver}" />
21         <property name="jdbcUrl" value="${test.jdbc.url}" />
22         <property name="user" value="${test.jdbc.username}" />
23         <property name="password" value="${test.jdbc.password}" />
24     </bean>
25 
26     <!-- 配置mybatis会话工厂 -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <property name="dataSource" ref="dataSource" />
29     </bean>
30 
31     <!-- 配置mybatis会话工厂 -->
32     <bean id="sqlSessionFactory-test" class="org.mybatis.spring.SqlSessionFactoryBean">
33         <property name="dataSource" ref="dataSource-test" />
34     </bean>
35 
36     <!-- 配置包扫描 -->
37     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
38         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
39         <property name="basePackage" value="com.alphajuns.ssm.mybatis" />
40         <property name="annotationClass" value="com.alphajuns.ssm.utils.BookRepository" />
41     </bean>
42 
43     <!-- 配置包扫描 -->
44     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
45         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory-test" />
46         <property name="basePackage" value="com.alphajuns.ssm.mybatis" />
47         <property name="annotationClass" value="com.alphajuns.ssm.utils.UserRepository" />
48     </bean>
49 </beans>

 Test them, test results are as follows:

   

If the annotation is not used to distinguish, but only for a packet data source for scanning mapper to the above example, just the same database table in the same package from To.

 

Guess you like

Origin www.cnblogs.com/alphajuns/p/11408579.html