Hibernate 再接触 Hello world 模拟Hibernate
没有Hibernate以前
Cilent 客户端 new出一个对象 然后执行JDBC 然后这样的访问数据库并不是面向对象语言
使用Hibernate以后
Cilent new 出一个对象后 访问配置文件 产生sessionfactory 然后opensession 获得一个session 拿到session后直接save 不用具体的拼sql语句
OR 即hibernate帮我们屏蔽了 R relationship 这层关系 只需要面向对象就好了
第一个小程序
首先引入相关jar包 透了懒 以前弄过 所以全部复制过来了 ‘

首先第一步 copy过来hibernate的配置文件
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings -->
<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"></property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size"></property> --> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/easy/Student.hbm.xml"/>
<!-- <mapping class="com.bjsxt.hibernate.Teacher"/> -->
</session-factory> </hibernate-configuration>
先建立一个model Student
package com.easy;
public class student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
先建立一个Xml版本的
创建Student.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 >
<class name="com.easy.student">
<id name="id" />
<property name="name" />
<property name="age" />
</class> </hibernate-mapping>
建立测试类
package com.easy; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class studenttest { public static void main(String[] args){
student su = new student();
su.setId();
su.setName("不成功");
su.setAge();
Configuration cf = new Configuration();
SessionFactory sf = cf.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(su);
session.getTransaction().commit();
session.close();
sf.close(); } }
创建数据库 hibernate 然后创建表student
该表应该有 id name age字段

插入成功
建立Annotation 版本 记得加入jar包哦
加入注解
注意包是javax.persistentce
package com.easy; import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class student {
private int id;
private String name;
private int age;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings -->
<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"></property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size"></property> --> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <!-- <mapping resource="com/easy/Student.hbm.xml"/> -->
<mapping class="com.easy.student"/>
</session-factory> </hibernate-configuration>
test
package com.easy; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration; public class studenttest { public static void main(String[] args){
student su = new student();
su.setId();
su.setName("不成功");
su.setAge();
Configuration cf = new AnnotationConfiguration();
SessionFactory sf = cf.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(su);
session.getTransaction().commit();
session.close();
sf.close(); } }
插入成功

Hibernate 再接触 Hello world 模拟Hibernate的更多相关文章
- Hibernate 再接触 关系映射 一对一单向外键关联
对象之间的关系 数据库之间的关系只有外键 注意说关系的时候一定要反面也要说通 CRUD 数据库之间设计 主键关联 单向的外键关联 中间表 一对一单向外键关联 Husband.java package ...
- Hibernate 再接触 基础配置 搭建Log4j环境 Junit日志环境等
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.aut ...
- Hibernate 再接触 悲观锁和乐观锁
为什么取1248 二进制 CRUD 移位效率高 在并发和效率选择一个平衡点 一般不会考虑幻读 因为我们不会再一个事务里查询两次,(只能设置为seralizable) 悲观锁和乐观锁的前提是read-u ...
- Hibernate 再接触 一级缓存 二级缓存 查询缓存
缓存 就是把本来应该放在硬盘里的东西放在内存里 将来存内存里读 一级缓存: session缓存 二级缓存: sessionFactory级别的 (适合经常访问,数据量有限,改动不大) 很多的se ...
- Hibernate 再接触 性能优化
Sessionclear 否则session缓存里越来越多 Java有内存泄露吗? 在语法中没有(垃圾自动回收) 但是在实际中会有 比如读文件没有关什么的 1+N问题 解决方法:把fetch设置为la ...
- Hibernate 再接触 CRUD
1.save 一对多双向 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import jav ...
- Hibernate 再接触 核心开发接口
1.可以重载方法进行配置文件的指定 sessionFactory = new AnnotationConfiguration().configure("hibernate.xml" ...
- Hibernate 再接触 ID生成策略
Xml 方法 在student.hbm.xml中 <generator class="uuid"></generator> 取值如下 1.identity: ...
- Hibernate 再接触 HQL
Category.java package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence ...
随机推荐
- Django中的ORM介绍,字段以及字段的参数。
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...
- pycharm中导入requests,xmlx等模块的方法。
现在pycharm的功能越来越强大,我们需要什么直接就可以导入: 下面正式开始介绍: 第一步: 先选择左边的:project interpreter ----->再点击后面的绿色的加号. 那 ...
- java使用zxing插件绘制二维码
ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码. 涉及到的依赖有: < ...
- WPF简单数据绑定
XAML: <!--#region 数据绑定控件--> <DataGrid x:Name="dataGrid" Grid.Column="2" ...
- expdp impdp 参数
With the Partitioning, OLAP, Data Mining and Real Application Testing options启动 "BEMIS".&q ...
- JSP 调用java 常量 枚举
JAVA:public enum ReimStatus { UNCONFIRMED ("118001"), //未确认 DISPATCH_VERIFY("118002&q ...
- 安装VMTools工具
1)什么是VMtools VM tools顾名思义就是Vmware的一组工具.主要用于虚拟主机显示优化与调整,另外还可以方便虚拟主机与本机的交互,如允许共享文件夹,甚至可以直接从本机向虚拟主机拖放文件 ...
- uva-321-暴力枚举-隐式图搜索
题意:给你n个房间,有许多灯的控制开关,i房间灯的开关在j房间,未开灯的房间不能进,i房间和j房间之间如果没有门,也不能从i进入到j,开始房间是1,并且灯是开着的,问你是否能够走到最后一个房间n,并且 ...
- [C基础修炼]如何用vs2017写一个C语言hello world程序
从微软官网下载vs2017安装后,打开 文件>新建>项目>Visual C++空项目(选择名称,位置)确定>找到源文件 鼠标右击>添加>新建项>Visual ...
- swfupload文件上传配置文件大小
在配置文件中加入: <system.web> <httpRuntime executionTimeout="36000" maxRequestLe ...