Hiberbate是面向对象,需要把对象和数据库进行映射。与数据库无关,操作的是对象,会根据数据源
和数据库的方言生成对应的sql语句进行查询,是一个优秀的java持久层解决方案,是当今主流的对象-关系映射工具,减少了代码的冗余。
1、hiberbate配置文件详细介绍:
<?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://127.0.0.1:3306/javaee?useUnicode=true&characterEncoding=UTF-8</property> <property name="connection.username">用户名</property> <property name="connection.password">密码</property> <!-- 数据库连接池的大小 --> <property name="connection.pool_size">50</property> <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢--> <property name="jdbc.fetch_size">500 </property> <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大--> <property name="jdbc.batch_size">23 </property> <!-- SQL 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- 在控制台输出sql语句 --> <property name="show_sql">true</property> <!-- 在启动时根据配置更新数据库 --> <property name="hbm2ddl.auto">update</property> <!--注册我们的实体映射类--> <mapping class="com.lwz.hibernate.bean.UserEntity"/> </session-factory> </hibernate-configuration>/
2、SessionFactory工厂类的创建(可以通过官网查询:hibernate.org官网)
package com.lwz.hibernate.bean; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class MyHibernate { public static SessionFactory sessionFactory; static { final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure("com/lwz/hibernate/resource/hibernate.cfg.xml").build(); try { sessionFactory = new MetadataSources(registry).buildMetadata() .buildSessionFactory(); } catch (Exception e) { StandardServiceRegistryBuilder.destroy(registry); } } }
3、实体类定义
package com.lwz.hibernate.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user1") public class UserEntity { @Id @Column(name="id",length=32) private String userId; @Column(name="name",length=20) private String userName; @Column(name="pass",length=20) private String password; @Column(name="age",length=22) private int age; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
4、测试hibernate基本的增删改查
package com.lwz.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;
import com.lwz.hibernate.bean.MyHibernate;
import com.lwz.hibernate.bean.UserEntity;
public class T {
SessionFactory session = null;
@Before
public void config(){
session = MyHibernate.sessionFactory;
}
public Session getSession(){
return session.openSession();
}
@Test
public void saveEntity(){
//獲取session
Session s = this.getSession();
//獲取事務
Transaction tr = s.getTransaction();
tr.begin();//開啟事務
UserEntity user = new UserEntity();
user.setAge(25);
user.setPassword("666666");
user.setUserId("xx002");
user.setUserName("关二哥在此,谁敢不服");
//s.save(user);
//s.update(user);
UserEntity u = s.get(UserEntity.class, "xx001");
UserEntity u2 = s.load(UserEntity.class, "xx002");
//s.delete(u);
System.out.println(u.getUserName());
tr.commit();//事务提交
s.close();//关闭连接
System.out.println(u.getAge());
//懒加载异常
//System.out.println(u2.getUserName());
}
// public static void main(String[] args) {
//
// //System.out.println(MyHibernate.sessionFactory.openSession());
// }
}
hibernate有三种形态:瞬态(new()出来还没有session连接),持久态(session连接,保持与数据库的同步),游离态(session关闭,不能与数据库同步)。
缓存级别???一级缓存就是Session的缓存,二级缓存是SessionFactory的(Hibernate的二级缓存配置,就是配置这个)。
这两个的概念比较明确,但是我看有书上说还有三级缓存,就是所谓的查询缓存。
SessionFactory的session连接随着版本不一样,代码也不一样
hiberbnate配置和增删改查操作;添加,删除,修改需要开启事务,查询不需要开启事务
get属于即加载,load是懒加载,在使用时加载(session关闭后再去调用延迟加载汇报懒加载初始化异常)