首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >mybatis 3.1 + sprinng 3.2特性

mybatis 3.1 + sprinng 3.2特性
EN

Stack Overflow用户
提问于 2013-02-11 14:51:50
回答 1查看 492关注 0票数 0

我们有4个应用程序运行在一个Tomcat7服务器上。现有的应用程序工作在Hibernate和Spring上。后端连接到第二个数据库,一些旧模式在这里实时保存。每个模式都被称为xxx_live和xxx_test。

当Tomcat服务器启动时,将为正确的环境设置一个JNDI属性。

  • 测试
  • 本地
  • 活着

在PropertySourcesPlaceholderConfigurer类的扩展上解析了这些属性:

代码语言:javascript
运行
复制
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配置中?类似于:

代码语言:javascript
运行
复制
<select id="findAllUsers" parameterType="list" resultType="user">
      SELECT * FROM ${mybatis.default_schema}.USER
    </select>
EN

回答 1

Stack Overflow用户

发布于 2013-02-11 15:09:41

是的,你绝对可以通过这个房产。

DAO层中的函数声明(JAVA Mapper for mybatis在春季)类似于

代码语言:javascript
运行
复制
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中) com.foo.bar是您的Java表示您的XML的包所在的位置。 这样,您将实际使用spring的好处,即DI / IOC
  3. parameterType将是Stringjava.lang.String,而不是list。

如果你需要进一步的帮助/任何疑问,可以随时要求回复。

谢谢。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14814560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档