没有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的更多相关文章

  1. Hibernate 再接触 关系映射 一对一单向外键关联

    对象之间的关系 数据库之间的关系只有外键 注意说关系的时候一定要反面也要说通 CRUD 数据库之间设计 主键关联 单向的外键关联 中间表 一对一单向外键关联 Husband.java package ...

  2. Hibernate 再接触 基础配置 搭建Log4j环境 Junit日志环境等

    <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.aut ...

  3. Hibernate 再接触 悲观锁和乐观锁

    为什么取1248 二进制 CRUD 移位效率高 在并发和效率选择一个平衡点 一般不会考虑幻读 因为我们不会再一个事务里查询两次,(只能设置为seralizable) 悲观锁和乐观锁的前提是read-u ...

  4. Hibernate 再接触 一级缓存 二级缓存 查询缓存

    缓存 就是把本来应该放在硬盘里的东西放在内存里  将来存内存里读 一级缓存: session缓存 二级缓存: sessionFactory级别的   (适合经常访问,数据量有限,改动不大) 很多的se ...

  5. Hibernate 再接触 性能优化

    Sessionclear 否则session缓存里越来越多 Java有内存泄露吗? 在语法中没有(垃圾自动回收) 但是在实际中会有 比如读文件没有关什么的 1+N问题 解决方法:把fetch设置为la ...

  6. Hibernate 再接触 CRUD

    1.save 一对多双向 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import jav ...

  7. Hibernate 再接触 核心开发接口

    1.可以重载方法进行配置文件的指定 sessionFactory = new AnnotationConfiguration().configure("hibernate.xml" ...

  8. Hibernate 再接触 ID生成策略

    Xml 方法 在student.hbm.xml中 <generator class="uuid"></generator> 取值如下 1.identity: ...

  9. Hibernate 再接触 HQL

    Category.java package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence ...

随机推荐

  1. Android之socket多线程

    一.添加权限 <uses-permission android:name="android.permission.INTERNET" /> 二.输入输出流 客户端和服务 ...

  2. Phpstorm的强大功能

    你是否也是重复,重复,一直重复的打着重复的代码? 你是否也一直重复重复 的 Ctrl+c .Ctrl+v? 当你看到此项操作的时候就会发现,原来复制粘贴还能这么玩儿... 演示效果(以phpExcel ...

  3. centos7 安装 nvm

    cd 到 /usr/local下创建nvm文件夹,并进入nvm目录, 执行命令: wget -qO- https://raw.githubusercontent.com/creationix/nvm/ ...

  4. WPF Stake

    WPF中的StackPanel.WrapPanel.DockPanel 转:http://blog.sina.com.cn/s/blog_6c81891701017a34.html StackPane ...

  5. 剑指Offer(二):替换空格

    说明: 1.本系列是根据<剑指Offer>这个系列做的一个小笔记. 2.直接动力是因为师兄师姐找工作很难,而且机械出生的我面试算法更难. 3.刚开始准备刷LeetCode.LintCode ...

  6. IntelliJ Idea设置Could not autowire. No beans of 'xxx' type found

    1.问题描述 在Idea的spring工程里,经常会遇到Could not autowire. No beans of ‘xxxx’ type found的错误提示.但程序的编译和运行都是没有问题的, ...

  7. python2和python3的内存使用情况

    python2 对回收后的整数复用内存不作处理 python3则改进了改设计,极大的减少了内存占用 例如 a = range(1000000000000) del a 此时: python2 对于a占 ...

  8. ant 小结

    ant 的配置文件是xml 格式的. 其xml根元素是 project project元素下面有 property path env target filelist patternset 其中 tar ...

  9. 36. Oracle查询数据库中所有表的记录数

    select t.table_name,t.num_rows from user_tables t

  10. JS 函数(arguments、箭头函数、bind)

    参数 函数内部可用的 arguments 对象来访问函数的实参 注意 在函数递归调用的时候(在某一刻同一个函数运行了多次,也就是有多套实参),那么 arguments 属性的值是最近一次该函数调用时传 ...