Hibernate初学者---Hello World
2017-09-05 14:17
369 查看
hibernate-release-5.2.10.Final版本学习笔记
1,hibernate.org 下载最新版,解压不用安装。
2,新建JAVA项目,项目中引入lib文件下所有JAR包,一共才30M 省心了!
同时要下载MYSQL的JDBC驱动包引入到项目中
3,项目src 目录下创建hibernate.cfg.xml文件
cfg为configuration的缩写,表示这是一份hibernate的配置文件
4,新建对象模型类,设置对象属性并分别生成get&set方法,
5, 新建MYSQL数据库及数据库表。表名与对象模型类名相同,表属性与对象模型属性一一对应。
create database hibernate;
use hibernate;
create table xxx(id int primary key,name varchar(20),age int);
6,在对象模型根目录下创建——类名.hbm.xml形式的文件。
这里的hbm是hibernate mapping的缩写,表示 这是一份hibernate的映射文件
7,创建测试类生成Main方法,
a, NEW对象模型
b, 调用set方法赋值
c, 创建Configuration对象
d, 调用Configuration的buildSessionFactory()方法返回一个SessionFactory对象
e,利用返回的SessionFactory对象NEW 一个新的SessionFactory对象
f, 调用SessionFactory的openSession()方法,返回一个新的Session
g, 利用返回的Session,NEW一个新的Session对象
h,调用Session对象的beginTransaction()方法,准备接收线程参数。
i, 调用Session的save方法传入NEW出来的对象模型。
j, 调用Session对象的getTransaction().commit()方法,提交接收到的参数。
k, 关闭Session
l, 关闭SessionFactory
8,启动测试,
9,进入数据库验证结果
1,hibernate.org 下载最新版,解压不用安装。
2,新建JAVA项目,项目中引入lib文件下所有JAR包,一共才30M 省心了!
同时要下载MYSQL的JDBC驱动包引入到项目中
3,项目src 目录下创建hibernate.cfg.xml文件
cfg为configuration的缩写,表示这是一份hibernate的配置文件
<?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> <!-- 指定数据库名--> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <!-- 指定用户名 --> <property name="connection.username">root</property> <!-- 指定密码 --> <property name="connection.password">shwythnn00</property> <!-- 指定数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 是否自动创建数据表 --> <!--<property name="hbm2ddl.auto">update</property> --> <!-- 显示SQL语句 --> <property name="show_sql">true</property> <!--输出前格式化SQL语句 --> <property name="hibernate.format_sql">true</property> <!-- 映射文件 --> <mapping resource="com/fan/bai/Baifan.hbm.xml"/> </session-factory> </hibernate-configuration>
4,新建对象模型类,设置对象属性并分别生成get&set方法,
5, 新建MYSQL数据库及数据库表。表名与对象模型类名相同,表属性与对象模型属性一一对应。
create database hibernate;
use hibernate;
create table xxx(id int primary key,name varchar(20),age int);
6,在对象模型根目录下创建——类名.hbm.xml形式的文件。
这里的hbm是hibernate mapping的缩写,表示 这是一份hibernate的映射文件
<?xml version="1.0"?> <!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="包路径 " <class name="类名" table="数据库表名"> <id name="类主属性" column="表主键名"> </id> <property name="类其它属性" /> <property name="类其它属性"/> </class> </hibernate-mapping>
7,创建测试类生成Main方法,
a, NEW对象模型
b, 调用set方法赋值
c, 创建Configuration对象
d, 调用Configuration的buildSessionFactory()方法返回一个SessionFactory对象
e,利用返回的SessionFactory对象NEW 一个新的SessionFactory对象
f, 调用SessionFactory的openSession()方法,返回一个新的Session
g, 利用返回的Session,NEW一个新的Session对象
h,调用Session对象的beginTransaction()方法,准备接收线程参数。
i, 调用Session的save方法传入NEW出来的对象模型。
j, 调用Session对象的getTransaction().commit()方法,提交接收到的参数。
k, 关闭Session
l, 关闭SessionFactory
8,启动测试,
9,进入数据库验证结果
相关文章推荐
- Hibernate环境搭建与配置方法(Hello world配置文件版)
- SSH——Hibernate初学者之旅(三)
- SSH——Hibernate初学者之旅(五)
- Hibernate初学者---多对一/一对多 双向关联
- 手低眼高 初学者学习Hibernate的方法
- 初学者,如何用hibernate配置SqlServer?(MyEclipse)
- 初学者,如何用hibernate配置SqlServer?(MyEclipse)2
- SSH——Hibernate初学者之旅(六)
- hibernate环境搭建(Hello world 配置文件版)
- hibernate初学者容易犯的错误
- SSH中Spring的hibernateTemple中提供一个得到result数据集的代码,对于SSH初学者而言,这样能将baseDao的代码融合到其中
- 基于Hibernate 4.0 的Hello World
- SSH——Hibernate初学者之旅(二)
- 汇编第一个程序 Hello World (初学者与入门)
- SSH(Spring+Struts+Hibernate)学习笔记(三)Struts 2 Hello World
- JPA的定义 以及hibernate初学者容易犯的错误
- 在运行Hibernate Hello World程序的时候,抛如下错误: view plain Exception in thread "main" org.hibernate.exception.LockAcquisitionException 解决方法
- Hibernate初学者,不加SessionFactory,自己写
- hibernate -- hello world
- 创建第一个hibernate应用程序---- Hello world