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 ...
随机推荐
- python学习笔记之二
1.python计算运行时间 方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datet ...
- django-response对象
HttpResponse 对象则需要 web 开发者自己创建,一般在视图函数中 return 回去.下面我们就来看看 HttpResponse 对象的各种细节 首先,这个对象由 HttpRespons ...
- python - requests从excel中获取测试用例数据
HttpRequests.py #-*- coding:utf-8 -*- import requests class HttpRequests(): def http_requests(self,u ...
- spring mvc 跨域问题。。。解决
官方推荐方式: http://spring.io/blog/2015/06/08/cors-support-in-spring-framework 方式1: $.ajax({ //前台:常规写法.注意 ...
- uva-10341-二分法
题意:已知方程的根在0-1范围内,求解方程的根,如果方程不存在根,那就输出 no solution. 直接二分,保留四位小数. #include "pch.h" #include ...
- python ftp文件夹文件递归上传推送
- GitHub使用指南之快速入门
出自http://blog.csdn.net/column/details/13170.html 1.Git安装 Git是一个版本控制系统,使用之前必须先下载安装,下面提供各平台的安装方式. Mac: ...
- 4. powerdesigner 生成sql脚本步骤
1. 选择数据库类型:DataBase(数据库)-- Change Current DBMS 2. 生成数据库脚本:DataBase(数据库)--generate Database
- GIFDecoder源码分析
源码见:ddxxll2008/gifdecoder_java run() public void run(){ if(in != null){ readStream(); }else if(gifDa ...
- UI学习网站
以下是我收集的UI设计的网站提供给大家参考: 站酷 www.zcool.com.cn UI中国 www.ui.cn 学UI网 www.xueui.cn UIGREAT www.uigreat.com ...