hibernate是个不错的工具,现在都流行用它。
如果你的项目原来并没有使用hibernate,但是现在要用它,你怎么办?
我可以告诉你最有效的方法。
如果你用 openSession(Connection con) 想取到原始项目的连接,然后构造hibernate session,那么你错了,这样根本不行。hibernate 还是会提示你提供数据库信息。
最有效的方法就是:实现ConnectionProvider,然后,在hibernate.cfg.xml里加入
<property name="connection.provider_class">
你的实现类名
</property>
也许你担心实现ConnectionProvider复杂,其实就五个方法。你只需要实现
getConnection(),closeConnection(Connection con)这样,hibernate就
可以跑了。
如此,十分简单有效。这是hibernate提供给我们的好接口。
可是,很多书都没有提到这么用,我有点纳闷。
如果你的项目原来并没有使用hibernate,但是现在要用它,你怎么办?
我可以告诉你最有效的方法。
如果你用 openSession(Connection con) 想取到原始项目的连接,然后构造hibernate session,那么你错了,这样根本不行。hibernate 还是会提示你提供数据库信息。
最有效的方法就是:实现ConnectionProvider,然后,在hibernate.cfg.xml里加入
<property name="connection.provider_class">
你的实现类名
</property>
也许你担心实现ConnectionProvider复杂,其实就五个方法。你只需要实现
getConnection(),closeConnection(Connection con)这样,hibernate就
可以跑了。
如此,十分简单有效。这是hibernate提供给我们的好接口。
可是,很多书都没有提到这么用,我有点纳闷。