rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » JavaEE

hibernate配置

hibernate.cfg.xml配置:

mysql配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
  <session-factory>
   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
   <property name="hibernate.connection.url">jdbc:mysql://localhost/lovo</property>
   <property name="hibernate.connection.username">root</property>
   <property name="hibernate.connection.password">root</property>
   <property name="hibernate.show_sql">true</property>
   <mapping resource="com/lovo/po/User.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

oracle配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
  <session-factory>
   <property name="hibernate.dialect">org.hibernate.dialect.OracleLDialect</property>
   <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
   <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:test</property>
   <property name="hibernate.connection.username">scott</property>
   <property name="hibernate.connection.password">tiger</property>
   <property name="hibernate.show_sql">true</property>
   <mapping resource="com/lovo/po/User.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

USER表的User.hbm.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.lovo.po">
 <class name="User" table="tb_user">
  <id name="u_id">
   <generator class="native"></generator>
  </id>
  <property name="u_name" not-null="true"></property>
  <property name="u_pwd" not-null="true"></property>
 </class>
</hibernate-mapping>
生成表的方法:

public void create() {
  Configuration cfg=new Configuration().configure();
  SchemaExport sc=new SchemaExport(cfg);
  sc.create(true, true);
 }

顶一下
(0)
踩一下
(0)