Mybatis如何在xml中动态创建tablesql
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();
现在我 select 来自 uc_login_nowYear_nowMonth(uc_login_2015_12)。
现在,我不想自己创建 tabkeSuffix,我希望 Mybatis 帮我在 xml 中动态创建 sql。
我该怎么做?
好的,我找到了解决这个问题的方法。
我找到一个问题: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}
,所以这可能解决了我的问题,但不是很好
之前,我用
<select id="queryUser" parameterType="userInfo">
select * from uc_login_${tableSuffix}
</select>
userInfo:{
private String tableSuffix;
}
并且我使用 tableSuffix 创建 userInfo,例如
new DateTime().getYear() + "_" + new DateTime().getMonthOfYear();
现在我 select 来自 uc_login_nowYear_nowMonth(uc_login_2015_12)。 现在,我不想自己创建 tabkeSuffix,我希望 Mybatis 帮我在 xml 中动态创建 sql。 我该怎么做?
好的,我找到了解决这个问题的方法。 我找到一个问题: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}
,所以这可能解决了我的问题,但不是很好