hibernate是开源的ORM开源框架,是JPA规范的实现,提供了丰富的api通过操作对象的方式来操作对应数据库数据,利用配置文件或者注解两种元数据形式描述对象和表之间的映射关系;
另提供了缓存(包括一级缓存和二级缓存的支持)机制,提供hql,锁 特性等;
框架入门级使用流程
配置文件
maven pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zoo</groupId>
<artifactId>zoo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
</dependencies>
</project>
在classpath下新建文件: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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/world</property>
<property name="connection.username">root1</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<mapping resource="Cart.hbm.xml"/>
</session-factory>
</hibernate-configuration>
上面的 Cart.hbm.xml 是 Cart.java实体的映射文件,也放在classpath下
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="zoo.bean.Cart" table="countrylanguage">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="countrycode"></property>
<property name="language"></property>
</class>
</hibernate-mapping>
新建实体
新建一个实体类Cart.java
package zoo.bean;
public class Cart {
private String id;
private String countrycode;
private String language;
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
数据库自己需要新建一个表
新建测试类
package zoo;
import java.util.Random;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import zoo.bean.Cart;
public class Data {
public static void main(String[] args) {
Configuration cfg=new Configuration(); //creating configuration object
cfg.configure(); //默认会去加载hibernate.cfg.xml文件
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Cart c = new Cart();
String id = "" + System.currentTimeMillis() ;
c.setId(id);
c.setCountrycode("ABW");
c.setLanguage("cn" + new Random().nextDouble());
session.save(c);
t.commit();
session.close();
System.out.println("successfully saved");
}
}
对数据的操作,都是由seesion对象去操作的(实际是SessionImpl)
在hibernate里有很多接口,实现类一般都是接口名+Impl
工程目录结构