第一回
1.向导创建Hibernate项目...
2.创建DB.Browser..连接数据
3.创建持久化类
4.创建Hibernate配置文件xxx.cfg.xml
如下:重要的参数
<hibernate-configuration>
<session-factory>
<!--必要的四个 参数 驱动,连接类,用户名,密码-->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:show</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 加载 -->
<property name="show_sql">true</property>
<!-- 调整格式 -->
<property name="format_sql">true</property>
<!--ddl语句创建数据库-->
<property name="hbm2ddl.auto">create</property>
<!-- 隐射类文件的完整路径 -->
<mapping resource="java16/wdy/po/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.创建对象--关系隐射文件 类名.hbm.xml
<hibernate-mapping package="java16.wdy.po">
<class name="User" table="T_USER">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="password"/>
</class>
</hibernate-mapping>
6.测试动态创建表
读取配置文件,隐射信息
Configuration cfg = new Configuration().configure();
ServiceRegistry service = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.buildServiceRegistry();
创建SessionFactory 实例
SessionFactory factory = cfg.buildSessionFactory(service);
创建会话
Session session = factory.openSession();
User user = new User();
user.setId(1);
user.setName("张三");
user.setPwd("124");
获取事务
Transaction trans = session.beginTransaction();
try {
//对象数据持久化
session.save(user);
trans.commit();
} catch (Exception e) {
trans.rollback();
e.printStackTrace();
} finally {
session.close();
factory.close();
}
}
1.向导创建Hibernate项目...
2.创建DB.Browser..连接数据
3.创建持久化类
4.创建Hibernate配置文件xxx.cfg.xml
如下:重要的参数
<hibernate-configuration>
<session-factory>
<!--必要的四个 参数 驱动,连接类,用户名,密码-->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:show</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 加载 -->
<property name="show_sql">true</property>
<!-- 调整格式 -->
<property name="format_sql">true</property>
<!--ddl语句创建数据库-->
<property name="hbm2ddl.auto">create</property>
<!-- 隐射类文件的完整路径 -->
<mapping resource="java16/wdy/po/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.创建对象--关系隐射文件 类名.hbm.xml
<hibernate-mapping package="java16.wdy.po">
<class name="User" table="T_USER">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="password"/>
</class>
</hibernate-mapping>
6.测试动态创建表
读取配置文件,隐射信息
Configuration cfg = new Configuration().configure();
ServiceRegistry service = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.buildServiceRegistry();
创建SessionFactory 实例
SessionFactory factory = cfg.buildSessionFactory(service);
创建会话
Session session = factory.openSession();
User user = new User();
user.setId(1);
user.setName("张三");
user.setPwd("124");
获取事务
Transaction trans = session.beginTransaction();
try {
//对象数据持久化
session.save(user);
trans.commit();
} catch (Exception e) {
trans.rollback();
e.printStackTrace();
} finally {
session.close();
factory.close();
}
}