您的位置:首页 > 其它

hibernate学习之路——hello word

2015-07-20 21:44 363 查看
hibernate是所谓的三大框架之一。现在流行的数据库都是关系型的数据库。而hibernate则是属于面向对象的持久层框架。使用hibernate程序员可以用面向对象的思维去对数据库 进行操作。
首先可以从官网下载hibernate的文件或者用myeclipse安装hibernatetools插件。具体安装插件的。我的hibernate将会使用myeclipse进行学习。(通过学习过struts2的小经验,建议可以看一下hibernate文件的API,在documentation\manual\en-US\html_single中,按照里面的配置文件格式,一般不会错)
第一步:在myeclipse创建一个web项目。导入hibernate的类库(lib\request下的所有包)和mysql数据库的驱动文件。
第二步:在src创建一个包,名字自拟。为了简单。我创建了hibernate.hello。
第三步:创建一个Person类。有id,name,say三个属性。构造方法以及setter,getter方法。
```
private Integer id;
private String name;
private String say
```
第四步:配置文件默认为hibernate.cfg.xml。(在对数据库URL使用&characterEncoding=utf-8时需要对&进行转码即&characterEncoding=utf-8)

```
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,hibernate连接的数据库名. -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf-8
</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password"></property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- 根据需要自动创建数据库 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 罗列所有的映射文件 -->
<mapping resource="hibernate/hello/Person.hbm.xml"/>
</session-factory>
```
第五部:为Person类创建映射文件:在Person类同目录下创建一个Person.hbm.xml

```
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.hello">
<class name="Person" table="person_inf">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 指定主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性 type为类型-->
<property name="name" type="string"/>
<property name="say" type="string"/>
</class>
</hibernate-mapping>
```

第六步:创建一个Text类。

```
public class Text{
public static viod main(String[] args){
//实例化Configuration,//下面方法默认加载hibernate.cfg.xml文件
Configuration conf = new Configuration().configure();
//以Configuration创建SessionFactory
SessionFactory sf = conf.buildSessionFactory(
new StandardServiceRegistryBuilder().build() )
//创建Session
Session session = sf.getCurrentSession()();
//开始事务
Transaction tx = sess.beginTransaction();
//创建Person实例
Person person = new Person();
//设置消息标题和消息内容
person.setName("hibernate");
person.setSay("hello world");
//保存消息
session.save(person);
//提交事务
System.out.println(person.getSay());
tx.commit();
//关闭Session
session.close();
sf.close();
}
}
```
运行Text。这样一个hibernate的小程序就完成啦!
本人刚学习不久。采用李刚老师的《轻量级javaEE企业应用实战》进行学习。所写的内容可能会有错误,请指示出来,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: