1,建立java工程,导入jar包
Hibernate依赖jar包,lib/required/*.jar,核心包hibernate3.jar,数据库驱动包
2,所有jar包的作用
3,创建核心配置文件到src目录中,hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/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:3306/hibernate?useUnicode=true&characterEncoding=utf-8</property> <property name="myeclipse.connection.profile">hibernate1</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/lz/javabean/customer.hbm.xml"/> </session-factory> </hibernate-configuration>
4,创建持久化类Customer.java
package com.lz.javabean;import java.io.Serializable; @SuppressWarnings("serial") public class Customer implements Serializable{ private Integer id; private String name; private Integer age; private String des; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } public Customer(Integer id, String name, Integer age, String des) { super(); this.id = id; this.name = name; this.age = age; this.des = des; } public Customer() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", des=" + des + "]"; } }
5,创建对应的数据表
create table customer ( id int primary key, name varchar(12), age int, des text )
6,创建ORM映射配置文件customer.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 package="org.hibernate.tutorial.domain"> <class name="com.lz.javabean.Customer"> <id name="id"> <generator class="native"></generator> </id> <property name="name" column="name"/> <property name="age" column="age"/> <property name="des" column="des"/> </class> </hibernate-mapping>
7,测试类
package com.lz.test; import org.hibernate.SessionFactory; import org.hibernate.classic.Session; import org.junit.Test; import com.lz.javabean.Customer; import com.lz.util.SessionFactoryUtil; public class HibernateTest { @Test public void addCustomer(){ Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); session.beginTransaction(); Customer customer=new Customer(1, "李四", 10, "帅气"); session.save(customer); session.getTransaction().commit(); session.close(); } }
成功保存Customer对象
8,其他方法的介绍
get(Customer.class,id);若主键id值不存在,返回null,立即执行sql语句
load(Customer.class,id);支持延迟加载,在使用对象时才发出sql语句,使用CGLIB代理,查询id不存在,抛出对象找不到异常
delete(customer);
update(customer);
时间: 2024-10-11 07:59:19