hibernate学习之路——hello word
来源:互联网 发布:淘宝优惠券如何领取 编辑:程序博客网 时间:2024/06/05 08:50
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创建SessionFactorySessionFactory sf = conf.buildSessionFactory( new StandardServiceRegistryBuilder().build() )//创建SessionSession 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();//关闭Sessionsession.close();sf.close();}}```运行Text。这样一个hibernate的小程序就完成啦!本人刚学习不久。采用李刚老师的《轻量级javaEE企业应用实战》进行学习。所写的内容可能会有错误,请指示出来,谢谢。
0 0
- hibernate学习之路——hello word
- IronPython学习 之hello word.
- Hibernate学习之一 项目搭建 hello Word!
- hibernate 学习一 hello word示例
- 1.Hibernate框架之Hello Word
- android学习日记——hello word
- Android学习笔记——HELLO WORD
- Python学习——Pycharm“Hello Word”
- C win32学习1—hello word
- 学习spring mvc之路一:简单的Hello word
- java学习之路(1)-第一个hello word
- shiro学习之路(1)---初识shiro(Hello Word)
- hibernate的hello word
- hibernate搭建hello word
- Android基础之——第一个项目Hello word!
- Android——Hello Word
- java——hello word
- springmvc学习 hello word
- 随笔 2015 7 20
- sun.jnu.encoding
- 玩转iOS 9的UIDynamics
- K-means聚类算法
- OSI七层网络参考模型
- hibernate学习之路——hello word
- Oracle的触发器
- 1009 数字1的数量
- 机器学习算法与Python实践之(三)支持向量机(SVM)进阶
- Remove Element
- Problem A: Median Value
- 【leetcode】Product of Array Except Self
- View Controller 生命周期
- 课程笔记 06:数据结构(清华) 列表-节点