如何通过配置自动实现ValueList中hql语句的整型参数转换
程序员文章站
2024-02-25 22:38:57
通过valuelist的querymap传递过来的参数默认都为string类型,在valuelist配置文件的hql中,如果直接将该值赋给整型的字段会报错。一般我们会在hq...
通过valuelist的querymap传递过来的参数默认都为string类型,在valuelist配置文件的hql中,如果直接将该值赋给整型的字段会报错。一般我们会在hql中利用转型函数来进行处理,如下:
<entry key="areasinfoadapter">
<bean parent="abstracthibernate30adapter">
<property name="defaultnumberperpage"><value>19</value></property>
<property name="statementbuilder"><ref bean="statementbuilderhql"/></property>
<property name="defaultsortcolumn"><value>id</value></property>
<property name="defaultsortdirection"><value>desc</value></property>
<property name="hql">
<value>
from com.berheley.hcms.persistence.model.tbaseareas as tb
where tb.status='1'
/~hid : and tb.hid = to_number{hid} ~/
/~oid : and tb.oid = to_number{oid} ~/
order by tb.code asc
</value>
</property>
<property name="defaultfocuspropertyobjectalias"><value>vo</value></property>
<property name="maxrowsforfocus"><value>160000</value></property>
<property name="removeemptystrings"><value>true</value></property>
</bean>
</entry>
该方法比较麻烦,在所有的整型字段地方都需要添加函数处理,并且进行数据库迁移时会出现函数不兼容的问题。可以利用valuelist配置来对整型的字段进行统一设置,valuelist在拼装sql前会对配置好的参数进行转型。如下:
<bean id="integersetterhql" class="net.mlw.vlh.adapter.hibernate3.util.setter.integersetter"></bean>
<bean id="statementbuilderhql" class="net.mlw.vlh.adapter.hibernate3.util.statementbuilder">
<property name="setters">
<map>
<entry key="uid"><ref bean="integersetterhql"/></entry>
<entry key="oid"><ref bean="integersetterhql"/></entry>
</map>
</property>
</bean>
复制代码 代码如下:
<entry key="areasinfoadapter">
<bean parent="abstracthibernate30adapter">
<property name="defaultnumberperpage"><value>19</value></property>
<property name="statementbuilder"><ref bean="statementbuilderhql"/></property>
<property name="defaultsortcolumn"><value>id</value></property>
<property name="defaultsortdirection"><value>desc</value></property>
<property name="hql">
<value>
from com.berheley.hcms.persistence.model.tbaseareas as tb
where tb.status='1'
/~hid : and tb.hid = to_number{hid} ~/
/~oid : and tb.oid = to_number{oid} ~/
order by tb.code asc
</value>
</property>
<property name="defaultfocuspropertyobjectalias"><value>vo</value></property>
<property name="maxrowsforfocus"><value>160000</value></property>
<property name="removeemptystrings"><value>true</value></property>
</bean>
</entry>
该方法比较麻烦,在所有的整型字段地方都需要添加函数处理,并且进行数据库迁移时会出现函数不兼容的问题。可以利用valuelist配置来对整型的字段进行统一设置,valuelist在拼装sql前会对配置好的参数进行转型。如下:
复制代码 代码如下:
<bean id="integersetterhql" class="net.mlw.vlh.adapter.hibernate3.util.setter.integersetter"></bean>
<bean id="statementbuilderhql" class="net.mlw.vlh.adapter.hibernate3.util.statementbuilder">
<property name="setters">
<map>
<entry key="uid"><ref bean="integersetterhql"/></entry>
<entry key="oid"><ref bean="integersetterhql"/></entry>
</map>
</property>
</bean>
上一篇: Android打造属于自己的时间钟表