Hibernate第一节:第一个Hibernate项目
2011-11-20 09:50
246 查看
第一个Hibernate项目
1、创建java项目
HibernateFirst
2、创建User Library,加入依赖包
* HIBERNATE_HOME/lib/*.jar
* HIBERNATE_HOME/hibernate3.jar
* 加入数据库驱动(Oracle驱动)
3、提供hibernate.cfg.xml文件,完成基本的配置
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">admin</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<mapping resource="org/xiaolong/entity/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、建立实体类User.java
package org.xiaolong.entity;
import java.util.Date;
public class UserInfo {
private Integer userid;
private String username;
private String password;
private Date birthday;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
5、提供User.hbm.xml文件,完成实体类的映射
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.xiaolong.entity">
<class name="UserInfo" table="userinfo" schema="ADMIN">
<id name="userid" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="username" insert="true" length="50" type="string"></property>
<property name="password" not-null="true"></property>
<property name="birthday" type="date"></property>
</class>
</hibernate-mapping>
6、将User.hbm.xml文件加入到hibernate.cfg.xml文件中
7、编写工具类ExoprtDB.java,将hbm生成ddl,也就是hbm2ddl
package org.xiaolong.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author xiaolong
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
//param1 print the DDL to the console
//param2 export the script to the database
export.create(true, true);
}
}
下面我们来执行该工具类ExportDB,手动创建UserInfo表,执行后控制台打印结果:
再去数据库中查看,会发现表已成功创建:
这里我们也可以使用Hibernate自带的功能区生成数据库表,只需在hibernate.cfg.xml文件中配置如下信息即可:
8、创建测试类UserDAOTest,添加用户数据到oracle
package org.xiaolong.test;
import java.util.Date;
public class UserDAOTest {
@Test
public void addUser() {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
// 建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
// 取得session
Session session = null;
try {
session = factory.openSession();
// 开启事务
session.beginTransaction();
UserInfo user = new UserInfo();
user.setUsername("xiaolong");
user.setPassword("123");
user.setBirthday(new Date());
// 保存UserInfo对象
session.save(user);
// 提交事务
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// 回滚事务
session.getTransaction().rollback();
} finally {
if (session != null) {
if (session.isOpen()) {
// 关闭session
session.close();
}
}
}
}
}
最好加入如下配置项,方便观察hibernate sql的生成:
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
最好加入log4j配置文件,将该配置文件拷贝到src下,便于程序的调试
查看控制台打印结果:
OK,第一个Hibernate项目运行成功!当然其中还有很多细节没有说明,需要了解的去查下资料即可。。
1、创建java项目
HibernateFirst
2、创建User Library,加入依赖包
* HIBERNATE_HOME/lib/*.jar
* HIBERNATE_HOME/hibernate3.jar
* 加入数据库驱动(Oracle驱动)
3、提供hibernate.cfg.xml文件,完成基本的配置
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">admin</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<mapping resource="org/xiaolong/entity/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、建立实体类User.java
package org.xiaolong.entity;
import java.util.Date;
public class UserInfo {
private Integer userid;
private String username;
private String password;
private Date birthday;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
5、提供User.hbm.xml文件,完成实体类的映射
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.xiaolong.entity">
<class name="UserInfo" table="userinfo" schema="ADMIN">
<id name="userid" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="username" insert="true" length="50" type="string"></property>
<property name="password" not-null="true"></property>
<property name="birthday" type="date"></property>
</class>
</hibernate-mapping>
6、将User.hbm.xml文件加入到hibernate.cfg.xml文件中
<mapping resource="org/xiaolong/entity/UserInfo.hbm.xml"/> |
package org.xiaolong.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author xiaolong
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
//param1 print the DDL to the console
//param2 export the script to the database
export.create(true, true);
}
}
下面我们来执行该工具类ExportDB,手动创建UserInfo表,执行后控制台打印结果:
再去数据库中查看,会发现表已成功创建:
这里我们也可以使用Hibernate自带的功能区生成数据库表,只需在hibernate.cfg.xml文件中配置如下信息即可:
<property name="hbm2ddl.auto">update</property> |
package org.xiaolong.test;
import java.util.Date;
public class UserDAOTest {
@Test
public void addUser() {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
// 建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
// 取得session
Session session = null;
try {
session = factory.openSession();
// 开启事务
session.beginTransaction();
UserInfo user = new UserInfo();
user.setUsername("xiaolong");
user.setPassword("123");
user.setBirthday(new Date());
// 保存UserInfo对象
session.save(user);
// 提交事务
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// 回滚事务
session.getTransaction().rollback();
} finally {
if (session != null) {
if (session.isOpen()) {
// 关闭session
session.close();
}
}
}
}
}
最好加入如下配置项,方便观察hibernate sql的生成:
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
最好加入log4j配置文件,将该配置文件拷贝到src下,便于程序的调试
查看控制台打印结果:
OK,第一个Hibernate项目运行成功!当然其中还有很多细节没有说明,需要了解的去查下资料即可。。
相关文章推荐
- 【Hibernate框架开发之二】第一个Hibernate-Annonation项目(采用@Entity、@Id直接映射实体类)
- Hibernate实战_笔记6(第一个Hibernate项目)
- Hibernate-----第一个hibernate项目
- 《Windows程序设计第六版》译文:第一章--标记语言和代码:第一节 第一个项目(1)
- 文章标题 傻瓜式建立第一个Hibernate 项目
- Hibernate——建立第一个hibernate项目
- 第一个hibernate项目
- Intellij IDEA下的第一个Hibernate项目
- 《Windows程序设计第六版》译文:第一章--标记语言和代码:第一节 第一个项目(2)
- Intellij IDEA创建第一个hibernate项目
- 开发第一个Hibernate项目,实现插入数据功能
- 【Spring boot】第一个项目 Springboot + mysql + hibernate
- javaweb:spring mvc+spring+hibernate第一个项目
- hibernate学习笔记之(第一个Hibernate项目)
- 第一个hibernate项目
- 新建第一个Hibernate项目的步骤
- 第一个Hibernate项目
- 第一个hibernate项目
- 【Hibernate框架开发之一】搭建Hibernate环境并成功运行第一个项目HelloHibernate并解决3个常出现的运行问题;
- 第一个成功的Hibernate项目