Hibernate 可编程的配置方式 数据库连接经典
程序员文章站
2024-03-14 08:18:28
...
<a id="configuration-programmatic" style="text-decoration:none"><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_plain" style="color:rgb(0,0,0)"> cfg </span><span class="java_operator" style="color:rgb(0,0,0)">=</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">new</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_separator" style="color:rgb(0,0,0)">()</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addResource</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"Item.hbm.xml"</span><span class="java_separator" style="color:rgb(0,0,0)">)</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addResource</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"Bid.hbm.xml"</span><span class="java_separator" style="color:rgb(0,0,0)">);</span></a> 一个替代方法(有时是更好的选择)是,指定被映射的类,让 Hibernate 帮你寻找映射定义文件: [JAVA] view plaincopy <a id="configuration-programmatic" style="text-decoration:none"><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_plain" style="color:rgb(0,0,0)"> cfg </span><span class="java_operator" style="color:rgb(0,0,0)">=</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">new</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_separator" style="color:rgb(0,0,0)">()</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Item</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">)</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Bid</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">);</span></a> Hibernate 将会在类路径(classpath)中寻找名字为 /org/hibernate/auction/Item.hbm.xml 和/org/hibernate/auction/Bid.hbm.xml 映射定义文件。这种方式消除了任何对文件名的硬编码(hardcoded)。 org.hibernate.cfg.Configuration > 也允许你指定配置属性。例如: [JAVA] view plaincopy <a id="configuration-programmatic" style="text-decoration:none"><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_plain" style="color:rgb(0,0,0)"> cfg </span><span class="java_operator" style="color:rgb(0,0,0)">=</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">new</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_separator" style="color:rgb(0,0,0)">()</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Item</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">)</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Bid</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">)</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">setProperty</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"hibernate.dialect"</span><span class="java_separator" style="color:rgb(0,0,0)">,</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_literal" style="color:rgb(42,0,255)">"org.hibernate.dialect.MySQLInnoDBDialect"</span><span class="java_separator" style="color:rgb(0,0,0)">)</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">setProperty</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"hibernate.connection.datasource"</span><span class="java_separator" style="color:rgb(0,0,0)">,</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_literal" style="color:rgb(42,0,255)">"java:comp/env/jdbc/test"</span><span class="java_separator" style="color:rgb(0,0,0)">)</span> <span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">setProperty</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"hibernate.order_updates"</span><span class="java_separator" style="color:rgb(0,0,0)">,</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_literal" style="color:rgb(42,0,255)">"true"</span><span class="java_separator" style="color:rgb(0,0,0)">);</span></a>
推荐阅读
-
Hibernate 可编程的配置方式 数据库连接经典
-
Hibernate连接三种数据库的配置文件
-
详解Spring Hibernate连接oracle数据库的配置
-
web.config中配置数据库连接的方式
-
web.config中配置数据库连接的方式
-
Hibernate连接三种数据库的配置文件
-
详解Spring Hibernate连接oracle数据库的配置
-
oracle11g 通过修改配置文件方式连接远程数据库的方法
-
配置Springframework与hibernate连接多数据库的事务 HibernateStrutsBeanSpringWeb
-
web.config中配置数据库连接的两种方式