如何在hibernate中使用动态SQL?

时间:2017-03-03 07:47:41

标签: java hibernate

就像mybatis * mapper.xml一样: select * from table where 1=1 <if test="id != null" > and id = #{id,jdbcType=VARCHAR}

我也试过freemarker grammer,就像:select * from table where 1=1 <#if id != null > and id = ${id}`

我无法在Web服务器启动之前使用此语法,因为没有id。 另一种方法是我在webServer启动时不缓存任何SQL。 在memont我重新解析所有XML使用freemarker语法,然后我将它们全部缓存。但它效率低下。

1 个答案:

答案 0 :(得分:0)

当我被告知从MyBatis迁移到Hibernate时,我也遇到了动态SQL的这种情况,最后我使用了Hibernate提供的Criteria机制。在我保存的帖子中,我记得我按照这个解释了Criteria的使用以及对文档的引用。 Check this post!

如果您不使用Hibernate xml配置,我认为这将解决您的问题。