2012-03-23 111 views
0

我遇到了一個問題,我一直在做很多搜索但仍未找到答案。我們在一個URL包含多個數據庫模式,包括模式的測試副本(例如,模式1,模式2,模式1_測試,模式2_測試都在同一個網址)。我試圖通過一個屬性文件來配置每個模式的哪個版本。從屬性文件中獲取模式名稱

我們正在使用Spring和MyBatis的,可惜我是新來的兩個(所以請原諒我的無知或任何錯誤我做描述了這個問題!)


所以在我的Spring配置文件,這是在/ src目錄/主/資源存儲,我有以下摘錄: (我只加「configLocation」屬性,後來加入「sqlSessionFactoryBeanName」屬性)

<!-- define the SqlSessionFactory --> 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="typeAliasesPackage" value="com.example.domain" /> 
    <property name="configLocation" value="classpath:mybatisConfig.xml" /> 
</bean> 

<!-- scan for mappers and let them be autowired --> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="basePackage" value="com.example.something.persistence" /> 
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
</bean> 


我mybatisConfig.xml(這是根據存儲/ SRC /米AIN /資源,這應該是在類路徑)

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
<properties resource="sqlmapconfig.properties" /> 
</configuration> 


sqlmapconfig.properties(在同一文件夾)

schema1=schema1_test 


我嘗試引用屬性映射器中的一個文件在com.example.something.persistence:

<select id="test" resultType="result" parameterType="long"> 
    select ${schema1}.table.col 
    from ${schema1}.table 
</select> 

當我嘗試用Maven構建失敗一個TE ST:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'schema1' in 'class java.lang.Long' 

任何建議,將不勝感激!

+0

」存儲在/ src/main/resources下,它應該在類路徑中「<==你能證實這種情況嗎?你在類路徑中是否有文件夾「/ src/main/resources」,或者你的內部版本是否在某處複製? – 2012-03-23 15:07:41

+0

是的,它在類路徑上。它是一個將被部署到tomcat服務器的web應用程序。 [編輯 - 錯誤提交] 即使我拿出classpath部分並將其保留爲相對url鏈接(它們在同一個文件夾中),它不會改變任何內容。 是否有任何中間步驟可以讀取{$ schema1}來縮小問題的位置? – witnessmenow 2012-03-23 15:19:46

+0

我發現你指定了配置的屬性文件,但在你的映射器文件中使用了屬性引用。據我所知,這些屬性不會傳遞給映射器文件。 你使用了哪個數據庫?你能在你的JDBC URL中指定模式嗎? – 2012-03-23 15:53:39

回答

3

我放棄了試圖直接讀取性能和走下從Java路線

在路過它,所以我不得不改變「參數類型」是在映射文件中的地圖

<select id="test" resultType="result" parameterType="map"> 
    select ${schema1}.table.col 
    from ${schema1}.table where number=#{number} 
</select> 

和編輯的Java代碼映射如下

import org.apache.ibatis.annotations.Param; 

... 

public List<result> test(@Param("number") long number, @Param("schema1") String schema1); 

希望這可以幫助別人。

注&參考:

仔細對使用$ {} VS#{},差異來解釋here

如何使用多個PARAMS從here

0

wwas服用我有類似的問題並設法通過創建自定義LanguageDriver來做到這一點。 mybatis中的語言驅動程序幾乎沒有鉤子方法。有趣的是在加載每個定義的sql語句時調用的那個是createSqlSource。這允許您在此處修改語句。做到這一點的最好方法是擴展XMLLanguageDriver,這是默認驅動程序,用於解釋在xml文件和註釋中定義的查詢。

的第一件事要做的就是通過添加一些標記,其將與架構(SCHEMA_MARKER是從一個介紹如下定製laguage驅動程序類進口)來代替修改查詢:

public interface MyMabatisMapper { 
    @Select("select * from " + SCHEMA_MARKER + ".myTable ") 
    List<MyObjects> getObjects(); 
} 

現在我們創建自定義語言驅動程序將用給定的模式替換標記。

例子:

public class QueriesModifyingDriver extends XMLLanguageDriver { 

    public static final String SCHEMA_MARKER = "###MY_SCHEMA###"; 
    public static final String SCHEMA_MARKER_PATTERN = SCHEMA.replaceAll("#", "\\#"); 

    private final String schemaName; 

    // NOTE: Name of the schema passed in constructor !!! 
    public QueriesModifyingDriver(String schemaName) { 
    this.schemaName = schemaName; 
    } 

    @Override 
    public SqlSource createSqlSource(org.apache.ibatis.session.Configuration configuration, String script, Class<?> parameterType) { 
    String modifiedScript = script.replaceAll(SCHEMA_MARKER_PATTERN, schemaName); 

    return super.createSqlSource(configuration, modifiedScript, parameterType); 
    } 
}; 

上面我們可以通過構造通過我們的模式的名稱,我們的語言的驅動程序。現在我們需要做的就是將這個語言驅動程序安裝到mybatis。下面的示例假定使用Spring使用註釋配置。架構名稱是從屬性my.schema.name讀,但它可以從課程的任何來源:

@Bean 
    public SqlSessionFactory mybatisSqlSessionFactory(DataSource dataSource, @Value("${my.schema.name}") String schemaName) throws Exception { 

    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 
    sqlSessionFactoryBean.setDataSource(dataSource); 

    org.apache.ibatis.session.Configuration mybatisConfiguration = sqlSessionFactoryBean.getObject().getConfiguration(); 

    QueriesModifyingDriver queriesModifyingDriver = new QueriesModifyingDriver(schemaName); 
    mybatisConfiguration.getLanguageRegistry().register(queriesModifyingDriver); 

    mybatisConfiguration.setDefaultScriptingLanguage(QueriesModifyingDriver.class); 

    return sqlSessionFactoryBean.getObject(); 
    } 

請注意,這是非常有效的方法,因爲語言驅動程序的進程每一次queryonly(在啓動時)和這個修改過的查詢稍後會被mybatis重用。 「