繁体   English   中英

Mybatis如何在XML中动态创建表SQL

[英]Mybatis how to dynamic create table sql in xml

以前我用

<select id="queryUser" parameterType="userInfo">
    select * from uc_login_${tableSuffix}
</select>

userInfo:{
    private String tableSuffix;
}

我用tableSuffix创建userInfo

new DateTime().getYear() + "_" + new DateTime().getMonthOfYear();

现在我从uc_login_nowYear_nowMonth(uc_login_2015_12)中选择。 现在,我不想自己创建tabkeSuffix,我希望Mybatis帮助我在xml中动态创建sql。 我怎样才能做到这一点?

好的,我找到了解决此问题的方法。 我发现一个问题: 问题

所以,我为createSuffix创建了一个util

 TimeUtil:
 public static String getLoginTableSuffix(){
    if(DateTime.now().getMonthOfYear()<10){
        return DateTime.now().getYear()+"_0"+DateTime.now().getMonthOfYear();
    }else{
        return DateTime.now().getYear()+"_"+DateTime.now().getMonthOfYear();
    }

}

然后像这样配置spring-dao.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    <property name="configurationProperties">
        <props>
            <prop key="LoginTableSuffix">#{new com.qunar.secteam.basis.web.util.TimeUtil().getLoginTableSuffix()}</prop>
        </props>
    </property>
</bean>

然后,我像这样在mybatis中使用此Param:

select * from uc_login_${LoginTableSuffix}

,这也许可以解决我的问题,但不是很好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM