簡體   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