hibernate如何不使用hibernate.cfg.xml文件而是动态配置数据库连接

night__cat 2008-07-09 05:26:36
数据库连接在程序里自己设置,而数据库表的映射文件还是通过hibernate.cfg.xml文件读取。不知道有没有什么好的方法?
...全文
323 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dygf1987 2010-12-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 phon_oy 的回复:]

用spring啊
[/Quote]

具体怎么用?能不能详细点?
Landor2004 2008-07-10
  • 打赏
  • 举报
回复
你可以这样,映射文件在配置文件中,而数据库连接属性等,用编程方式写入
<hibernate-configuration>
<session-factory>
<mapping resource="xxx.hbm.xml" />
<mapping resource="yyy.hbm.xml" />
</session-factory>
</hibernate-configuration>

java代码
configuration.configure("hibernate.cfg.xml");
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
configuration.setProperty("hibernate.connection.url", "jdbc:sqlserver://localhost:1433; DatabaseName=test");
configuration.setProperty("hibernate.connection.username", "sa");
......
sessionFactory = configuration.buildSessionFactory();
phon_oy 2008-07-09
  • 打赏
  • 举报
回复
用spring啊

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧