0

Tomcat7 サーバーで 4 つのアプリケーションを実行しています。既存のアプリケーションは Hibernate と Spring で動作します。バックエンドは 2 番目のデータベースに接続されており、いくつかの古いスキーマがここに保存されています。各スキーマは、xxx_live および xxx_test と呼ばれます。

Tomcat サーバーが起動すると、適切な環境用に JNDI プロパティが設定されます。

  • テスト
  • ローカル
  • ライブ

プロパティは、PropertySourcesPlaceholderConfigurer クラスの拡張で解析されます。

public class GenericPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {

private String application;
private String environment;
private static final String ENVIRONMENT = "environment";

public GenericPropertySourcesPlaceholderConfigurer(String application) throws IOException {
    this.application = application;

    this.environment = System.getProperty(ENVIRONMENT);
    if (this.environment == null) {
        this.environment = System.getenv().get(ENVIRONMENT);
    }
    initPropertySources();
}

/**
 * setup default properties configuration
 * Default configuration properties look like :
 * app-global.properties
 * app-environment.properties
 * jndi properties
 */
private void initPropertySources() throws IOException {

    MutablePropertySources propertySources = new MutablePropertySources();

    propertySources.addLast(new ResourcePropertySource(new ClassPathResource(MessageFormat.format("properties/{0}-global.properties", application))));
    propertySources.addLast(new ResourcePropertySource(new ClassPathResource(MessageFormat.format("properties/{0}/{1}.properties", environment, application))));
    propertySources.addLast(new NotFailingJndiPropertySource("jndi"));

    setPropertySources(propertySources);
    setIgnoreResourceNotFound(false);
    setIgnoreUnresolvablePlaceholders(true);
}
}

現在、すべてを MyBatis に移行しています。これらのプロパティを XML 構成に挿入または解析する方法はありますか? 何かのようなもの:

<select id="findAllUsers" parameterType="list" resultType="user">
      SELECT * FROM ${mybatis.default_schema}.USER
    </select>
4

1 に答える 1

1

はい、間違いなくこのプロパティを渡すことができます。

DAOレイヤー(春のmybatisのJAVA Mapper)での関数宣言は次のようになります

List<User> findAllUsers(@Param("schemaName") String schemaName)

この関数を呼び出すときは、スキーマ名を引数として渡します。

いくつかの提案 (MyBatis を初めて使用する場合)

  1. context.xml で spring の util タグを使用してプロパティを構成する必要があります
    <util:properties id="mailProps" location="classpath:mail.properties" />

  2. spring を使用して Mappers と Autowire をスキャンします (ここでも context.xml にあります)。

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.foo.bar" />
    </bean>
    

    com.foo.barXML を表す Java インターフェイスが配置されているパッケージはどこにありますか。
    このようにして、実際に春の利点を使用します。DI / IOC

  3. parameterTypeリストであるStringかどうか。java.lang.String

さらにサポートが必要な場合やご不明な点がございましたら、お気軽にお問い合わせください。

ありがとうございます。

于 2013-02-11T15:09:41.497 に答える