0

We have 4 applications running on a Tomcat7 server. The existing applications work on Hibernate and Spring. The backend is connected to a second database and some old schemas are kept here live. Each schema is called xxx_live and xxx_test.

When the Tomcat server starts, a JNDI property is set for the right environment.

  • Test
  • Local
  • Live

The properties are parsed on an extention of the PropertySourcesPlaceholderConfigurer class:

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);
}
}

Now we're migrating everything to MyBatis. Is there a way to inject or parse these properties into my XML configuration? Something like:

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

1 回答 1

1

是的,你绝对可以通过这个属性。

DAO 层中的函数声明(spring 中 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.bar代表 XML 的 Java 接口所在的包在哪里。
    这样,您实际上将使用弹簧的好处,即DI / IOC

  3. parameterTypeStringjava.lang.String不列出。

如果您需要进一步的帮助/有任何疑问,请随时询问。

谢谢。

于 2013-02-11T15:09:41.497 回答