百科定义:
hibernate是java语言下的对象关系映射解决方案
同类产品:
ibatis:半ORM产品,可以直接写sql
mybatis:这是ibatis的升级版
springJDBC: 这是spring提供的持久层技术
hibernate和mybatis各有各的方式,hibernate封装比较完整,完全是面向对象方式操作数据库,而mybatis是可以写sql的,在大多数互联网应用中,使用mybatis比较多,但是hibernate还是要学习的
安装配置
配置文件
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test"</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="entity/User.hbm.xml" /> -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
sessionFactory工具类(单例模式)
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class hibernateUtil {
private static SessionFactory factory;
static{
try{
//读取配置文件hibernate.cfg.xml
//这里最后的函数configure,如果不加的话,是读取.propertys文件
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static Session getSession()
{
return factory.openSession();
}
public static void closeSession(Session session)
{
if(session!=null)
{
if(session.isOpen())
{
session.close();
}
}
}
public static SessionFactory getSessionFactory()
{
return factory;
}
}
实体类:
package entity;
public class User {
private int id;
private String userName;
private String passWd;
public User(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWd() {
return passWd;
}
public void setPassWd(String passWd) {
this.passWd = passWd;
}
}
映射文件:
<?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="entity.User">
<id name="id">
<generator class="native"/>
</id>
<property name="userName" />
<property name="passWd" />
</class>
</hibernate-mapping>
建表代码:
package util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDb {
public static void main(String[] args) {
//读取配置文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
//自动生成对应的表
export.create(true, true);
}
}