Hibernate简单例子以及笔记
先创建Person类,代码如下:(Person.java)
import java.util.*;
public class Person {
public int id;
public String name;
public String password;
public Date birthday;
public Person() {
}
public Person(String name, String password, Date birthday) {
super();
this.name = name;
this.password = password;
this.birthday = birthday;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", password=" + password
+ ", birthday=" + birthday + "]";
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
创建person的映射,代码如下:(Person.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="Person" table="person" >
<id name="id" column="id">
<generator class="native"/>
</id>
<!-- column如果不写,那么会根据name中的值,自动创建;若已存在的话,那就自动映射 -->
<property name="name"></property>
<property name="password" column="password"></property>
<property name="birthday" column="birthday"></property>
</class>
</hibernate-mapping>
配置Hibernate文件,代码如下:(hibernate.cfg.xml)
<!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>
<!-- 那些name的属性的值都可以从hibernate文件包中的hibernate.properties拷贝,根据不同数据库类型,复制不同的语句 -->
<!-- 数据库方言,就是能让数据库发挥最大的特性 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 加载数据库的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 要连接的数据库,相当于获取连接吧 -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernatedemo</property>
<!-- 数据库账号 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password">123456</property>
<property name="show_sql">true</property>
<!-- 若表不存在,,添加这句可以自动建表 -->
<!--
create-drop 每次启动时候创建对应数据表结构,在SessionFactory关闭时候删除对应的表结构
create 每次启动时候删除上次创建的表结构然后重新创建一个新的表结构
validate 每次启动时候会验证并修改表结构
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射,这个resource的路径一定要写正确,要写上包名,可以右击那个文件,选择roperties就可以看到路径,把src后面的复制黏贴过来即可 -->
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
创建Test类,主要用于测试各个功能,代码如下:(Test.java)
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.spi.ServiceBinding;
public class Test {
public static void main(String[] args) {
// test();
// sava();
// get();
// load();
// update1();
// update2();
// merge1();
merge2();
}
private static void merge2() {
Configuration config = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(sr);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Person p1 = new Person();
p1.setId(4);
p1.setName("tttmm");
Person p2 = (Person) session.merge(p1);
p1.setName("uuudhaf");
p2.setName("kkkkjjss");
tx.commit();
session.close();
}
private static void merge1() {
Configuration config = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(sr);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Person p1 = (Person) session.load(Person.class, 3);
p1.setName("jjoo");
Person p2 = (Person) session.merge(p1);
// p1.setName("oojj");
// p2.setName("qqww");
p2.setName("qqww");
p1.setName("oojj");
tx.commit();
session.close();
}
private static void update2() {
Configuration config = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(sr);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Person p1 = new Person();
p1.setName("ooxx");// 同样执行这句时候,因为p1是临时态,因此此时的name并没有改变
p1.setId(4);
// session.update(p1);
// p1.setName("xxoo");
tx.commit();
session.close();
}
private static void update1() {
Configuration config = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(sr);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Person p1 = (Person) session.get(Person.class, 3);
p1.setName("tom"); // 这句执行完毕的时候,数据库中的name已经变成了tom了。这就是持久态的一种作用。
// session.update(p1);
// p1.setName("lalala");
// //此时数据库中的name变成了lalala,就是上面说的实时改动,也可看做是一种数据库一天记录的引用,,,一有改变就改变。
tx.commit();
session.close();
}
private static void load() {
Configuration config = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(sr);
Session session = factory.openSession();
Person p = (Person) session.load(Person.class, 3);
System.out.println("load----->" + p);
session.close();
}
private static void get() {
Configuration config = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(sr);
Session session = factory.openSession();
Person p = (Person) session.get(Person.class, 1);
System.out.println("get----->" + p);
session.close();
}
private static void sava() {
Person p = new Person("kkjj", "123456", new java.util.Date());
Configuration config = new Configuration().configure();
// 这是在hibernate4一种新的获取SessionFactory的方法,这个方法比旧的方法更高效、安全
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(sr);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
session.save(p);
tx.commit();
session.close();
}
private static void test() {
Person p = new Person("kk", "123", new java.util.Date());
// 读取配置文件
Configuration cfg = new Configuration().configure();
// hibernate的核心接口
// 下面这种方法获取SessionFactory已经被淘汰了,在hibernate4已经用另外的方法了。
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
session.save(p);
// 提交,类似执行吧,可以理解为excetuUpdate()
tx.commit();
session.close();
}
}
本文出自 “小柯奇思妙想” 博客,请务必保留此出处http://773478496.blog.51cto.com/7382353/1584571
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。