【问题标题】:Mybatis how to dynamic create table sql in xmlMybatis如何在xml中动态创建表sql
【发布时间】:2015-12-01 06:56:22
【问题描述】:

以前,我用过

<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。 我该怎么做?

【问题讨论】:

    标签: java mysql spring mybatis


    【解决方案1】:

    好的,我找到了解决这个问题的方法。 我找到了一个问题:question

    所以,我创建了一个类似 createSuffix 的工具

     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}
    

    ,所以这可能是我的问题,但不是很好

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      相关资源
      最近更新 更多